标签:
You are the prince of Dragon Kingdom and your kingdom is in danger of running out of power. You must find power to save your kingdom and its people. An old legend states that power comes from a place known as Dragon Maze. Dragon Maze appears randomly out of nowhere without notice and suddenly disappears without warning. You know where Dragon Maze is now, so it is important you retrieve some power before it disappears.
Dragon Maze is a rectangular maze, an N×M grid of cells. The top left corner cell of the maze is (0, 0)and the bottom right corner is (N-1, M-1). Each cell making up the maze can be either a dangerous place which you never escape after entering, or a safe place that contains a certain amount of power. The power in a safe cell is automatically gathered once you enter that cell, and can only be gathered once. Starting from a cell, you can walk up/down/left/right to adjacent cells with a single step.
Now you know where the entrance and exit cells are, that they are different, and that they are both safe cells. In order to get out of Dragon Maze before it disappears, you must walk from the entrance cell to the exit cell taking as few steps as possible.
If there are multiple choices for the path you could take, you must choose the one on which you collect as much power as possible in order to save your kingdom.
The first line of the input gives the number of test cases, T(1 ≤ T ≤ 30). T test cases follow.
Each test case starts with a line containing two integers N and M(1 ≤ N, M ≤ 100), which give the size of Dragon Maze as described above.
The second line of each test case contains four integers enx, eny, exx, exy(0 ≤ enx, exx < N, 0 ≤ eny, exy < M), describing the position of entrance cell (enx, eny) and exit cell (exx, exy).
Then N lines follow and each line has M numbers, separated by spaces, describing the N×M cells of Dragon Maze from top to bottom.
Each number for a cell is either -1, which indicates a cell is dangerous, or a positive integer, which indicates a safe cell containing a certain amount of power.
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1).
If it‘s possible for you to walk from the entrance to the exit, y should be the maximum total amount of power you can collect by taking the fewest steps possible.
If you cannot walk from the entrance to the exit, y should be the string "Mission Impossible." (quotes for clarity).
2 2 3 0 2 1 0 2 -1 5 3 -1 6 4 4 0 2 3 2 -1 1 1 2 1 1 1 1 2 -1 -1 1 1 1 1 1
Case #1: Mission Impossible. Case #2: 7
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> #include<queue> #define INF 100000000 using namespace std; int mp[105][105],n,m,sx,sy,ex,ey,step[105][105],val[105][105]; bool vis[105][105]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; int minstep; bool check(int x,int y) { if(x<0||y<0||x>=n||y>=m||mp[x][y]==-1) return false; return true; } void bfs() { memset(step,0,sizeof(step)); memset(val,0,sizeof(val)); memset(vis,0,sizeof(vis)); queue<int> q; q.push(sx),q.push(sy); vis[sx][sy]=1; val[sx][sy]=mp[sx][sy]; while(!q.empty()) { int fx,fy; fx=q.front(),q.pop(); fy=q.front(),q.pop(); //cout<<fx<<" "<<fy<<endl; //system("pause"); if(step[fx][fy]>minstep) continue; if(fx==ex&&fy==ey) { minstep=step[fx][fy]; } for(int i=0;i<4;i++) { int xx,yy; xx=fx+dx[i],yy=fy+dy[i]; if(!check(xx,yy)) continue; if(step[xx][yy]==step[fx][fy]+1) { val[xx][yy]=max(val[xx][yy],val[fx][fy]+mp[xx][yy]); } if(vis[xx][yy]) continue; vis[xx][yy]=1; step[xx][yy]=step[fx][fy]+1; val[xx][yy]=val[fx][fy]+mp[xx][yy]; q.push(xx),q.push(yy); } } } int main() { int tt,cas=1; scanf("%d",&tt); while(tt--) { minstep=INF; scanf("%d%d",&n,&m); scanf("%d%d%d%d",&sx,&sy,&ex,&ey); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) scanf("%d",&mp[i][j]); } bfs(); if(minstep==INF) printf("Case #%d: Mission Impossible.\n",cas++); else printf("Case #%d: %d\n",cas++,val[ex][ey]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/water-full/p/4510800.html