【POJ】2312-Battle City (bfs,优先队列)

来源:互联网 发布:湖南省网络作家协会 编辑:程序博客网 时间:2024/05/15 20:37

Battle City
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7805 Accepted: 2613

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

3 4YBEBEERESSTE0 0

Sample Output

8

Source

POJ Monthly,鲁小石

第一次学bfs和优先队列,意料之中的十连跪。

一开始的思路是不断地更新扫描过的地图,连续TLE了4次,后来才采用标记地图的方法,但是在优先队列的重载的地方又4连WA。

此题记下以后反思吧。


代码如下:

#include <queue>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int st_x,st_y,end_x,end_y;//起点、终点 char map[311][311];//原地图 bool vis_map[311][311];//扫描地图 int move_x[4]={0,0,1,-1};int move_y[4]={1,-1,0,0};//移动 int H,W;int check(int x,int y)//检查该点是否合法 {if (x<0 || y<0 || x>=H || y>=W || vis_map[x][y] || map[x][y]=='S' || map[x][y]=='R')return 1;//不能移动到此elsereturn 0; }struct tank{int x,y,stp;bool friend operator<(tank num1,tank num2){return num1.stp>num2.stp;}}a,next;int bfs()//搜索{priority_queue<tank> move;a.x=st_x;a.y=st_y;a.stp=0;move.push(a);vis_map[st_x][st_y]=1;while (!move.empty()){a=move.top();move.pop();if (a.x==end_x && a.y==end_y)return a.stp;for (int i=0;i<4;i++){next.x=a.x+move_x[i];next.y=a.y+move_y[i];next.stp=a.stp;if (check(next.x,next.y))//移动是否合法 continue;if (map[next.x][next.y]=='B')//是否为软墙,是则多花一步 next.stp+=2;elsenext.stp++;vis_map[next.x][next.y]=1;move.push(next);}}return -1;}int main(){while (~scanf ("%d %d",&H,&W) && (H||W)){for (int i=0;i<H;i++)//输入并寻找起点终点 {scanf ("%s",map[i]);for (int j=0;j<W;j++){if (map[i][j]=='Y'){st_x=i;st_y=j;}else if (map[i][j]=='T'){end_x=i;end_y=j;}}}memset(vis_map,0,sizeof(vis_map));int ans;ans=bfs();printf ("%d\n",ans);}}


纠结了三天的题,AC了还是蛮开心的。



0 0
原创粉丝点击