Poj-2312 Battle City

Poj-2312 Battle City_第1张图片Poj-2312 Battle City_第2张图片Poj-2312 Battle City_第3张图片Poj-2312 Battle City_第4张图片Poj-2312 Battle City_第5张图片

#include
#include
#include
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_queueq;
	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]&&00&&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;
}

你可能感兴趣的:(bfs)