POJ 2312:Battle City【BFS & 优先队列】
来源:互联网 发布:c语言贪吃蛇源代码思路 编辑:程序博客网 时间:2024/05/29 14:17
Battle City
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7889 Accepted: 2635
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?
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 4YBEBEERESSTE0 0
Sample Output
8
AC-code:
#include<cstdio>#include<queue>#include<cstring>using namespace std;int dx[]={0,1,-1,0};int dy[]={1,0,0,-1};int n,m,vis[305][305];char map[305][305];struct node{int x,y,step;friend bool operator < (node a,node b){return a.step>b.step;}}p,temp;priority_queue<node>q;void bfs(int ex,int ey){memset(vis,0,sizeof(vis));while(!q.empty())q.pop();p.x=ex;p.y=ey;p.step=0;vis[ex][ey]=1;q.push(p);while(!q.empty()){p=q.top();q.pop();for(int i=0;i<4;i++){temp.x=p.x+dx[i];temp.y=p.y+dy[i];temp.step=p.step+1;if(temp.x<n&&!vis[temp.x][temp.y]&&temp.x>=0&&temp.y<m&&temp.y>=0&&map[temp.x][temp.y]!='S'&&map[temp.x][temp.y]!='R'){if(map[temp.x][temp.y]=='B')temp.step++;if(map[temp.x][temp.y]=='T'){printf("%d\n",temp.step);return;}vis[temp.x][temp.y]=1;q.push(temp);}}}printf("-1\n");}int main(){int i,j,ex,ey;while(scanf("%d%d",&n,&m),n||m){for(i=0;i<n;i++){scanf("%s",map[i]);for(j=0;j<m;j++)if(map[i][j]=='Y'){ex=i;ey=j;}}bfs(ex,ey);}return 0;}
0 0
- POJ 2312 Battle City 优先队列+BFS
- 【BFS+优先队列】Battle City(poj-2312)
- poj 2312 Battle City bfs +优先队列
- POJ 2312 Battle City 优先队列+BFS
- POJ 2312 Battle City(优先队列 + BFS)
- POJ 2312 Battle City(优先队列+BFS)
- POJ 2312 Battle City BFS+优先队列
- poj 2312 Battle City(优先队列+bfs)
- POJ 2312:Battle City【BFS & 优先队列】
- poj 2312 Battle City(优先队列+bfs)
- POJ 2312 Battle City【Bfs+优先队列】
- POJ 2312 Battle City 优先队列+bfs
- POJ 2312 Battle City(BFS+优先队列)
- 【POJ】2312-Battle City (bfs,优先队列)
- POJ 2312 Battle City (优先队列 + BFS)
- (POJ 2312)Battle City 优先队列 & 简单BFS
- poj 2312 Battle City(优先队列)
- poj 2312 Battle City 优先队列
- linux里的backlog详解
- vijos1514天才的记忆
- View的onMeasure()方法解读
- 2.12 执行计划并取得数据行
- 2.13 SQL执行——总览
- POJ 2312:Battle City【BFS & 优先队列】
- 【Android适配问题集锦-rom类】Android Recents App应用无法关闭引起的适配问题
- arcsde post installation 时出现 Operation failed, Unable to start iomgr.
- 2.14 小结
- RAC集群资源 Not All Endpoints Registered的故障
- android canvas博客详解
- 3.1.1 如何选择全扫描操作
- 华为机试题:密码验证合格程序
- navicat 修改数据库字符集、字符排序规则、数据库引擎