-
描述
-
Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now.
What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you .
Your tank can't move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there?-
输入
-
The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R' (river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.
输出
-
For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead.
样例输入
-
3 4 YBEB EERE SSTE 0 0
样例输出
-
8
来源
-
POJ
题目大意:在地图中有几种障碍物,“S” 金属块(你不能打破),“B”砖块,可以打破它(需要一分钟,打破之后就相当于没有障碍物了,即穿过一个砖块要两分钟),“R”河流(不能通过),“E”没有障碍物(走一步要一分钟)。现在你处于“Y”,从“Y”走到“T”,输出需要的最短时间。解题思路:刚开始,我直接是用的BFS,结果WA,后来在讨论区发现一个测试数据,结果发现自己的程序确实过不了这个数据。之后才知道这个题目要用优先队列+BFS,要好好理解这些数据结构才能快速切题,加油关于优先队列的使用方法其实和队列差不多,可以在这里看看用法: http://blog.youkuaiyun.com/yin_zongming/article/details/21559981下面是AC代码
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<string> #include<stack> #include<queue> #include<vector> #include<algorithm> #include<iostream> using namespace std; #ifdef __int64 typedef __int64 LL; #else typedef long long LL; #endif #define maxn 300+10 int vis[maxn][maxn]; char v[maxn][maxn]; int dir[4][2]={-1,0,1,0,0,1,0,-1}; int m,n; int bx,by; int cnt; struct point { int x,y,step; friend bool operator < (const point &s1,const point &s2)//改变优先级,由于优先队列默认是大的数字优先级高 { //现在改为step小的优先级高,符合题意 return s1.step>s2.step; } }; void bfs(int x,int y) { priority_queue<point>p; point t,q; t.x=x;t.y=y;t.step=0; p.push(t); while(!p.empty()) { t=p.top();//优先队列是用top获取队列头部的元素,而队列是用front,注意 p.pop(); if(v[t.x][t.y]=='T') { cnt=t.step; return ; } for(int i=0;i<4;i++) { q.x=t.x+dir[i][0]; q.y=t.y+dir[i][1]; if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&!vis[q.x][q.y]&&v[q.x][q.y]!='S'&&v[q.x][q.y]!='R') { vis[q.x][q.y]=1; if(v[q.x][q.y]=='B') { q.step=t.step+2; p.push(q); } else { q.step=t.step+1; p.push(q); } } } } } int main() { while(scanf("%d%d",&m,&n)&&m&&n) { for(int i=0;i<m;i++) { scanf("%s",v[i]); for(int j=0;j<n;j++) { if(v[i][j]=='Y') { bx=i; by=j; } } } memset(vis,0,sizeof(vis)); vis[bx][by]=1; cnt=0; bfs(bx,by); if(cnt) printf("%d\n",cnt); else printf("-1\n"); } return 0; }
-
The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R' (river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.