码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2312 Battle City【bfs+优先队列】

时间:2015-07-29 22:43:00      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

 
Battle City
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7579   Accepted: 2544

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 4
YBEB
EERE
SSTE
0 0

Sample Output

8
Y是自己  B是砖墙(可以打破)  S是铁墙(无法通过) R是河流(无法通过) T是敌人 当遇到B花费加2当遇到E花费加1
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define MAX 1100
char map[MAX][MAX];
int vis[MAX][MAX];
int x1,x2,y1,y2;
int n,m;
struct node
{
	int a;
	int b;
	int cost;
	friend bool operator < (node a,node b)
	{
		return a.cost>b.cost;
	}
};
void bfs()
{
	int i,j,t;
	int move[4][2]={0,1,0,-1,-1,0,1,0};
	node beg,end;
	priority_queue<node>q;
	beg.a=x1;
	beg.b=y1;
	beg.cost=0;
	q.push(beg);
	vis[x1][y1]=1;
	while(!q.empty())
	{
		end=q.top();
		q.pop();
		if(end.a==x2&&end.b==y2)
		{
			printf("%d\n",end.cost);
			return ;
		}
		for(i=0;i<4;i++)
		{
			beg.a=end.a+move[i][0];
			beg.b=end.b+move[i][1];
			if(!vis[beg.a][beg.b]&&0<beg.a&&beg.a<=n&&beg.b>0&&beg.b<=m&&map[beg.a][beg.b]!=‘S‘&&map[beg.a][beg.b]!=‘R‘)
			{
				vis[beg.a][beg.b]=1;
				if(map[beg.a][beg.b]==‘B‘)
				beg.cost=end.cost+2;
				else
				beg.cost=end.cost+1;
				q.push(beg);
			}
		}
	}
	printf("-1\n");
}
int main()
{
	int j,i,s,t;
	while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)
	{
		for(i=1;i<=n;i++)
		{
			getchar();
			for(j=1;j<=m;j++)
			{
				scanf("%c",&map[i][j]);
				if(map[i][j]==‘Y‘)
				{
					x1=i;y1=j;
				}
				else if(map[i][j]==‘T‘)
				{
					x2=i;y2=j;
				}
			}
		}
		memset(vis,0,sizeof(vis));
	    bfs();
	}
	return 0;
}

  



poj 2312 Battle City【bfs+优先队列】

标签:

原文地址:http://www.cnblogs.com/tonghao/p/4687316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!