Description
话说
CZ
由于不守基道,被妖怪抓走了,好基友
WP
在努力讨好高富帅
RQ
救出
CZ
的同时,
CZ
也意识到了自己的错误,然后努力的想逃出妖怪的闺房。
Input
每组测试数据的第一行有三个整数
n,m,t(2<=n,m<=20,t>0)。接下来的
n行
m列为闺房的地图,其中包括
:
. 代表路
* 代表墙
@ 代表CZ的起始位置
^ 代表闺房的出口
A-J 代表带锁的门,对应的钥匙分别为a-j
a-j 代表钥匙,对应的门分别为A-J
. 代表路
* 代表墙
@ 代表CZ的起始位置
^ 代表闺房的出口
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
Hint
用进制转换的方法标记第三维,也就是钥匙的状态,也想明白了,当前状态的钥匙存储状态,无法保证同一时刻正在进行的另一状态,有无钥匙,所以说必须是三维数组,同时存储当前第三维所有 该钥匙位置的状态
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;
const int INF = 1<<10;//将1左移十位
const int N = 22;
const int Size = 999999;
char map[N][N] ;
int vis[N][N][INF];//三维表示状态
struct node
{
int x,y,z,ans ;
}q[Size];//结构体成员变量
int n,m,T;
int mv[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};//定义一个移动数组
void BFS(int x ,int y)
{
int s = 0 , e = 0 ;
node f , t ;
t.x = x ;
t.y = y ;
t.ans = 0 ;
t.z = 0 ;
q[e++] = t ;
vis[t.x][t.y][t.z] = 1;
while(s < e)
{
t = q[s++] ;
if(map[t.x][t.y]=='^' && t.ans < T)//表示找到出口,直接输出步数,返回结果即可
{
printf("%d\n", t.ans);
return ;
}
for(int i = 0;i < 4;i++)//否则循环4个方向进行搜索
{
f.x = t.x + mv[i][0];
f.y = t.y + mv[i][1];
f.z = t.z;
if(f.x >= 0 && f.x < n && f.y >= 0 && f.y < m && vis[f.x][f.y][f.z]==0)
{
if(map[f.x][f.y]=='@' || map[f.x][f.y]=='.' || map[f.x][f.y]=='^')
{
f.ans = t.ans + 1;
q[e++] = f ;
vis[f.x][f.y][f.z] = 1 ;
}
else if('a' <= map[f.x][f.y] && map[f.x][f.y] <='j' )
{
int sum = 0,kk,xx;
f.ans = t.ans + 1;
xx = f.z;
kk = map[f.x][f.y] - 'a' + 1;
for(int ll = 0;ll<kk;ll++)
{
sum = xx % 2;
xx /= 2;
}
if(sum==0)
f.z += pow(2,(map[f.x][f.y]-'a'));
vis[f.x][f.y][f.z] = 1 ;
q[e++] = f ;
}
else if('A' <= map[f.x][f.y] && map[f.x][f.y] <='J' )
{
int sum = 0,kk,xx;
f.ans = t.ans + 1;
xx = f.z;
kk = map[f.x][f.y] - 'A' + 1;
for(int ll = 0;ll<kk;ll++)
{
sum = xx % 2;
xx /= 2;
}
if(sum==1)
{
f.ans = t.ans + 1;
q[e++] = f ;
vis[f.x][f.y][f.z] = 1 ;
}
}
}
}
}
printf("-1\n");
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&T))
{
int x,y;
memset(vis,0,sizeof(vis));//清空数组
for(int i = 0 ; i < n ; i++)
{
scanf("%s%*c", map[i]);
}
for(int i = 0;i<n;i++)
{
for(int j = 0;j < m;j++)
{
if(map[i][j]=='@')
{
x = i;
y = j;
break;
}
}
}
BFS(x,y);
}
return 0;
}