HDU 1429 状态压缩搜索
胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4105 Accepted Submission(s): 1365
Problem Description
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)……
这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方。刚开始Ignatius被关在(sx,sy)的位置,离开地牢的门在(ex,ey)的位置。Ignatius每分钟只能从一个坐标走到相邻四个坐标中的其中一个。魔王每t分钟回地牢视察一次,若发现Ignatius不在原位置便把他拎回去。经过若干次的尝试,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
每组测试数据之间有一个空行。
. 代表路
* 代表墙
@ 代表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
解题思路:用状态压缩来记录每个位置所拥有的钥匙状态,然后广搜。
代码:
#include<iostream>
#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
int hash[22][22][1050];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int n,m,t;
char map[22][22];
struct state
{
int x,y,st,key;
bool logic()
{
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='*')return 1;
return 0;
}
}start,cur,next;
queue<state> q;
int bfs()
{
while(!q.empty())
{
cur=q.front();q.pop();
if(map[cur.x][cur.y]=='^')if(cur.st<t)return cur.st;
if(cur.st>=t)break;
for(int i=0;i<4;i++)
{
next=cur;
next.x+=dx[i];
next.y+=dy[i];
next.st++;
if(next.logic())
{
if(map[next.x][next.y]>='A'&&map[next.x][next.y]<='J')
{
int key=map[next.x][next.y]-'A';
if(((next.key>>key)&1)&&!hash[next.x][next.y][next.key])
{
hash[next.x][next.y][next.key]=1;
q.push(next);
}
}
else if(map[next.x][next.y]>='a'&&map[next.x][next.y]<='j')
{
int key=map[next.x][next.y]-'a';
next.key|=(1<<key);
if(!hash[next.x][next.y][next.key])
{
hash[next.x][next.y][next.key]=1;
q.push(next);
}
}
else if(!hash[next.x][next.y][next.key])
{
hash[next.x][next.y][next.key]=1;
q.push(next);
}
}
}
}
return -1;
}
int main()
{
int i,j,k;
while(~scanf("%d%d%d",&n,&m,&t))
{
for(i=0;i<n;i++)scanf("%s",map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(map[i][j]=='@')
{
start.x=i;
start.y=j;
start.st=0;
start.key=0;
}
memset(hash,0,sizeof(hash));
while(!q.empty())q.pop();
hash[start.x][start.y][start.key]=1;
q.push(start);
printf("%d\n",bfs());
}
return 0;
}
#include<stdio.h>
#include<queue>
#include<cstring>
using namespace std;
int hash[22][22][1050];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
int n,m,t;
char map[22][22];
struct state
{
int x,y,st,key;
bool logic()
{
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='*')return 1;
return 0;
}
}start,cur,next;
queue<state> q;
int bfs()
{
while(!q.empty())
{
cur=q.front();q.pop();
if(map[cur.x][cur.y]=='^')if(cur.st<t)return cur.st;
if(cur.st>=t)break;
for(int i=0;i<4;i++)
{
next=cur;
next.x+=dx[i];
next.y+=dy[i];
next.st++;
if(next.logic())
{
if(map[next.x][next.y]>='A'&&map[next.x][next.y]<='J')
{
int key=map[next.x][next.y]-'A';
if(((next.key>>key)&1)&&!hash[next.x][next.y][next.key])
{
hash[next.x][next.y][next.key]=1;
q.push(next);
}
}
else if(map[next.x][next.y]>='a'&&map[next.x][next.y]<='j')
{
int key=map[next.x][next.y]-'a';
next.key|=(1<<key);
if(!hash[next.x][next.y][next.key])
{
hash[next.x][next.y][next.key]=1;
q.push(next);
}
}
else if(!hash[next.x][next.y][next.key])
{
hash[next.x][next.y][next.key]=1;
q.push(next);
}
}
}
}
return -1;
}
int main()
{
int i,j,k;
while(~scanf("%d%d%d",&n,&m,&t))
{
for(i=0;i<n;i++)scanf("%s",map[i]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(map[i][j]=='@')
{
start.x=i;
start.y=j;
start.st=0;
start.key=0;
}
memset(hash,0,sizeof(hash));
while(!q.empty())q.pop();
hash[start.x][start.y][start.key]=1;
q.push(start);
printf("%d\n",bfs());
}
return 0;
}

浙公网安备 33010602011771号