Battle City(POJ - 2312)(深搜+优先队列)
2017-07-22 10:56
453 查看
Battle City
Description
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 (See the following picture).
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?
Input
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.
Output
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.
Sample Input
Sample Output
Source
POJ Monthly,鲁小石
题意理解:就是一个起点,遇到R和S不能走,遇到B花费为2,遇到E花费为1,问走到T的花费最小是多少。
解题思路:因为每个点的花费不一样,那么我们就用优先队列加广搜,优先走步数最小的。走过的就不用再走了,因为你遇到的走过的一定花费比你小。所以只需要用一个二维数组记录有没有走过这些点就好了,当找到T的时候,就直接打印结果就好。还有千万不要忘了如果没有能走到Tde方案,就打印 -1。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8877 | Accepted: 2965 |
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 (See the following picture).
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?
Input
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.
Output
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.
Sample Input
3 4 YBEB EERE SSTE 0 0
Sample Output
8
Source
POJ Monthly,鲁小石
题意理解:就是一个起点,遇到R和S不能走,遇到B花费为2,遇到E花费为1,问走到T的花费最小是多少。
解题思路:因为每个点的花费不一样,那么我们就用优先队列加广搜,优先走步数最小的。走过的就不用再走了,因为你遇到的走过的一定花费比你小。所以只需要用一个二维数组记录有没有走过这些点就好了,当找到T的时候,就直接打印结果就好。还有千万不要忘了如果没有能走到Tde方案,就打印 -1。
#include<queue> #include<stdio.h> #include<string.h> using namespace std; struct s{ int x; //坐标X int y; //坐标Y int step; //所用步数 friend bool operator<(s a,s b) { return a.step>b.step; //优先走步数小的 } }; int book[310][310]; //记录是否有走过 char map_[310][310]; //存入地图 int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0) return 0; priority_queue<s>q; memset(book,0,sizeof(book)); memset(map_,0,sizeof(map_)); int x1=0,y1=0; for(int i=0;i<n;i++) { getchar(); for(int j=0;j<m;j++) { scanf("%c",&map_[i][j]); if(map_[i][j]=='Y') { x1=i; y1=j; } } } s start; start.x=x1; start.y=y1; start.step=0; q.push(start); int flag=0; while(!q.empty()) { s temp=q.top(); q.pop(); int next[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; for(int i=0;i<4;i++) //向四个方向找 { s tmp; tmp.x=temp.x+next[i][0]; tmp.y=temp.y+next[i][1]; if(tmp.x>n||tmp.y>m||tmp.x<0||tmp.y<0) continue; if(book[tmp.x][tmp.y]==0) { if(map_[tmp.x][tmp.y]=='B') { tmp.step=temp.step+2; book[tmp.x][tmp.y]=1; q.push(tmp); } else if(map_[tmp.x][tmp.y]=='E') { tmp.step=temp.step+1; book[tmp.x][tmp.y]=1; q.push(tmp); } else if(map_[tmp.x][tmp.y]=='T') { tmp.step=temp.step+1; flag=1; printf("%d\n",tmp.step); break; } } } if(flag==1) { break; } } if(flag==0) { printf("-1\n"); } } }
相关文章推荐
- POJ 2312 Battle City 优先队列+BFS
- POJ 2312 Battle City(优先队列+BFS)
- poj 2312 Battle City(优先队列)
- poj 2312 Battle City(优先队列+bfs)
- poj 2312 Battle City(优先队列+bfs)
- POJ2312 Battle City(优先队列+广搜BFS)
- poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
- NYOJ 284 坦克大战 && POJ 2312 Battle City (广搜+优先队列)
- poj 2312 Battle City(优先队列+bfs)
- 【POJ】2312-Battle City (bfs,优先队列)
- 深入探讨POJ 2312 Battle City 优先队列+BFS
- POJ 2312 Battle City(bfs)
- POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)
- POJ 2312 Battle City【Bfs+优先队列】
- poj 2312 Battle City
- poj 2312:Battle City
- POJ 2312 Battle City 优先队列+BFS
- Battle City(POJ 2312)
- POJ 2312 Battle City (BFS状态搜索)
- POJ 2312 Battle City 优先队列+BFS