题目描述
然而事情并不是Richard想的那么简单,等他登陆后他才发现,地图里有各种各样的炸弹和关卡。当Richard经过炸弹的时候,他需要花费1分钟的时间拆弹;地图中有关卡对应的通行证,如果Richard拿到了通行证,那么它就可以通过对应的关卡。现在请你重新为Richard规划一条路线,使执行任务的时间最短。
.代表空地
‘#’代表障碍
@代表出发点
*代表任务地点
=代表炸弹
1-4代表通行证
a-d代表关卡
输入
输入第一行为T,即有T组输入数据(1<=T<=10)
每组数据第一行为整数N 10≤N≤100,代表地图的长度和宽度
接下来为N行,每行N个字符,代表地图
输出
输出一个整数,为Richard从起点出发到达终点所需的最短时间。
如果Richard无法到达终点,请输出MISSION FAILED
每组输出占一行
样例输入
5
@….
.1…
…..
=a###
….*
样例输出
8
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define INF 0xfffffff
using namespace std;
typedef struct node{
int x,y,t;
int k,bomb;
}node;
int fmin(int a,int b){return a<b?a:b;}
int dirx[]={0,0,1,0,-1},diry[]={0,1,0,-1,0};
int bombx[10],bomby[10],bombl;
int n,TT;
bool mark[105][105][20][64];
char maze[105][105];
int which_bomb(int x,int y){
for(int i=1;i<=bombl;++i)
if(x==bombx[i] && y==bomby[i]) return i;
}
void bfs(node s){
queue <node> q;
mark[s.x][s.y][s.k][s.bomb]=true;
q.push(s);
while(!q.empty()){
s=q.front();
q.pop();
if(s.t>TT)continue;
if(maze[s.x][s.y]=='*'){
TT =fmin(TT, s.t);
continue ;
}
for(int i=1;i<=4;i++){
node w=s;
w.x+=dirx[i];
w.y+=diry[i];
w.t++;
if(w.x<1 || w.x>n || w.y<1 || w.y>n || maze[w.x][w.y]=='#') continue;
char c=maze[w.x][w.y];
if(c=='='){
int j=which_bomb(w.x,w.y);
if((w.bomb&(1<<j))==0){
w.bomb |= (1<<j);
w.t++;
}
if(!mark[w.x][w.y][w.k][w.bomb]){
q.push(w);
mark[w.x][w.y][w.k][w.bomb]=true;
}
}
else if(isdigit(c)){
w.k |= (1<<(c-'1'));
if(!mark[w.x][w.y][w.k][w.bomb]){
mark[w.x][w.y][w.k][w.bomb]=true;
q.push(w);
}
}
else if(islower(c)){
if((w.k & (1<<(c-'a'))) && (!mark[w.x][w.y][w.k][w.bomb])){
mark[w.x][w.y][w.k][w.bomb]=true;
q.push(w);
}
}
else if(!mark[w.x][w.y][w.k][w.bomb]){
mark[w.x][w.y][w.k][w.bomb]=true;
q.push(w);
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF){
node s;
memset(mark,false,sizeof(mark));
bombl=0;
for(int i=1;i<=n;i++){
scanf("%s",&maze[i][1]);
for(int j=1;j<=n;j++)
if(maze[i][j]=='@') s.x=i,s.y=j;
else if(maze[i][j]=='=') bombx[++bombl]=i,bomby[bombl]=j;
}
TT=INF;
s.k=0;
s.bomb=0;
s.t=0;
bfs(s);
if(TT==INF )printf("MISSION FAILED\n");
else printf("%d\n",TT);
}
return 0;
}