关闭

hdu2102

标签: bfs
67人阅读 评论(0) 收藏 举报

http://acm.hdu.edu.cn/showproblem.php?pid=2102

不多说,被两层相同位置都是#坑惨了,一开始没有考虑到,简单的bfs,上代码

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
struct node
{
	int floor,x,y;
	int t;
};
char map[2][10][10];
int mov[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int book[2][10][10];
int xx,yy;
bool bfs(int n,int m,int t)
{
	queue<node>q;
	node point1,point2;
	memset(&point1,0,sizeof(point1));
	book[0][0][0] = 1;
	q.push(point1);
	while(!q.empty())
	{
		int next_x,next_y;
		point1 = q.front();
		q.pop();
		for(int i = 0;i<4;i++)
		{
			int fl = point1.floor;
			next_x = point1.x+mov[i][0];
			next_y = point1.y+mov[i][1];
			if(map[fl][next_x][next_y]=='#')fl = !fl;
			if(next_x<0 || next_x>n-1 || next_y<0 || next_y>m-1)continue;
			if(map[fl][next_x][next_y]=='P' && point1.t+1<=t)
			{
				return true;
			}
			if(map[fl][next_x][next_y]!='*' && point1.t+1<t && book[fl][next_x][next_y]!=1)
			{
				point2.floor = fl;
				point2.t = point1.t+1;
				point2.x = next_x;
				point2.y = next_y;
				q.push(point2);
				book[fl][next_x][next_y] = 1;
			}
		}
	}
	return false;
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		memset(book,0,sizeof(book));
		memset(map,0,sizeof(map));
		int N,M,t,x,y,fl;
		scanf("%d%d%d",&N,&M,&t);
		getchar();
		for(int i = 0;i<2;i++)
		{
			for(int j = 0;j<N;j++)
			{
				for(int k = 0;k<M;k++)
				{
					scanf("%c",&map[i][j][k]);
					if(map[i][j][k]=='P')
					{xx=j,yy=k;}
				}
				getchar();
			}
			if(i==0)getchar();
		}
		for(int i = 0;i<N;i++)
		{
			for(int j = 0;j<M;j++)
			{
				if(map[0][i][j] == '#' && map[1][i][j] == '#')
				{map[0][i][j] = '*';map[1][i][j] = '*';}
			}
		}
		book[0][0][0] = 1;
		bool boo = bfs(N,M,t);
		if(boo==true)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2283次
    • 积分:338
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条