hdu 2102

额,还是BFS,只不过分层进行而已,注意时空机部分的就好了

真的好熟了呀,可是每次总有一些小错误要改好久

郁闷呐

#include<iostream>
#include<queue>
using namespace std;
char map[2][10][10];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}},n,m,T;
bool vis[2][10][10],flag;
struct node 
{
	int x,y,z,cnt;
	node(int _z=0,int _x=0,int _y=0,int _cnt=0):z(_z),x(_x),y(_y),cnt(_cnt){};
	friend bool operator <(const node& a,const node &b)
	{
		return a.cnt>b.cnt;
	}
};
void bfs()
{
	node f;
	f.x=0;f.y=0;f.z=0;f.cnt=0;
	priority_queue<node> Q;
	Q.push(f);
	vis[0][0][0]=1;
	while(!Q.empty())
	{
		node t=Q.top();
		Q.pop();
		if(t.cnt>T) return ;
		if(map[t.z][t.x][t.y]=='P')
		{
			flag=1;
			return;
		}
		for(int k=0;k<4;k++)
		{
			int i=t.x+dir[k][0];
			int j=t.y+dir[k][1];
		  // cout<<i<<' '<<j<<endl;
			if(i<n&&i>=0&&j<m&&j>=0&&!vis[t.z][i][j]&&map[t.z][i][j]!='*')
			{	
				vis[t.z][i][j]=1;
				if(map[t.z][i][j]=='#')
				{
					int c;
					if(t.z==0)
						c=1;
					else c=0;
					if(!vis[c][i][j]&&map[c][i][j]!='#'&&map[c][i][j]!='*')//这里记得上下俩层相对位置是否同为时空机
					{
						vis[c][i][j]=1;
						Q.push(node(c,i,j,t.cnt+1));
					}
				}
				else Q.push(node(t.z,i,j,t.cnt+1));
			}
		}
	}
	return;
}
int main()
{
	int cas;
	cin>>cas;
	while(cas--)
	{
		cin>>n>>m>>T;
		for(int i=0;i<n;i++)
			cin>>map[0][i];
		for(int i=0;i<n;i++)
			cin>>map[1][i];
		memset(vis,0,sizeof(vis));
		flag=0;
		bfs();
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}
posted @ 2011-08-04 00:49  枕边梦  阅读(205)  评论(0编辑  收藏  举报