将原来的地图转化为一个图,省内存,省时间
第一份代码是刘汝佳代码仓库里的,我加了点注释
第二份是看了代码之后自己模仿的写的,照抄都WA了好多次,太不仔细了,难受
// UVa1601 The Morning after Halloween
// Rujia Liu
// This code implements the simpliest yet efficient-enough algorithm I'm aware of
// Readers are encouraged to experiment on other algorithms (especially for better efficiency!)
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;
const int maxs = 20;
const int maxn = 150; // 75% cells plus 2 fake nodes
const int dx[]={1,-1,0,0,0}; // 4 moves, plus "no move"
const int dy[]={0,0,1,-1,0};
//因为编号最大为150,为了省时间没看题,他说150就150吧,但我感觉不是啊
//因为2的8次方减一为255,所以任何幽灵的位置编号都可以用8位二进制数表示
//总共3个幽灵,所以24位二进制就可以表示了,所以就有了下面的左移运算
inline int ID(int a, int b, int c) {
return (a<<16)|(b<<8)|c;
}
int s[3], t[3]; // starting/ending position of each ghost
int deg[maxn], G[maxn][5]; //deg数组存储每个节点的出度,G数组存储边
inline bool conflict(int a, int b, int a2, int b2) {
return a2 == b2 || (a2 == b && b2 == a);
}
int d[maxn][maxn][maxn]; // distance from starting state
int bfs() {
queue<int> q;
memset(d, -1, sizeof(d));
//
q.push(ID(s[0], s[1], s[2])); // 存储当前状态,使用散列函数将三个数压缩成一个数
d[s[0]][s[1]][s[2]] = 0; //初始位置标记
while(!q.empty()) {
int u = q.front(); q.pop();
int a = (u>>16)&0xff, b = (u>>8)&0xff, c = u&0xff; //提取位置
if(a == t[0] && b == t[1] && c == t[2]) return d[a][b][c]; // solution found
for(int i = 0; i < deg[a]; i++) {
int a2 = G[a][i];
for(int j = 0; j < deg[b]; j++) {
int b2 = G[b][j];
if(conflict(a, b, a2, b2)) continue; //判断幽灵0与幽灵1是否走在一起或者对换了位置
for(int k = 0; k < deg[c]; k++) {
int c2 = G[c][k];
if(conflict(a, c, a2, c2)) continue; //同上
if(conflict(b, c, b2, c2)) continue; //同上
if(d[a2][b2][c2] != -1) continue; //走过的不走
d[a2][b2][c2] = d[a][b][c]+1; //更新步数
q.push(ID(a2, b2, c2));
}
}
}
}
return -1;
}
int main() {
int w, h, n;
while(scanf("%d%d%d\n", &w, &h, &n) == 3 && n) {
char maze[20][20];
for(int i = 0; i < h; i++)
fgets(maze[i], 20, stdin);
// extract empty cells
int cnt, x[maxn], y[maxn], id[maxs][maxs]; // cnt is the number of empty cells
cnt = 0;
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
if(maze[i][j] != '#') {
x[cnt] = i; y[cnt] = j; id[i][j] = cnt; //给空格子和小写字母大写字母所在位置编号,
if(islower(maze[i][j])) s[maze[i][j] - 'a'] = cnt; //
else if(isupper(maze[i][j])) t[maze[i][j] - 'A'] = cnt;//s,t数组分别存储小写字母编号,大写字母编号,相当于存了位置
cnt++;
}
// build a graph of empty cells
for(int i = 0; i < cnt; i++) {
deg[i] = 0;
for(int dir = 0; dir < 5; dir++) {
int nx = x[i]+dx[dir], ny = y[i]+dy[dir];
// "Outermost cells of a map are walls" means we don't need to check out-of-bound
if(maze[nx][ny] != '#') G[i][deg[i]++] = id[nx][ny]; //说明这个位置幽灵可以往坐标为nx,ny的位置移动
} //应该判断下是否超出边界,但是依旧可以过,可能因为我没读题吧
}
// add fakes nodes so that in each case we have 3 ghosts. this makes the code shorter
if(n <= 2) { deg[cnt] = 1; G[cnt][0] = cnt; s[2] = t[2] = cnt++; }
if(n <= 1) { deg[cnt] = 1; G[cnt][0] = cnt; s[1] = t[1] = cnt++; }
printf("%d\n", bfs());
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#include<iostream>
#include<string>
#pragma warning(disable:4996)
using namespace std;
const int MAXN = 20;
const int MAXS = 150;
int dir[5][2] = { {1,0},{0,1},{-1,0},{0,-1},{0,0} };
string map[MAXN];
int s[3], e[3];
int deg[MAXS], G[MAXS][5];
int dist[MAXS][MAXS][MAXS];
int myhash(int a, int b, int c)
{
return (a << 16) | (b << 8) | c;
}
inline bool isconflict(int a, int b,int c,int d)
{
return b == d || (a == d && b == c);
}
int bfs()
{
memset(dist, -1, sizeof(dist));
queue<int> q;
q.push(myhash(s[0], s[1], s[2]));
dist[s[0]][s[1]][s[2]] = 0;
while (!q.empty())
{
int u = q.front(); q.pop();
int a = (u >> 16) & 0xff, b = (u >> 8) & 0xff, c = u & 0xff;
if (a == e[0] && b == e[1] && c == e[2]) return dist[a][b][c];
for (int i = 0; i < deg[a]; i++) {
for (int j = 0; j < deg[b]; j++) {
if (isconflict(a, G[a][i], b, G[b][j])) continue;
for (int k = 0; k < deg[c]; k++) {
if (isconflict(a, G[a][i], c, G[c][k])) continue;
if (isconflict(b, G[b][j], c, G[c][k])) continue;
if (dist[G[a][i]][G[b][j]][G[c][k]] != -1) continue;
q.push(myhash(G[a][i], G[b][j], G[c][k]));
dist[G[a][i]][G[b][j]][G[c][k]] = dist[a][b][c] + 1;
}
}
}
}
return -1;
}
int main()
{
int row, col, n;
while (scanf("%d%d%d", &col, &row, &n) == 3 && n)
{
getline(cin, map[0]);
for (int i = 0; i < row; i++)
getline(cin, map[i]); //个人喜欢用getline函数,可以接受上面输入的空行,同时也可以消去enter
//刘汝佳采用x,y数组存储位置
//但数学中x一般表示横轴,也就是列,而y则表示行,而程序中的数组是先行后列,与坐标表示相反
//所以坐标表示尽量不要用,最好采用与数组对应得先行后列的表示法
//r是ROW的简写,c是COL的简写
int cnt = 0, r[MAXS], c[MAXS], id[MAXN][MAXN];
for(int i=0;i<row;i++)
for (int j = 0; j < col; j++) {
if (map[i][j] != '#') {
r[cnt] = i; c[cnt] = j; id[i][j] = cnt;
if (islower(map[i][j])) s[map[i][j] - 'a'] = cnt;
else if (isupper(map[i][j])) e[map[i][j] - 'A'] = cnt;
cnt++;
}
}
for (int i = 0; i < cnt; i++) {
deg[i] = 0;
int x = r[i], y = c[i]; //这里简单,可以用一下,就当普通变量了
for (int d = 0; d < 5; d++) {
int dx = x + dir[d][0], dy = y + dir[d][1];
if (dx >= 0 && dx < row&&dy >= 0 && dy < col&&map[dx][dy] != '#') {
G[i][deg[i]++] = id[dx][dy];
}
}
}
if (n <= 2) { deg[cnt] = 1; G[cnt][0] = cnt; s[2] = e[2] = cnt++; }
if (n <= 1) { deg[cnt] = 1; G[cnt][0] = cnt; s[1] = e[1] = cnt++; }
printf("%d\n", bfs());
}
}