hdu2102 双层bfs

75 篇文章 0 订阅
6 篇文章 0 订阅

hdu2102


当传送的目的地是墙或者仍是传送机的时候就不要去了,把他当做墙.

#include<iostream>
#include<queue>
using namespace std;

struct point{
	int x,y,step,type;
}	P[105];
char mp[2][12][12];
int M,N,T,dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int bfs(point X)
{
	point t,tt;
	queue<point> Q;
	Q.push(X);
	while(!Q.empty())
	{
		t=Q.front();
		Q.pop();
		if(mp[t.type][t.x][t.y]=='*')	continue;
		if(mp[t.type][t.x][t.y]=='P')	return 1;
		mp[t.type][t.x][t.y]='*';
		for(int i=0;i<4;i++){
			tt.x=t.x+dir[i][0];tt.y=t.y+dir[i][1];tt.step=t.step+1;tt.type=t.type;
			if(tt.step>T||mp[tt.type][tt.x][tt.y]=='*')	continue;
			
			if(mp[tt.type][tt.x][tt.y]=='#'){				//如果是#
				mp[tt.type][tt.x][tt.y]=='*';				//变为*
				tt.type=1-tt.type;							//传送到另一层 
				if(mp[tt.type][tt.x][tt.y]=='#'||mp[tt.type][tt.x][tt.y]=='*'){			//忽略 
					mp[tt.type][tt.x][tt.y]=mp[1-tt.type][tt.x][tt.y]='*';			//变为* 
					continue;								//换个方向
				}
			}
			Q.push(tt);
		}
	}
	return 0;
}

int main()
{
	int i,j,C;
	point S;
	scanf("%d",&C);
	while(C--)
	{
		memset(mp,'*',sizeof(mp));
		cin>>M>>N>>T;
		for(i=1;i<=M;i++)	for(j=1;j<=N;j++)	cin>>mp[0][i][j];
		for(i=1;i<=M;i++)	for(j=1;j<=N;j++)	cin>>mp[1][i][j];
		
		S.x=1;S.y=1;S.step=0;S.type=0;
		
		if(bfs(S))	printf("YES\n");
		else		printf("NO\n");
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值