[POJ3523]The Morning after Halloween
Time Limit: 8000MS   Memory Limit: 65536K
Total Submissions: 2395   Accepted: 543

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:

  1. No more than one ghost occupies one position at the end of the step.

  2. No pair of ghosts exchange their positions one another in the step.

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#
#c##
####
 
####
a b#
#c##
####
 
####
acb#
# ##
####
 
####
ab #
#c##
####

Input

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

wh 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.

4 ≤ w ≤ 16, 4 ≤ h ≤ 16, 1 ≤ n ≤ 3

Subsequent h lines of w characters are the floor map. Each of cij is either:

  • a ‘#’ representing a wall cell,

  • a lowercase letter representing a corridor cell which is the initial position of a ghost,

  • an uppercase letter representing a corridor cell which is the position where the ghost corresponding to its lowercase letter is supposed to be, or

  • a space representing a corridor cell that is none of the above.

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.

Output

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.

Sample Input

5 5 2
#####
#A#B#
# #
#b#a#
#####
16 4 3
################
## ########## ##
# ABCcba #
################
16 16 3
################
### ## # ##
## # ## # c#
# ## ########b#
# ## # # # #
# # ## # # ##
## a# # # # #
### ## #### ## #
## # # # #
# ##### # ## ##
#### #B# # #
## C# # ###
# # # ####### #
# ###### A## #
# # ##
################
0 0 0

Sample Output

7
36
77

Source

 
题目大意:详见《算法入门经典》第7章7-9 万圣节的早晨(P205)
      就是有一个M*N的地图,有K个字母,小写字母要走到大写字母去,可以同时移动,但移动后不能重叠或交换,每2*2的矩形中保证至少会有2个是障碍,问最小步数。
