题目

题目

 


 

分析

双向bfs,对着书打的,我还调了好久。

 


 

代码

#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std; const int maxs=20,maxn=150;
const int dx[]={1,-1,0,0,0},dy[]={0,0,1,-1,0};
int s[3],t[3];
int deg[maxn],G[maxn][5];
int dis[maxn][maxn][maxn],dis_back[maxn][maxn][maxn];
int id[maxn][maxn]; inline int ID(int a,int b,int c)
{
return (a<<16)|(b<<8)|c;
} inline bool conflict(int a,int b,int a2,int b2)
{
return a2==b2 || (a2==b && b2==a);
} int bfs(queue<int>& q,int d[maxn][maxn][maxn])
{
int u=q.front(); q.pop();
int a = (u>>16)&0xff, b = (u>>8)&0xff, c = u&0xff;
if(dis[a][b][c]!=-1 && dis_back[a][b][c]!=-1) return dis[a][b][c]+dis_back[a][b][c];
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;
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 solve()
{
queue<int> q;
q.push(ID(s[0],s[1],s[2]));
memset(dis,-1,sizeof(dis));
dis[s[0]][s[1]][s[2]]=0; queue<int> q_back;
q_back.push(ID(t[0],t[1],t[2]));
memset(dis_back,-1,sizeof(dis_back));
dis_back[t[0]][t[1]][t[2]]=0; int ans,t=0;
while(1)
{
t++;
ans=bfs(q,dis);
if(ans!=-1) return ans;
ans=bfs(q_back,dis_back);
if(ans!=-1) return ans;
} return -1;
}
int main()
{
int w,h,n;
while(scanf("%d%d%d",&w,&h,&n)==3 && n)
{
getchar();
char maze[20][20];
for(int i=0;i<h;i++) fgets(maze[i],20,stdin); int cnt=0,x[maxn],y[maxn],id[maxs][maxs];
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
{
char ch=maze[i][j];
if(ch!='#')
{
x[cnt]=i; y[cnt]=j;
id[i][j]=cnt;
if(islower(ch)) s[ch-'a']=cnt;
else if(isupper(ch)) t[ch-'A']=cnt;
cnt++;
}
} for(int i=0;i<cnt;i++)
{
deg[i]=0;
for(int dir=0;dir<5;dir++)
{
int nx=x[i]+dx[dir];
int ny=y[i]+dy[dir];
if(maze[nx][ny]!='#')
G[i][deg[i]++]=id[nx][ny];
}
} 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",solve());
}
return 0;
}

【UVa】1601 The Morning after Halloween(双向bfs)的更多相关文章

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

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

  2. UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)

    题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...

  3. UVA 1601 The Morning after Halloween

    题意: 给出一个最大为16×16的迷宫图和至多3个ghost的起始位置和目标位置,求最少经过几轮移动可以使三个ghost都到达目标位置.每轮移动中,每个ghost可以走一步,也可以原地不动,需要注意的 ...

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

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

  5. UVA 1601 双向BFS

    但是我们还不是很清楚每一次的状态怎么储存?我们可以用一个结构体,将每次的位置存起来,但是这个程序中用了一个更好的储存方法:我们知道最大的格数是16*16个,也就是256个,那么我们转换为二进制表示就是 ...

  6. <<操作,&0xff以及|的巧妙运用(以POJ3523---The Morning after Halloween(UVa 1601)为例)

    <<表示左移,如a<<1表示将a的二进制左移一位,加一个0,&0xff表示取最后8个字节,如a&0xff表示取a表示的二进制中最后8个数字组成一个新的二进制数, ...

  7. UVA1601-The Morning after Halloween(双向BFS)

    Problem UVA1601-The Morning after Halloween Accept: 289 Submit: 3136 Time Limit: 12000 mSec  Problem ...

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

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

  9. UVA - 11624 Fire! 双向BFS追击问题

    Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...

随机推荐

  1. pgpool安装配置整理

    安装PostgreSQL并配置三节点流复制环境,就不仔细说了,大致步骤如下: 1.下载源码 2.解压安装,如果在./configure --prefix=/usr/pgsql-10执行时提示要--wi ...

  2. js获取来源网址

    举例: 1. a.html文件内容如下: <a href="b.html">浏览b.html </a> 2. b.html文件中的内容如下: <bod ...

  3. pcm ulaw alaw转换

    static byte ALawCompressTable[] = { 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5 ...

  4. Java多线程编程实战指南(核心篇)读书笔记(四)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76690961冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  5. [置顶] Android 关于SP读取与存储正确打开方式?

    一.存储方式分类:SharedPreferences存储 二.SharedPreferences存储 1.特点 ①存储单一数据,例如数值,字符串,布尔 ②文件:/date/date/包名/shared ...

  6. angularjs 阻止浏览器自带的回退

    $scope.$on('$locationChangeStart', function(e) { if(!tfOrder && comm.getStorage('orederlistL ...

  7. matlab 破解教程密钥

    http://wenku.baidu.com/link?url=YW1f0XP1VRcOq8YtwsIv-m0vMS3YuFwY7H617koFr9zM6I86P-KpCRo1tDewI81eBiXR ...

  8. Ubuntu和win双系统删除ubuntu开机出错

    Ubuntu和win双系统删除ubuntu开机出错问题. 报错error:unknown filesystem. grub rescue>_ 很简单: 进入pe 打开diskgenius 选择你 ...

  9. JSP学习(五)JSP标签

    JSP标签 jsp的常用标签有: <jsp:include>标签 <jsp:forward>标签 <jsp:param>标签 <jsp:include> ...

  10. ubuntu 部署Django

    1, 安装python包管理工具easy_install. sudo apt-get install python-setuptools 2,安装Django. sudo easy_install & ...