Problem UVA1601-The Morning after Halloween

Accept: 289 Submit: 3136

Time Limit: 12000 mSec

 Problem Description

 Input

 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

 

 Sample Ouput

7

36

77

题解:双向BFS经典题目。

抛开双向BFS,这个题的预处理也很值得学习。节点个数太多16*16=256,并且有三个鬼,所以状态总数几乎是256^3,不管是时间还是空间,肯定都要炸,所以就要把状态算的精确一些,省去一些不可能的情况,首先最外层都是'#',这样这个图就变成了最大14*14 = 196,还是有点大,注意题目条件,每2*2中至少有一个障碍,所以这196个点至多有75%是空格,196*0.75 = 147。这就差不多了,由于后面可能有加点的操作,不过最多加2个点,也就是说总数不会超过150,这个数字就比较理想了,时间上和空间上都不错。判重的问题,刚才分析过了,每个鬼能够出现的格子数目不超过150,八位二进制数就能表示,因此三个就可以用24位二进制数表示,和int还有一段距离,所以转换成一个int型整数判重是再好不过的了。将所有空格连接起来用的是类似邻接表的操作,不过lrj没有用vector,可能还是效率原因吧。

之后就是双向BFS的模板了,两个队列,分别入队出发点和终点,vis数组肯定是三维的,只是原来的0、1变成了现在的0、1、2,因为要区分是在哪一边扩展的过程中遍历到的这个点。当一个点在两个队列中都出现了,就说明找到了,返回dis的和,别忘了+1.还有一个需要注意的是双向BFS每次扩展一层而不是扩展一个节点。原因参考下面的链接:

http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx

先来一个单向BFS,680ms

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; const int maxs = ;
const int maxn = ; int n,m,cnt,sum;
int deg[maxn],G[maxn][maxn];
int x[maxn],y[maxn],id[maxn][maxn];
int d[maxn][maxn][maxn];
int s[],t[];
int dx[] = {,,,-,};
int dy[] = {,,-,,}; int find_ID(int a,int b,int c){
return (a<<)|(b<<)|c;
} bool conflict(int a,int b,int a2,int b2){
if((a2==b && a==b2) || a2==b2) return true;
return false;
} int bfs(){
queue<int> que;
d[s[]][s[]][s[]] = ;
que.push(find_ID(s[],s[],s[]));
while(!que.empty()){
int top = que.front();que.pop();
int a = (top>>)&0xff,b = (top>>)&0xff,c = (top&0xff);
if(a==t[] && b==t[] && c==t[]) return d[a][b][c];
for(int i = ;i < deg[a];i++){
int a2 = G[a][i];
for(int j = ;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = ;k < deg[c];k++){
int c2 = G[c][k];
if(d[a2][b2][c2] != -) continue;
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue;
d[a2][b2][c2] = d[a][b][c]+;
que.push(find_ID(a2,b2,c2));
}
}
}
}
return -;
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%d%d%d\n",&m,&n,&sum) && (m||n||sum)){
char gra[maxs][maxs];
cnt = ;
memset(d,-,sizeof(d));
for(int i = ;i < n;i++) fgets(gra[i],maxs,stdin); for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
if(gra[i][j] != '#'){
x[cnt] = i,y[cnt] = j,id[i][j] = cnt;
if(islower(gra[i][j])) s[gra[i][j]-'a'] = cnt;
else if(isupper(gra[i][j])) t[gra[i][j]-'A'] = cnt;
cnt++;
}
}
} for(int i = ;i < cnt;i++){
int X = x[i],Y = y[i];
deg[i] = ;
for(int k = ;k < ;k++){
int xx = X+dx[k],yy = Y+dy[k];
if(gra[xx][yy] != '#') G[i][deg[i]++] = id[xx][yy];
}
} if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
}
if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
} printf("%d\n",bfs());
}
return ;
}