试题分析:这道题目非常好,一道费劲的BFS,但是很值得写的。写死我了
      vis[17][17][17][17][17][17],普通BFS是最显然的方法,但会炸的很惨。。。
      那么有终止和起始,我们可以考虑用双向BFS。
      但这样写普通的双向BFS会炸空间,所以还是要优化。
      如何优化呢?yy了一个想法:将可以走的地方全标号(按顺序)
      那么我们只需要开vis[151][151][151]就足以标记了。
      然后有一点要注意一下,就是双向BFS第一个搜出来的不一定是最优解。
      要将同层的先搜完再确定答案。
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm> using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int INF = 9999999;
const int MAXN = 100000;
int N,M,K;
char MP[17][17];
int Ax,Ay; int Bx,By; int Cx,Cy;
int ax,ay; int bx,by; int cx,cy;
int vis[151][151][151];
int visB[151][151][151];
struct data{
int ax,ay;
int bx,by;
int cx,cy;
int stp;
}que[1000001];
struct data2{
int Ax,Ay;
int Bx,By;
int Cx,Cy;
int stp;
}que2[1000001];
int dis[6][2]={{0,1},{1,0},{0,-1},{-1,0},{0,0}}; int ls=1,rs=0;
int lb=1,rb=0;
int dit[21][21];
void in(int axx,int ayy,int bxx,int byy,int cxx,int cyy,int sp,bool fg){
if(!fg){
vis[dit[axx][ayy]][dit[bxx][byy]][dit[cxx][cyy]]=sp;
que[++rs].ax=axx;
que[rs].ay=ayy;
que[rs].bx=bxx;
que[rs].by=byy;
que[rs].cx=cxx;
que[rs].cy=cyy;
que[rs].stp=sp;
}
else{
visB[dit[axx][ayy]][dit[bxx][byy]][dit[cxx][cyy]]=sp;
que2[++rb].Ax=axx;
que2[rb].Ay=ayy;
que2[rb].Bx=bxx;
que2[rb].By=byy;
que2[rb].Cx=cxx;
que2[rb].Cy=cyy;
que2[rb].stp=sp;
}
}
int ans;
bool flag=false; void BFS(){
in(ax,ay,bx,by,cx,cy,0,0);
in(Ax,Ay,Bx,By,Cx,Cy,0,1);
int x1,y1,x2,y2,x3,y3,sp;
int t=0;
while(ls<=rs&&lb<=rb){
if(flag) t++;
if(t>1024) break;
x1=que[ls].ax;
y1=que[ls].ay;
x2=que[ls].bx;
y2=que[ls].by;
x3=que[ls].cx;
y3=que[ls].cy;
sp=que[ls].stp;
//cout<<"S:"<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<<x3<<" "<<y3<<" "<<sp<<endl;
for(int i=0;i<5;i++){
int xx1=x1+dis[i][0];
int yy1=y1+dis[i][1];
if(xx1>N||yy1>M||yy1<1||xx1<1) continue;
if(MP[xx1][yy1]=='#') continue;
if(x2||y2)
for(int j=0;j<5;j++){
int xx2=x2+dis[j][0];
int yy2=y2+dis[j][1];
if(xx2==xx1&&yy1==yy2) continue;
if(xx2>N||yy2>M||xx2<1||yy2<1) continue;
if(xx2==x1&&yy2==y1&&xx1==x2&&yy1==y2) continue;
if(MP[xx2][yy2]=='#') continue;
if(x3||y3)
for(int k=0;k<5;k++){
if(i==4&&j==4&&k==4) continue;
int xx3=x3+dis[k][0];
int yy3=y3+dis[k][1];
if(xx2==x3&&yy2==y3&&xx3==x2&&yy3==y2) continue;
if(xx3==x1&&yy3==y1&&xx1==x3&&yy1==y3) continue;
if((xx3==xx1&&yy3==yy1)||(xx3==xx2&&yy3==yy2)) continue;
if(xx3>N||yy3>M||xx3<1||yy3<1||vis[dit[xx1][yy1]][dit[xx2][yy2]][dit[xx3][yy3]]!=-1) continue;
if(MP[xx3][yy3]=='#') continue;
if(visB[dit[xx1][yy1]][dit[xx2][yy2]][dit[xx3][yy3]]!=-1){
ans=min(visB[dit[xx1][yy1]][dit[xx2][yy2]][dit[xx3][yy3]]+sp+1,ans);
flag=true;// return ;
}
in(xx1,yy1,xx2,yy2,xx3,yy3,sp+1,0);
}
else{
if(vis[dit[xx1][yy1]][dit[xx2][yy2]][dit[x3][y3]]!=-1) continue;
if(i==4&&j==4) continue;
//cout<<"tmp:"<<xx1<<" "<<yy1<<" "<<xx2<<" "<<yy2<<endl;
if(visB[dit[xx1][yy1]][dit[xx2][yy2]][dit[x3][y3]]!=-1){
ans=min(visB[dit[xx1][yy1]][dit[xx2][yy2]][dit[x3][y3]]+sp+1,ans);
flag=true; //return ;
}
in(xx1,yy1,xx2,yy2,x3,y3,sp+1,0);
}
}
else{
if(vis[dit[xx1][yy1]][dit[x2][y2]][dit[x3][y3]]!=-1) continue;
if(i==4) continue;
if(visB[dit[xx1][yy1]][dit[x2][y2]][dit[x3][y3]]!=-1){
ans=min(visB[dit[xx1][yy1]][dit[x2][y2]][dit[x3][y3]]+sp+1,ans);
flag=true; //return ;
}
in(xx1,yy1,x2,y2,x3,y3,sp+1,0);
}
} x1=que2[lb].Ax;
y1=que2[lb].Ay;
x2=que2[lb].Bx;
y2=que2[lb].By;
x3=que2[lb].Cx;
y3=que2[lb].Cy;
sp=que2[lb].stp;
//cout<<"B:"<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" "<<x3<<" "<<y3<<" "<<sp<<endl;
for(int i=0;i<5;i++){
int xx1=x1+dis[i][0];
int yy1=y1+dis[i][1];
if(xx1>N||yy1>M||yy1<1||xx1<1) continue;
if(MP[xx1][yy1]=='#') continue;
if(x2||y2)
for(int j=0;j<5;j++){
int xx2=x2+dis[j][0];
int yy2=y2+dis[j][1];
if(xx2==x1&&yy2==y1&&xx1==x2&&yy1==y2) continue;
if(xx2==xx1&&yy1==yy2) continue;
if(xx2>N||yy2>M||xx2<1||yy2<1) continue;
if(MP[xx2][yy2]=='#') continue;
if(x3||y3)
for(int k=0;k<5;k++){
if(i==4&&j==4&&k==4) continue;
int xx3=x3+dis[k][0];
int yy3=y3+dis[k][1];
if(xx2==x3&&yy2==y3&&xx3==x2&&yy3==y2) continue;
if(xx3==x1&&yy3==y1&&xx1==x3&&yy1==y3) continue;
if((xx3==xx1&&yy3==yy1)||(xx3==xx2&&yy3==yy2)) continue;
if(xx3>N||yy3>M||xx3<1||yy3<1||visB[dit[xx1][yy1]][dit[xx2][yy2]][dit[xx3][yy3]]!=-1) continue;
if(MP[xx3][yy3]=='#') continue;
if(vis[dit[xx1][yy1]][dit[xx2][yy2]][dit[xx3][yy3]]!=-1){
ans=min(vis[dit[xx1][yy1]][dit[xx2][yy2]][dit[xx3][yy3]]+sp+1,ans);
flag=true; //return ;
}
in(xx1,yy1,xx2,yy2,xx3,yy3,sp+1,1);
}
else{
if(visB[dit[xx1][yy1]][dit[xx2][yy2]][dit[x3][y3]]!=-1) continue;
if(i==4&&j==4) continue;
//cout<<"tmp:"<<xx1<<" "<<yy1<<" "<<xx2<<" "<<yy2<<" "<<sp+1<<endl;
if(vis[dit[xx1][yy1]][dit[xx2][yy2]][dit[x3][y3]]!=-1){
ans=min(vis[dit[xx1][yy1]][dit[xx2][yy2]][dit[x3][y3]]+sp+1,ans);
flag=true; //return ;
}
in(xx1,yy1,xx2,yy2,x3,y3,sp+1,1);
}
}
else{
if(visB[dit[xx1][yy1]][dit[x2][y2]][dit[x3][y3]]!=-1) continue;
if(i==4) continue;
if(vis[dit[xx1][yy1]][dit[x2][y2]][dit[x3][y3]]!=-1){
ans=min(vis[dit[xx1][yy1]][dit[x2][y2]][dit[x3][y3]]+sp+1,ans);
flag=true; //return ;
}
in(xx1,yy1,x2,y2,x3,y3,sp+1,1);
}
}
ls++,lb++;
}
} int main(){
//freopen("a.txt","w",stdout);
while(1){
M=read(),N=read(),K=read();
if(!N||!M) break;
Ax=Ay=Bx=By=Cx=Cy=ax=ay=bx=by=cx=cy=0;
ls=1,rs=0;
lb=1,rb=0;
for(int i1=0;i1<=150;i1++)
for(int i2=0;i2<=150;i2++)
for(int i3=0;i3<=150;i3++){
vis[i1][i2][i3]=-1;
visB[i1][i2][i3]=-1;
}
int cnt=0;
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
MP[i][j]=getchar();
if(MP[i][j]=='A') Ax=i,Ay=j;
if(MP[i][j]=='B') Bx=i,By=j;
if(MP[i][j]=='C') Cx=i,Cy=j;
if(MP[i][j]=='a') ax=i,ay=j;
if(MP[i][j]=='b') bx=i,by=j;
if(MP[i][j]=='c') cx=i,cy=j;
if(MP[i][j]!='#'){
dit[i][j]=++cnt;
}
}
getchar();
}
ans=INF; flag=false;
BFS();
cout<<ans<<endl;
}
}

