坦克大战(bfs简单变形)

坦克大战

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
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?
输入
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
上传者

sadsad


本来应该是简单BFS的变形,对B特殊判断

本来打算放在for对ed的判断上,发现有点小问题,如果放在对ed的判断上,可能一条路径的墙打通另一条路径就直接经过了


所以变形转化一下,(根据实际情况,实际走的路径来变换)


#include 
#include 
#include 
#include 
#include 
using namespace std;

char map[310][310];
int book[310][310];
int n,m;
struct node
{
	int x,y;
	int step;
}st,ed;

int bfs()
{
	int next[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
	queueQ;
	Q.push(st);
	//printf("**%d %d\n",st.x,st.y);
	while(!Q.empty())
	{
		st=Q.front();
		Q.pop();
		if(map[st.x][st.y]=='T')
		{
			return st.step;
		}
		if(map[st.x][st.y]=='B')
		{
			st.step=st.step+1;
			map[st.x][st.y]='E';
			Q.push(st);
			continue;
		}
		for(int i=0;i<4;i++)
		{
			ed.x=st.x+next[i][0];
			ed.y=st.y+next[i][1];
			if(ed.x<0||ed.x>=n||ed.y<0||ed.y>=m||map[ed.x][ed.y]=='S'||map[ed.x][ed.y]=='R'||book[ed.x][ed.y]==1)
			{
				continue;
			}
			ed.step=st.step+1;
			book[ed.x][ed.y]=1;
			Q.push(ed);
		}
	}
	return -1;
}

int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		memset(book,0,sizeof(book));
		if(n==0&&m==0)
		{
			break;
		}
		for(int i=0;i


你可能感兴趣的:(qdu_蓝桥训练,搜索)