POJ - 2312 Battle City

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

题目大意:给你一个mxn的字符矩阵,S和R是墙和河,都走不过去。B是需要两步才能走过去,E就是空地,需要一步可以走过去。Y是起点,T是终点。问从起点到终点的最少步数。 

解题思路:最短路径就使用bfs来遍历图,因为当遇到B时需要走两步,所以使用优先队列,这样当遇到B时就把B放到下一层次搜索

AC代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=350;
int vis[maxn][maxn];
int m,n;
char map[maxn][maxn];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
struct node{
	int x,y,turns;
	bool friend operator <(node a,node b)
	{
		return a.turns>b.turns;//turns小的优先级大 
	}
}start,end;
void bfs()
{
	memset(vis,0,sizeof(vis));
	node now,nextt;
	priority_queue<node> q;
	now=start;
	now.turns=0;
	vis[now.y][now.x]=1;
	q.push(now);
	while(!q.empty())
	{
		now=q.top();
		q.pop();
		for(int i=0;i<4;i++)
		{
			nextt.x=now.x+dir[i][0];
			nextt.y=now.y+dir[i][1];
			if(nextt.x<0||nextt.x>=n||nextt.y<0||nextt.y>=m||vis[nextt.y][nextt.x]
			||map[nextt.y][nextt.x]=='R'||map[nextt.y][nextt.x]=='S')
				continue;
			nextt.turns=now.turns+1;//步数先加一,以便当前走到终点直接输出 
			if(nextt.x==end.x&&nextt.y==end.y)
			{
				cout<<nextt.turns<<endl;
				return;
			}
			else if(map[nextt.y][nextt.x]=='B')
				nextt.turns+=1;//额外在加一步 
			q.push(nextt);
			vis[nextt.y][nextt.x]=1;
		}
	}
	cout<<"-1"<<endl;
	return;
} 
int main()
{
	while(cin>>m>>n,m+n)
	{
		for(int i=0;i<m;i++)
		{
			getchar();//吸收换行符 
			for(int j=0;j<n;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='Y')
				{
					start.y=i;
					start.x=j;
				}
				if(map[i][j]=='T')
				{
					end.y=i;
					end.x=j;
				}
			}
		}
		bfs();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40707370/article/details/84935740
今日推荐