UVA-1604 Cubic Eight-Puzzle (双向BFS+状态压缩+限制搜索层数)
题目大意:立体的八数码问题,一次操作是滚动一次方块,问从初始状态到目标状态的最少滚动次数。
题目分析:这道题已知初始状态和目标状态,且又状态数目庞大,适宜用双向BFS。每个小方块有6种状态,整个大方格有9*6^8个状态。每个小方块用一位6进制数表示即可。
注意:状态转移时要谨慎,否则会出现意想不到的错误;
这道题的末状态有256(2^8)个,如果对搜索层数不加限制,即使双向BFS也会TLE的,当限制正向搜索15层逆向搜索15层至正向搜索27层反向搜索3层时都能AC(我下面贴出的程序是这样的),其中正向搜21层,逆向搜9层时在时间上最高效;
对于这道题,开辟一个恰当大小的标记数组也能使时间降低一大截;
代码如下:
# include<iostream>
# include<cstdio>
# include<cmath>
# include<map>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std; struct Node
{
int t,n,s;
Node(int _t,int _n,int _s):t(_t),n(_n),s(_s){}
bool operator < (const Node &a) const {
return t>a.t;
}
}; map<char,int>mp;
int ss[8]={1,6,36,216,1296,7776,46656,279936};
int goal[9],path[9],vis[9][1679616],dist[9][1679616];
int d[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
int p[6][4]={
{2,2,5,5},
{4,4,3,3},
{0,0,4,4},
{5,5,1,1},
{1,1,2,2},
{3,3,0,0},
};
priority_queue<Node>q[2]; void dfs(int u,int gp)
{
if(u==9){
int sta=0,cnt=0;
for(int i=0;i<9;++i)
if(path[i]!=-1){
sta=sta+ss[7-cnt]*path[i];
++cnt;
}
dist[gp][sta]=0;
vis[gp][sta]=1;
q[1].push(Node(0,gp,sta));
return ;
}
if(goal[u]==1){
path[u]=0;
dfs(u+1,gp);
path[u]=1;
dfs(u+1,gp);
}else if(goal[u]==2){
path[u]=2;
dfs(u+1,gp);
path[u]=3;
dfs(u+1,gp);
}else if(goal[u]==3){
path[u]=4;
dfs(u+1,gp);
path[u]=5;
dfs(u+1,gp);
}else{
path[u]=-1;
dfs(u+1,gp);
}
} void init(int x,int y)
{
while(!q[0].empty())
q[0].pop();
while(!q[1].empty())
q[1].pop();
memset(dist,-1,sizeof(dist));
memset(vis,-1,sizeof(vis));
dist[x*3+y][0]=0;
vis[x*3+y][0]=0;
q[0].push(Node(0,x*3+y,0));
for(int i=0;i<9;++i)
if(goal[i]==0){
dfs(0,i);
break;
}
} int getv(int p,int s)
{
for(int i=1;i<=8-p;++i)
s/=6;
return s%6;
} int bfs(int id,int step)
{
while(!q[id].empty())
{
Node u=q[id].top();
if(u.t>step)
return -1;
q[id].pop(); if(vis[u.n][u.s]==!id)
return u.t; int gg[9];
int x=u.n/3,y=u.n%3;
for(int i=0;i<4;++i){
int nx=x+d[i][0],ny=y+d[i][1];
if(nx<0||nx>2||ny<0||ny>2)
continue; int s=u.s;
for(int j=8;j>=0;--j){
if(j==u.n)
gg[j]=-1;
else{
gg[j]=s%6;
s/=6;
}
}
gg[u.n]=p[gg[nx*3+ny]][i];
gg[nx*3+ny]=-1;
int sta=0,cnt=0;
for(int j=0;j<9;++j)
if(gg[j]!=-1){
sta=sta+ss[7-cnt]*gg[j];
++cnt;
}
if(vis[nx*3+ny][sta]!=id){
if(vis[nx*3+ny][sta]==-1){
vis[nx*3+ny][sta]=id;
dist[nx*3+ny][sta]=u.t+1;
q[id].push(Node(u.t+1,nx*3+ny,sta));
}
else
return u.t+1+dist[nx*3+ny][sta];
}
}
}
return -1;
} int solve()
{
int step=0;
while(!q[0].empty()||!q[1].empty()){
if(step>30)
return -1;
if(!q[0].empty()&&step<=21){
int k=bfs(0,step);
if(k>30)
return -1;
if(k!=-1)
return k;
}
if(!q[1].empty()&&step<=9){
int k=bfs(1,step);
if(k>30)
return -1;
if(k!=-1)
return k;
}
++step;
}
return -1;
} int main()
{
//freopen("UVA-1604 Cubic Eight-Puzzle.txt","r",stdin);
mp['E']=0,mp['W']=1,mp['R']=2,mp['B']=3;
int x,y,gx,gy;
char s[2];
while(scanf("%d%d",&y,&x)&&(x+y))
{
--x,--y;
for(int i=0;i<9;++i){
scanf("%s",s);
goal[i]=mp[s[0]];
}
init(x,y);
printf("%d\n",solve());
}
return 0;
}
UVA-1604 Cubic Eight-Puzzle (双向BFS+状态压缩+限制搜索层数)的更多相关文章
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- HDU 1885 Key Task (BFS + 状态压缩)
题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...
- hdu 1429(bfs+状态压缩)
题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...
随机推荐
- AngularJs:Directive指令用法
摘自:http://www.jb51.net/article/83051.htm 摘要:Directive(指令)是AngularJ非常强大而有有用的功能之一.它就相当于为我们写了公共的自定义DOM元 ...
- __devexit_p的功能
看驱动的时候,时常会有如下代码: .remove = __devexit_p(XX_exit), 这里的__devexit_p有什么作用呢? 我在include/linux/init.h中找到了它的定 ...
- java 颁发公钥 私钥 php js RSA 加密解密整合
PHP rsa密钥生成 加密解密 - PHP开发 - CSDN博客 https://blog.csdn.net/duzhenxun/article/details/8879227 <?php c ...
- NAT STURN,ICE
NAT原理与NAT穿越 最近在看东西的时候发现很多网络程序中都需要NAT穿越,特意在此总结一下. 先做一个约定: 内网A中有:A1(192.168.0.8).A2(192.168.0.9)两用户 网关 ...
- Kafka Consumer接口
对于kafka的consumer接口,提供两种版本, high-level 一种high-level版本,比较简单不用关心offset, 会自动的读zookeeper中该Consumer grou ...
- 大话https演化过程(对称加密、非对称加密、公钥、私钥、数字签名、数字证书)
大话https演化过程(包括概念:对称加密.非对称加密.公钥.私钥.数字签名.数字证书.https访问全过程) 在网络上发送数据是非常不安全的,非常容易被劫持或是被篡改,所以每次定向发送数据你都可 ...
- malloc calloc realloc 区别
(1)C语言跟内存分配方式 <1>从静态存储区域分配. 内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量.<2> ...
- redis—Spring中redis缓存的简单使用
这里使用的是 Spring-4.3 , redis-2.8 的版本 1.添加maven依赖 <dependency> <groupId>redis.clients</ ...
- (ZT)谷歌大脑科学家 Caffe缔造者 贾扬清 微信讲座完整版
一.讲座正文:大家好!我是贾扬清,目前在Google Brain,今天有幸受雷鸣师兄邀请来和大家聊聊Caffe.没有太多准备,所以讲的不好的地方还请大家谅解.我用的ppt基本上和我们在CVPR上要做的 ...
- VS2010/MFC编程入门之三十(常用控件:树形控件Tree Control 上)
前面两节为大家讲了列表视图控件List Control,这一节开始介绍一种特殊的列表--树形控件Tree Control. 树形控件简介 树形控件在Windows系统中是很常见的,例如资源管理器左侧的 ...