Description
You are working for an amusement park as an operator of an obakeyashiki, or a haunted house, in which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corridors. One morning, you found that the ghosts are not in the positions where they are supposed to be. Ah, yesterday was Halloween. Believe or not, paranormal spirits have moved them around the corridors in the night. You have to move them into their right positions before guests come. Your manager is eager to know how long it takes to restore the ghosts.
In this problem, you are asked to write a program that, given a floor map of a house, finds the smallest number of steps to move all ghosts to the positions where they are supposed to be.
A floor consists of a matrix of square cells. A cell is either a wall cell where ghosts cannot move into or a corridor cell where they can.
At each step, you can move any number of ghosts simultaneously. Every ghost can either stay in the current cell, or move to one of the corridor cells in its 4-neighborhood (i.e. immediately left, right, up or down), if the ghosts satisfy the following conditions:
For example, suppose ghosts are located as shown in the following (partial) map, where a sharp sign (`#') represents a wall cell and `a', `b', and `c' ghosts.
#### ab# #c## ####
The following four maps show the only possible positions of the ghosts after one step.
#### #### #### #### ab# a b# acb# ab # #c## #c## # ## #c## #### #### #### ####
The input consists of at most 10 datasets, each of which represents a floor map of a house. The format of a dataset is as follows.
w | h | n | |
c11 | c12 | ... | c1w |
c21 | c22 | ... | c2w |
ch1 | ch2 | ... | chw |
w , h and n in the first line are integers, separated by a space. w and h are the floor width and height of the house, respectively. n is the number of ghosts. They satisfy the following constraints.
Subsequent h lines of w characters are the floor map. Each of cij is either:
In each map, each of the first n letters from a and the first n letters from A appears once and only once. Outermost cells of a map are walls; i.e. all characters of the first and last lines are sharps; and the first and last characters on each line are also sharps. All corridor cells in a map are connected; i.e. given a corridor cell, you can reach any other corridor cell by following corridor cells in the 4-neighborhoods. Similarly, all wall cells are connected. Any 2×2 area on any map has at least one sharp. You can assume that every map has a sequence of moves of ghosts that restores all ghosts to the positions where they are supposed to be.
The last dataset is followed by a line containing three zeros separated by a space.
For each dataset in the input, one line containing the smallest number of steps to restore ghosts into the positions where they are supposed to be should be output. An output line should not contain extra characters such as spaces.
5 5 2 ##### #A#B# # # #b#a# ##### 16 4 3 ################ ## ########## ## # ABCcba # ################ 16 16 3 ################ ### ## # ## ## # ## # c# # ## ########b# # ## # # # # # # ## # # ## ## a# # # # # ### ## #### ## # ## # # # # # ##### # ## ## #### #B# # # ## C# # ### # # # ####### # # ###### A## # # # ## ################ 0 0 0
7 36 77
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #include<cmath> using namespace std; const int MAXM=1500; char mmp[20][20]; int mpp[20][20],u[MAXM],v[MAXM],next[MAXM],first[256];bool done[(1<<25)]; int vcnt,ecnt,A,B,C,a,b,c;//#define don(a,b,c) done[(a<<16)+(b<<8)+c] struct node { int v[3],w; node(int x=0,int y=0,int z=0,int ww=0) { v[0]=x;v[1]=y;v[2]=z;w=ww; } }; int bfs() { int i,j,k; queue<node>q; memset(done,0,sizeof done); q.push(node(a,b,c,0)); done[(a<<16)+(b<<8)+c]=1; while(!q.empty()) { node e=q.front();q.pop(); for(i=first[e.v[0]];i!=-1;i=next[i]) for(j=first[e.v[1]];j!=-1;j=next[j]) for(k=first[e.v[2]];k!=-1;k=next[k]) { if(v[i]==v[j]||v[i]==v[k]||v[j]==v[k]||done[(v[i]<<16)+(v[j]<<8)+v[k]])continue;//去掉移动后位置冲突的 if(v[i]==e.v[1]&&v[j]==e.v[0])continue;//前后交换位置肯定相撞了 if(v[i]==e.v[2]&&v[k]==e.v[0])continue; if(v[j]==e.v[2]&&v[k]==e.v[1])continue; if(v[i]==A&&v[j]==B&&v[k]==C)return e.w+1; q.push(node(v[i],v[j],v[k],e.w+1)); done[(v[i]<<16)+(v[j]<<8)+v[k]]=1; //printf("%d %d %d-->%d %d %d %d\n",e.v[0],e.v[1],e.v[2],v[i],v[j],v[k],e.w); } } return -1; } void add_(int a,int b) { u[ecnt]=a; v[ecnt]=b; next[ecnt]=first[a]; first[a]=ecnt++; } int main() { int n,m,i,j,human;char s[2]; while(~scanf("%d%d%d",&m,&n,&human)) { gets(s); if(!m&&!n&&!human)break; for(i=0;i<n;i++) gets(mmp[i]); vcnt=0;ecnt=0; memset(first,-1,sizeof first); memset(next,-1,sizeof next); memset(mpp,0,sizeof mpp); for(i=0;i<n;i++) for(j=0;j<m;j++) if(mmp[i][j]!='#') { vcnt++; if(mmp[i][j]=='A')A=vcnt; if(mmp[i][j]=='B')B=vcnt; if(mmp[i][j]=='C')C=vcnt; if(mmp[i][j]=='a')a=vcnt; if(mmp[i][j]=='b')b=vcnt; if(mmp[i][j]=='c')c=vcnt; mpp[i][j]=vcnt; if(i&&mpp[i-1][j])add_(mpp[i-1][j],vcnt),add_(vcnt,mpp[i-1][j]); if(j&&mpp[i][j-1])add_(mpp[i][j-1],vcnt),add_(vcnt,mpp[i][j-1]); add_(vcnt,vcnt);//a,b,c可以选择不动 } if(human==2)//如果人数不为3,将多的人发在其他节点中,而且该结点只能连接自己 { C=c=++vcnt; add_(vcnt,vcnt); } else if(human==1) { C=c=++vcnt; add_(vcnt,vcnt); B=b=++vcnt; add_(vcnt,vcnt); } printf("%d\n",bfs()); } return 0; }