hdu 1043 Eight(双向bfs)
题意:经典八数码问题
思路:双向bfs
ps:还有a*算法(还不会)等解法。
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; #define MAXN 362885//最多组合个数:9!=362880 int dir[]={-,,-,};//4个方向移动:u,d,l,r
char opF[]={'u','d','l','r'};//正向bfs操作
char opR[]={'d','u','r','l'};//反向bfs操作
int fac[]={,,,,,,,,};//阶乘
bool visF[MAXN];//正向访问标志
bool visR[MAXN];//
string sTarg="";//目标棋盘 struct node{//存储棋盘信息
string s;//当前棋盘
int xLoca;//x位置
}t; struct node2{//存储操作
int idF,idR;//正向前驱、反向前驱
char cF,cR;//操作
}op[MAXN]; int cantor_hash(string s){//康托展开,一共n位
int n=;//共9位
int i,j,temp,num;
num=;
for(i=;i<n-;i++){//n为位数
temp=;
for(j=i+;j<n;j++){
if(s[j]<s[i])temp++;
}
num+=fac[n-(i+)]*temp;
}
return num;//从0开始
} void display(int i){//输出正向操作
if(op[i].idF==-)return;
display(op[i].idF);
printf("%c",op[i].cF);
} void dBfs(){
queue<node>qF,qR;//正向队列,反向队列
node tmp1,tmp2;//棋盘状态临时变量
int id_hash,id_hash2,xLoca,i;//id 康托哈希下标值,xLoca 暂存x位置 tmp1=t;//初始状态 tmp2.s=sTarg;//目标状态
tmp2.xLoca=;//目标状态x位置 id_hash=cantor_hash(tmp1.s);//初始状态
visF[id_hash]=true;
op[id_hash].idF=-;
qF.push(tmp1); id_hash=cantor_hash(tmp2.s);//目标状态
visR[id_hash]=true;
op[id_hash].idR=-;
qR.push(tmp2); while(!qF.empty()&&!qR.empty()){
//正向bfs
tmp1=qF.front();
qF.pop();
id_hash=cantor_hash(tmp1.s);
if(visR[id_hash]){//反向bfs也访问过
display(id_hash);
id_hash2=id_hash;
while(op[id_hash2].idR!=-){
printf("%c",op[id_hash2].cR);
id_hash2=op[id_hash2].idR;
}
printf("\n");
return;
}
for(i=;i<;++i){
if(i==&&tmp1.xLoca<)continue;
if(i==&&tmp1.xLoca>)continue;
if(i==&&tmp1.xLoca%==)continue;
if(i==&&tmp1.xLoca%==)continue;
xLoca=tmp1.xLoca+dir[i]; tmp2=tmp1;
swap(tmp2.s[tmp1.xLoca],tmp2.s[xLoca]);
id_hash2=cantor_hash(tmp2.s);
if(!visF[id_hash2]){
visF[id_hash2]=true;
tmp2.xLoca=xLoca;
op[id_hash2].idF=id_hash;
op[id_hash2].cF=opF[i];
qF.push(tmp2);
}
} //反向bfs
tmp1=qR.front();
qR.pop();
id_hash=cantor_hash(tmp1.s);
if(visF[id_hash]){//正向bfs也访问过
display(id_hash);
id_hash2=id_hash;
while(op[id_hash2].idR!=-){
printf("%c",op[id_hash2].cR);
id_hash2=op[id_hash2].idR;
}
printf("\n");
return;
}
for(i=;i<;++i){
if(i==&&tmp1.xLoca<)continue;
if(i==&&tmp1.xLoca>)continue;
if(i==&&tmp1.xLoca%==)continue;
if(i==&&tmp1.xLoca%==)continue;
xLoca=tmp1.xLoca+dir[i]; tmp2=tmp1;
swap(tmp2.s[tmp1.xLoca],tmp2.s[xLoca]);
id_hash2=cantor_hash(tmp2.s);
if(!visR[id_hash2]){
visR[id_hash2]=true;
tmp2.xLoca=xLoca;
op[id_hash2].idR=id_hash;
op[id_hash2].cR=opR[i];
qR.push(tmp2);
}
}
}
} int main(){
char str[];//串
int len;//串长度
int i,j,k;//k 第几个数码
int ivsNum;//逆序数
while(~scanf("%[^\n]",str)){
getchar();//吸收回车 memset(visF,false,sizeof(visF));
memset(visR,false,sizeof(visR)); len=strlen(str);
t.s="";
k=;//第几个数码
for(i=;i<len;++i){//处理字符串
if(str[i]!=' '){
if(str[i]=='x'){
t.s=t.s+'';
t.xLoca=k;
}
else t.s=t.s+str[i];
++k;//
}
} ivsNum=;//逆序数初始为0
for(i=;i<;++i){
if(t.s[i]=='')continue;
for(j=;j<i;++j){
if(t.s[j]=='')continue;
if(t.s[j]>t.s[i])++ivsNum;
}
}
if(ivsNum&)printf("unsolvable\n");//逆序数为奇数
else dBfs();//双向bfs
}
return ;
}
hdu 1043 Eight(双向bfs)的更多相关文章
- 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
- Nightmare Ⅱ HDU - 3085 (双向bfs)
Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...
- ACM-BFS之Open the Lock——hdu1195(双向BFS)
这道题的0基础版本号,暴力BFS及题目详情请戳:http://blog.csdn.net/lttree/article/details/24658031 上回书说道,要用双向BFS来尝试一下. 最终A ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
随机推荐
- android layout
android的视图分为两类,一类是布局,另一个类是控件 一.LinearLayout(线性布局) 最常用布局之一,线性布局的特性是每添加一个控件默认会在上个控件的下面占一行. <LinearL ...
- Unity 3D 之通过序列化来存档游戏数据
我们在使用u3d开发一些单机游戏的过程中,都会涉及到游戏数据的存单和加载.一般情况下,如果存储的数据不复杂,我们就可以用PlayerPrefs,但有时涉及到的数据更加复杂,使用PlayerPrefs难 ...
- python多线程(一)
原文:http://www.pythonclub.org/python-basic/threading 一.python多线程thread和threading实现 python是支持多线程的,并且是n ...
- luogu P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- codeforces edu40
H(dp计数) 题意: 有一颗树,最深的点的深度是n,每个深度为i的点都有ai个孩子. 对于1<=k<=2n-2,回答树上有多少点对之间的距离是k,答案对1e9+7取模 n<=500 ...
- 解决filter拦截request中body内容后,字符流关闭,无法传到controller的问题
解决filter拦截request中body内容后,字符流关闭,无法传到controller的问题 2.问题: 在一般的请求中,content-type为:application/x-www-form ...
- python中pymysql使用
python中pymysql使用 https://blog.csdn.net/johline/article/details/69549131 import pymysql # 连接数据库 conne ...
- 《Deep Learning》全书已完稿_附全书电子版
Deep Learning第一篇书籍最终问世了.站点链接: http://www.deeplearningbook.org/ Bengio大神的<Deep Learning>全书电子版在百 ...
- 【转载】C#扫盲之:==/Equals /ReferenceEquals 异同的总结,相等性你真的知道吗?
1.前言 == Equals ReferenceEquals 三个相等性测试,是.NET提供给程序员使用的三个方法,他们之间有什么联系和区别,你真的仔细研究过?虽然之前也多多少少知道一点,但是有时候又 ...
- Vue 基础
1. data 数据 methods 方法 watch 监听变化 2. 模版指令(类似 angular) {{}} v-text 渲染数据 v-html html 结构 3. v-if v-show ...