现场没敲出来,回来写过了样例不知道能不能ac
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
#include <queue>
#define maxn 18
#define eps (1e-7)
typedef long long ll;
int n, m, k;
int wall[55][55][55][55];
int key[55][55][12];
int dx[] = {0,0,-1,1}, dy[] = {1,-1,0,0}; //UDLR
bool Hash[55][55][1030];
using namespace std ;
struct node {
int x, y, key, st;
node(int x=0, int y=0, int key=0, int st=0) : x(x), y(y), key(key), st(st) {}
bool ok() {
if (x > 0 && x <= n && y > 0 && y <= m) return true;
return false;
}
};
queue<node> Q;
int bfs() {
while (!Q.empty()) {
node tmp = Q.front();
Q.pop();
//system("pause");
if (tmp.x == n && tmp.y == m) {
return tmp.st;
}
for (int i = 0; i < 4; ++ i) {
int nx = tmp.x + dx[i], ny = tmp.y + dy[i];
if (node(nx,ny,0,0).ok()) {
if (wall[tmp.x][tmp.y][nx][ny] == -1) { //没有门
if (key[nx][ny][0]) { //这个地方有钥匙
int nkey = tmp.key;
for (int j = 1; j <= 10; ++ j) if (key[nx][ny][j]) { //把所有钥匙全捡了
nkey |= (1 << (j-1));
}
if (!Hash[nx][ny][nkey]) {
Hash[nx][ny][nkey] = 1;
Q.push(node(nx,ny,nkey,tmp.st+1));
}
}
else { //这个地方没钥匙,走过去就行了
if (!Hash[nx][ny][tmp.key]) {
Hash[nx][ny][tmp.key] = 1;
Q.push(node(nx,ny,tmp.key,tmp.st+1));
}
}
}
else { //有门当着
int nkey = wall[tmp.x][tmp.y][nx][ny];
if ((tmp.key >> (nkey-1)) & 1) { //有钥匙能开门
if (!Hash[nx][ny][tmp.key]) {
Hash[nx][ny][tmp.key] = 1;
Q.push(node(nx,ny,tmp.key,tmp.st+1));
}
}
}
}
}
}
return -1;
}
int main() {
while (cin >> n >> m >> k) {
memset(wall, -1, sizeof(wall));
memset(key, 0, sizeof(key));
memset(Hash, 0, sizeof(Hash));
int k;
cin >> k;
while (k --) {
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
wall[a][b][c][d] = e;
wall[c][d][a][b] = e;
}
cin >> k;
while (k --) {
int a, b, c;
cin >> a >> b >> c;
key[a][b][0] ++;
key[a][b][c] ++;
}
while (!Q.empty()) Q.pop();
Q.push(node(1,1,0,0));
cout << bfs() << endl;
}
}