【BFS】The Morning after Halloween的更多相关文章

  1. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  2. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  3. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  4. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  5. 【bfs】献给阿尔吉侬的花束

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  6. 【bfs】迷宫问题

    [题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...

  7. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

  8. 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz

    最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...

  9. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. shell 给未定义的变量设定默认值 ${parameter:-word}

    参考: [ Unix & Linux ] Shell Demo $echo ${JENKINS_VERSION:-2.7.4} 2.7.4 $JENKINS_VERSION=2.99 $ech ...

  2. No 'Access-Control-Allow-Origin' Ajax跨域访问解决方案

    No 'Access-Control-Allow-Origin' header is present on the requested resource. 当使用ajax访问远程服务器时,请求失败,浏 ...

  3. python碎片记录(二)

    1.字典中嵌套字典使用 dict={'a':{1:2,2:3}} print(dict) print(dict['a'][2]) 输出如下: {'a': {1: 2, 2: 3}} 3  2.元组与l ...

  4. Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】

    转自:http://www.th7.cn/system/lin/201606/167750.shtml 我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一 ...

  5. win10出现"win10系统即将过期,请激活"的处理办法

    当打开电脑时,出现"你的win10系统即将过期,请前往激活”的提示.上网查了解决方案,避免以后在出现这样的情况,现将解决步骤整理如下: 1.下载KMSpico激活软件,百度网盘下载链接如下: ...

  6. 解决Ubuntu的错误提示

    如果你是一个Ubuntu用户,也许偶尔甚至经常,遇到这样一个错误提示“System Program problem detected”. Ubuntu有一个内建的实用程序叫做Apport, 当一个程序 ...

  7. Flask 知识总结

    阅读目录 第一篇:Flask基础知识介绍 第二篇:Flask扩展小结 第三篇:Flask 配置文件 第四篇:ORM.MySQL数据库连接池 第五篇:单例模式.蓝图Blueprint 第六篇:SQLAL ...

  8. window下线程同步之(Critical Sections(关键代码段、关键区域、临界区域)

    关键区域(CriticalSection) 临界区是为了确保同一个代码片段在同一时间只能被一个线程访问,与原子锁不同的是临界区是多条指令的锁定,而原子锁仅仅对单条操作指令有效;临界区和原子锁只能控制同 ...

  9. 【JBPM4】EL表达式的使用,实现JAVA与JPDL的交互

    user.java实体类 private String kezhang; private String zhuren; /...完善set get 方法.../ 创建流程实例 //创建流程引擎 Pro ...

  10. Linux下几个命令的技巧

    Ctrl的组合键+a,移动到一行命令的首部+e,移动到一行命令的尾部+左右键,以单词为单位左右移动+u,删除光标之前的所有内容+k,删除光标之后的所有内容Alt+.为引用上一个命令的最后一个参数 还有 ...