题目大意:立体的八数码问题,一次操作是滚动一次方块,问从初始状态到目标状态的最少滚动次数。

题目分析:这道题已知初始状态和目标状态,且又状态数目庞大,适宜用双向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+状态压缩+限制搜索层数)的更多相关文章

  1. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  2. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  3. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

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

  5. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  7. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  8. HDU 1885 Key Task (BFS + 状态压缩)

    题意:给定一个n*m的矩阵,里面有门,有钥匙,有出口,问你逃出去的最短路径是多少. 析:这很明显是一个BFS,但是,里面又有其他的东西,所以我们考虑状态压缩,定义三维BFS,最后一维表示拿到钥匙的状态 ...

  9. hdu 1429(bfs+状态压缩)

    题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...

随机推荐

  1. java web 打印(lodop)案例

    应需求接触到lodop 打印. 首先在lodop官网下载相关文件(js.css等):http://www.lodop.net/download.html 在下载好的包里  除了html页面  其他的j ...

  2. 利用gulp解决微信浏览器缓存问题

    做了好多项目,这次终于要解决微信浏览器缓存这个令人头疼的问题了.每次上传新的文件,在微信浏览器中访问时,总要先清除微信的缓存,实在麻烦,在网上搜罗了很多解决办法,终于找到了方法:利用gulp解决缓存问 ...

  3. JS实现数字千位符格式化方法

    /** * [number_format 参数说明:] * @param {[type]} number [number:要格式化的数字] * @param {[type]} decimals [de ...

  4. BBS - 后台管理

    一.添加文章 注: 后台管理页面,应该有个新得 app /blog/backend/ # 文章列表页/blog/add_article/ # 添加文章 # 后台管理re_path(r'backend/ ...

  5. 【Loadrunner】平台1.9环境APP成功录制并调试成功后的脚本备份

    1.录制相关Loadrunner及录制的APP所在手机网络代理相关设置请参考日志:http://www.cnblogs.com/zhuzhubaoya/p/9152022.html 2.调试成功的脚本 ...

  6. android GridView的setOnItemClickListener事件不执行

    问题可能1: item设置的可能是button,或者可以click点击事件控件,导致控件执行而item按钮不执行 解决方法:设置控件 的  android:clickable="false& ...

  7. go-005-变量、常量

    概述 变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念.变量可以通过变量名访问. Go 语言变量名由字母.数字.下划线组成,其中首个字母不能为数字. 声明变量的一般形式是使用 var 关 ...

  8. nodejs通过代理(proxy)发送http请求(request)

    有可能有这样的需求,需要node作为web服务器通过另外一台http/https代理服务器发http或者https请求,废话不多说直接上代码大家都懂的: var http = require('htt ...

  9. mac常用操作:

    Mac常用软件需要熟悉 常用操作: command + w 关闭窗口  + n 最小化当前窗口  + m 关闭所有窗口  +  + w command + c 复制 command + v 粘贴 co ...

  10. (转)JavaScriptSerializer,DataContractJsonSerializer解析JSON字符串功能小记

    JsonAbout: using System;using System.Collections.Generic;using System.Linq;using System.Text;using S ...