胜利大逃亡(续)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4692 Accepted Submission(s): 1580
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
Author
LL
Source
Recommend
linle
思路:刚开始以为可以拿到钥匙后被抓回去然后又重新开始走,看了别人的发现不是这样。不是这样就简单了。
直接BFS,BFS状态多加一个得到的钥匙
思路:刚开始以为可以拿到钥匙后被抓回去然后又重新开始走,看了别人的发现不是这样。不是这样就简单了。
直接BFS,BFS状态多加一个得到的钥匙
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define S64I1(a) scanf(iform1, &(a))
#define P64I1(a) printf(oform1, (a))
#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int maxn = 20 + 5;
const int moveX[] = {-1, 1, 0, 0};
const int moveY[] = {0, 0, -1, 1};
int n, m, T;
int sx, sy;
int ex, ey;
char G[maxn][maxn];
int vis[maxn][maxn][1024+40];
bool isok(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
class Node {
public :
int x, y, key, step;
Node(int _x=0, int _y=0, int _key=0, int _step=0) {
x = _x;
y = _y;
key = _key;
step = _step;
}
bool isPoint(int x, int y) {
return this->x == x && this->y == y;
}
};
queue<Node> Q;
int bfs() {
memset(vis, 0, sizeof(vis));
while(!Q.empty()) Q.pop();
Q.push(Node(sx, sy, 0, 0));
int ttt = 0;
while(!Q.empty()) {
Node cur = Q.front();
Q.pop();
//if(++ttt <= 20) printf("-->%d %d", cur.x, cur.y);
if(cur.isPoint(ex, ey) && cur.step < T) return cur.step;
for(int i=0; i<4; i++) {
int nx = cur.x + moveX[i];
int ny = cur.y + moveY[i];
int key = cur.key;
if(!isok(nx, ny) || G[nx][ny] == '*') continue;
if(G[nx][ny] >= 'A' && G[nx][ny] <= 'J') {
int t = G[nx][ny] - 'A';
if(!(key & (1<<t))) continue;
}
if(G[nx][ny] >= 'a' && G[nx][ny] <= 'j') key = key | (1<<(G[nx][ny]-'a'));
if(vis[nx][ny][key]) continue;
Q.push(Node(nx, ny, key, cur.step+1));
vis[nx][ny][key] = 1;
}
}
return -1;
}
int main() {
while(scanf("%d%d%d", &n, &m, &T) != EOF) {
for(int i=0; i<n; i++) {
char s[maxn];
scanf("%s", s);
for(int j=0; j<m; j++) {
G[i][j] = s[j];
if(G[i][j] == '@') {
sx = i;
sy = j;
} else if(G[i][j] == '^') {
ex = i;
ey = j;
}
}
}
int ans = bfs();
printf("%d\n", ans);
}
return 0;
}