hdu1429(bfs利用状态压缩)

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1429

胜利大逃亡(续)

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3803    Accepted Submission(s): 1246


Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……

这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,Ignatius已画出整个地牢的地图。现在请你帮他计算能否再次成功逃亡。只要在魔王下次视察之前走到出口就算离开地牢,如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败。
 

Input
每组测试数据的第一行有三个整数n,m,t(2<=n,m<=20,t>0)。接下来的n行m列为地牢的地图,其中包括:

. 代表路
* 代表墙
@ 代表Ignatius的起始位置
^ 代表地牢的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J

每组测试数据之间有一个空行。
 

Output
针对每组测试数据,如果可以成功逃亡,请输出需要多少分钟才能离开,如果不能则输出-1。
 

Sample Input
  
  
4 5 17 @A.B. a*.*. *..*^ c..b* 4 5 16 @A.B. a*.*. *..*^ c..b*
 

Sample Output
  
  
16 -1
 

将带钥匙的状态整理为二进制表示,因为一共只有10串钥匙,所以开个2的10次方的数组来记录就行

能否开门用'&'来计算,是否有着串钥匙用‘|’来计算

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node{
	int x,y,t;
	int s;
};
int m,n,maxt,vi[21][21][1030],a[4][2]={-1,0,1,0,0,-1,0,1};
char map[21][21];
int find(node s)
{
	if(s.x>=0&&s.y>=0&&s.x<m&&s.y<n&&map[s.x][s.y]!='*')
		return 1;
	else return 0;
}
int bfs(node here)
{
	queue<node> qi;
	qi.push(here);
	node next;
	int i,key;
	while(!qi.empty())
	{
		here=qi.front();
		qi.pop();
		if(here.t>=maxt) return 0;
		if(map[here.x][here.y]=='^') return here.t;
		for(i=0;i<4;i++)
		{
			next.x=here.x+a[i][0];
			next.y=here.y+a[i][1];
			next.t=here.t+1;
			next.s=here.s;
			if(find(next))
			{
				if(map[next.x][next.y]>='a'&&map[next.x][next.y]<='j')
				{
					key=1<<(map[next.x][next.y]-'a'); //记录编号
					next.s=(next.s|key);
					if(!vi[next.x][next.y][next.s])
					{
						vi[next.x][next.y][next.s]=1;
						qi.push(next);
					}
				}
				else if(map[next.x][next.y]>='A'&&map[next.x][next.y]<='J')
				{
					key=map[next.x][next.y]-'A';
					if(((next.s>>key)&1)&&!vi[next.x][next.y][next.s])
					{
						vi[next.x][next.y][next.s]=1;
						qi.push(next);
					}
				}
				else if(!vi[next.x][next.y][next.s])
				{
					vi[next.x][next.y][next.s]=1;
					qi.push(next);
				}
			}
		}
	}
	return 0;
}
int main()
{
	node s;
	int i,j,ans;
	while(scanf("%d%d%d",&m,&n,&maxt)>0)
	{
		getchar();
		for(i=0;i<m;i++)
		{
			scanf("%s",map[i]);
			for(j=0;j<n;j++)
			{
				if(map[i][j]=='@')
				{
					s.x=i;
					s.y=j;
					s.t=0;
					s.s=0;
					vi[s.x][s.y][s.s]=1;
				}
			}
		}
		memset(vi,0,sizeof(vi));
		ans=bfs(s);
		if(ans) printf("%d\n",ans);
		else printf("-1\n");
		/*for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
				printf("%c",map[i][j]);
			printf("\n");
		}*/
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值