题解:这一题收获很大,参考了刘汝佳的代码。很多细节处理的都很不错。我用的是单向BFS。注释在代码里。花一点时间细心的看,还是能看懂的。
代码:
#include <bits/stdc++.h>
#define ft first
#define sd second
using namespace std;
typedef pair<int,int>pii;
int const N = 20;
int const M = 256;
int const GH = 5;
int w,h,n,cnt;
int st[GH],en[GH],id[N][N],d[M][M][M];
vector<int>G[M];
int dir[5][2] = {0,1,1,0,0,-1,-1,0,0,0}; //(0,0)表示到一个位置
pii space[M]; //记录空格的坐标
char ch,mp[N][N];
int code(int x,int y,int z){ //三只鬼的位置进行状态压缩
return (x<<16) | (y<<8) | z;
}
bool ok(int x,int y,int xx,int yy){ //处理冲突
if(xx == yy || (yy == x && xx == y)) return false; //如果到一个地方,或者交换位置
else return true;
}
int bfs(){
memset(d,-1,sizeof(d));
queue<int>q;
q.push(code(st[0],st[1],st[2]));
d[st[0]][st[1]][st[2]] = 0;
while(!q.empty()){
int p = q.front(); q.pop();
int x = (p>>16)&0xff, y = (p>>8)&0xff, z = p&0xff; //0xff表示低位的8个1
if(x == en[0] && y == en[1] && z == en[2]) return d[x][y][z]; //如果到达终点
for(int i=0;i<G[x].size();i++){ //枚举空格
int u = G[x][i];
for(int j=0;j<G[y].size();j++){
int v = G[y][j];
if(!ok(x,y,u,v)) continue;
for(int k=0;k<G[z].size();k++){
int t = G[z][k];
if(!ok(x,z,u,t) || !ok(y,z,v,t)) continue;
if(d[u][v][t] != -1) continue;
d[u][v][t] = d[x][y][z] + 1;
q.push(code(u,v,t));
}
}
}
}
return -1;
}
int main(){
while(~scanf("%d%d%d\n",&w,&h,&n)){ //注意这里有一个'\n',因为fgets会读入换行
if(!w && !h && !n) break;
for(int i=0;i<h;i++)
fgets(mp[i],20,stdin); //会读入换行
cnt = 0; // 记录空格的数量
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if(mp[i][j] != '#'){
space[cnt] = pii(i,j); id[i][j] = cnt; //id把坐标映射到空格的编号
if(islower(mp[i][j])) st[mp[i][j]-'a'] = cnt;
else if(isupper(mp[i][j])) en[mp[i][j]-'A'] = cnt;
cnt++;
}
for(int i=0;i<cnt;i++){ //枚举空格
G[i].clear();
for(int j=0;j<5;j++){
int nx = space[i].ft + dir[j][0], ny = space[i].sd + dir[j][1];
if(mp[nx][ny] == '#') continue;
G[i].push_back(id[nx][ny]); //记录每个空格四周相邻的空格的编号
}
} //下面的代码是为了统一有三只鬼,bfs判断比较容易,但是鬼只能在自己的地方,不影响后边的处理
if(n<=2){G[cnt].clear(); G[cnt].push_back(cnt); st[2] = en[2] = cnt++;}
if(n<=1){G[cnt].clear(); G[cnt].push_back(cnt); st[1] = en[1] = cnt++;}
printf("%d\n",bfs());
}
return 0;
}#include <bits/stdc++.h>
#define ft first
#define sd second
using namespace std;
typedef pair<int,int>pii;
int const N = 20;
int const M = 256;
int const GH = 5;
int w,h,n,cnt;
int st[GH],en[GH],id[N][N],d[M][M][M];
vector<int>G[M];
int dir[5][2] = {0,1,1,0,0,-1,-1,0,0,0}; //(0,0)表示到一个位置
pii space[M]; //记录空格的坐标
char ch,mp[N][N];
int code(int x,int y,int z){ //三只鬼的位置进行状态压缩
return (x<<16) | (y<<8) | z;
}
bool ok(int x,int y,int xx,int yy){ //处理冲突
if(xx == yy || (yy == x && xx == y)) return false; //如果到一个地方,或者交换位置
else return true;
}
int bfs(){
memset(d,-1,sizeof(d));
queue<int>q;
q.push(code(st[0],st[1],st[2]));
d[st[0]][st[1]][st[2]] = 0;
while(!q.empty()){
int p = q.front(); q.pop();
int x = (p>>16)&0xff, y = (p>>8)&0xff, z = p&0xff; //0xff表示低位的8个1
if(x == en[0] && y == en[1] && z == en[2]) return d[x][y][z]; //如果到达终点
for(int i=0;i<G[x].size();i++){ //枚举空格
int u = G[x][i];
for(int j=0;j<G[y].size();j++){
int v = G[y][j];
if(!ok(x,y,u,v)) continue;
for(int k=0;k<G[z].size();k++){
int t = G[z][k];
if(!ok(x,z,u,t) || !ok(y,z,v,t)) continue;
if(d[u][v][t] != -1) continue;
d[u][v][t] = d[x][y][z] + 1;
q.push(code(u,v,t));
}
}
}
}
return -1;
}
int main(){
while(~scanf("%d%d%d\n",&w,&h,&n)){ //注意这里有一个'\n',因为fgets会读入换行
if(!w && !h && !n) break;
for(int i=0;i<h;i++)
fgets(mp[i],20,stdin); //会读入换行
cnt = 0; // 记录空格的数量
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if(mp[i][j] != '#'){
space[cnt] = pii(i,j); id[i][j] = cnt; //id把坐标映射到空格的编号
if(islower(mp[i][j])) st[mp[i][j]-'a'] = cnt;
else if(isupper(mp[i][j])) en[mp[i][j]-'A'] = cnt;
cnt++;
}
for(int i=0;i<cnt;i++){ //枚举空格
G[i].clear();
for(int j=0;j<5;j++){
int nx = space[i].ft + dir[j][0], ny = space[i].sd + dir[j][1];
if(mp[nx][ny] == '#') continue;
G[i].push_back(id[nx][ny]); //记录每个空格四周相邻的空格的编号
}
} //下面的代码是为了统一有三只鬼,bfs判断比较容易,但是鬼只能在自己的地方,不影响后边的处理
if(n<=2){G[cnt].clear(); G[cnt].push_back(cnt); st[2] = en[2] = cnt++;}
if(n<=1){G[cnt].clear(); G[cnt].push_back(cnt); st[1] = en[1] = cnt++;}
printf("%d\n",bfs());
}
return 0;
}