再来一个双向BFS,530ms

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; const int maxs = ;
const int maxn = ; int n,m,cnt,sum;
int deg[maxn],G[maxn][maxn];
int x[maxn],y[maxn],id[maxn][maxn];
int d[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int s[],t[];
int dx[] = {,,,-,};
int dy[] = {,,-,,}; int find_ID(int a,int b,int c){
return (a<<)|(b<<)|c;
} bool conflict(int a,int b,int a2,int b2){
if((a2==b && a==b2) || a2==b2) return true;
return false;
} int bfs(){
queue<int> que,anti_que;
d[s[]][s[]][s[]] = ;
d[t[]][t[]][t[]] = ; que.push(find_ID(s[],s[],s[]));
anti_que.push(find_ID(t[],t[],t[])); vis[s[]][s[]][s[]] = ;
vis[t[]][t[]][t[]] = ; while(!que.empty() && !anti_que.empty()){
int num = que.size(),anti_num = anti_que.size();
while(num--){
int top = que.front();que.pop();
int a = (top>>)&0xff,b = (top>>)&0xff,c = (top&0xff); for(int i = ;i < deg[a];i++){
int a2 = G[a][i];
for(int j = ;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = ;k < deg[c];k++){
int c2 = G[c][k];
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue; if(vis[a2][b2][c2] == ){
d[a2][b2][c2] = d[a][b][c]+;
vis[a2][b2][c2] = ;
que.push(find_ID(a2,b2,c2));
}
else if(vis[a2][b2][c2] == ){
return d[a][b][c]+d[a2][b2][c2]+;
}
}
}
}
} while(anti_num--){
int top = anti_que.front();anti_que.pop();
int a = (top>>)&0xff,b = (top>>)&0xff,c = (top&0xff); for(int i = ;i < deg[a];i++){
int a2 = G[a][i];
for(int j = ;j < deg[b];j++){
int b2 = G[b][j];
if(conflict(a,b,a2,b2)) continue;
for(int k = ;k < deg[c];k++){
int c2 = G[c][k];
if(conflict(a,c,a2,c2)) continue;
if(conflict(b,c,b2,c2)) continue; if(vis[a2][b2][c2] == ){
d[a2][b2][c2] = d[a][b][c]+;
vis[a2][b2][c2] = ;
anti_que.push(find_ID(a2,b2,c2));
}
else if(vis[a2][b2][c2] == ){
return d[a][b][c]+d[a2][b2][c2]+;
}
}
}
}
}
}
return -;
} int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
while(~scanf("%d%d%d\n",&m,&n,&sum) && (m||n||sum)){
char gra[maxs][maxs];
cnt = ;
memset(d,-,sizeof(d));
memset(vis,,sizeof(vis));
for(int i = ;i < n;i++) fgets(gra[i],maxs,stdin); for(int i = ;i < n;i++){
for(int j = ;j < m;j++){
if(gra[i][j] != '#'){
x[cnt] = i,y[cnt] = j,id[i][j] = cnt;
if(islower(gra[i][j])) s[gra[i][j]-'a'] = cnt;
else if(isupper(gra[i][j])) t[gra[i][j]-'A'] = cnt;
cnt++;
}
}
} for(int i = ;i < cnt;i++){
int X = x[i],Y = y[i];
deg[i] = ;
for(int k = ;k < ;k++){
int xx = X+dx[k],yy = Y+dy[k];
if(gra[xx][yy] != '#') G[i][deg[i]++] = id[xx][yy];
}
} if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
}
if(sum <= ){
s[] = t[] = G[cnt][] = cnt;
deg[cnt++] = ;
} printf("%d\n",bfs());
}
return ;
}

UVA1601-The Morning after Halloween(双向BFS)的更多相关文章

  1. UVa1601 - The Morning after Halloween [单向bfs]

    解题思路: 1.注意到2*2方格中必有一个#,那么最多只有192条通道,可以将所有非‘#’的位置提取出来用邻接表的方式建图,通过bfs搜索目标位置. 2.将三个ghost的位置(a,b,c)作为状态量 ...

  2. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  3. UVA-1601 The Morning after Halloween(BFS或双向BFS)

    题目大意:在一张图中,以最少的步数将a,b,c移到对应的A,B,C上去.其中,每个2x2的方格都有障碍并且不能两个小写字母同时占据一个格子. 题目分析:为避免超时,先将图中所有能联通的空格建起一张图, ...

  4. 【UVa】1601 The Morning after Halloween(双向bfs)

    题目 题目     分析 双向bfs,对着书打的,我还调了好久.     代码 #include<cstdio> #include<cstring> #include<c ...

  5. UVA - 1601 The Morning after Halloween (双向BFS&单向BFS)

    题目: w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

  6. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  7. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  8. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  9. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

随机推荐

  1. Java设计模式 - 单例模式详解(下)

    单例模式引发相关整理 关联线程安全 在多线程下,懒汉式会有一定修改.当两个线程在if(null == instance)语句阻塞的时候,可能由两个线程进入创建实例,从而返回了两个对象.对此,我们可以加 ...

  2. CDRAF之Service mesh

    最近翻看一些网上的文章,偶然发现我们的CDRAF其实就是Service mesh的C++版本.不管从架构的理念上,或者功能的支持上面,基本完全符合.发几个简单的文章链接,等有时间的时候,再来详细描述. ...

  3. 使用laravel框架开发接口时ajax post请求报错419

    nginx服务器,使用laravel框架开发后台接口.get请求正常,但是post请求一直报错.H5和APP都不成功,code=419. 解决办法: 找到 VerifyCsrfToken.php文件( ...

  4. OSI 七层,TCP 四层 , TCP 五层模型介绍

    以 TCP 四层模型为例,介绍对应的物理设备 传输层: 四层交换机,四层路由器 网络层: 路由器,三层交换机 数据链路层: 网桥,以太网交换机,网卡 物理层: 中继器,集线器,双绞线 各层功能介绍 物 ...

  5. elementUI vue 编辑中的input的验证残留清除

    当使用编辑的时候, 假如上次的验证没通过, 报红了, 下次再点击编辑的时候还会报红,因此要清除验证残留, 方式有两种: this.$refs["from"].resetFields ...

  6. CSS选择器【记录】

    1.基本选择器 2.组合选择器 3.伪类选择器 4.伪元素选择器 CSS选择器规定了CSS规则会应用到哪些元素上 1.基本选择器 基本选择器:通配选择器.元素选择器.类选择器.ID选择器.属性选择器 ...

  7. apache配置-html碎片shtml格式

    修改SSI 文件 conf–httpd.conf <Directory "D:/Android/Apache2.2/htdocs">  //修改文件目录 # # Pos ...

  8. 【代码笔记】Web-JavaScript-JavaScript 运算符

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  9. 关于js脚本宿主对脚本代码的绑定

    脚本代码绑定,Unity3D是这样做的.为了体现Unity3D的基于对象设计,Unity3D可以为每个对象绑定多个脚本文件,可以是js,可以是cs,也可以是boo. threejs/editor也有与 ...

  10. python 数据驱动ddt使用,需要调用下面的代码,请挨个方法调试,把不用的注释掉

    #!/usr/bin/env/python # -*- coding: utf-8 -*- # @Time : 2018/12/15 15:27 # @Author : ChenAdong # @Em ...