题意:经典八数码问题

思路:双向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)的更多相关文章

  1. 2017多校第10场 HDU 6171 Admiral 双向BFS或者A*搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6171 题意: 给你一个高度为6的塔形数组,你每次只能将0与他上下相邻的某个数交换,问最少交换多少次可以 ...

  2. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  3. HDU 3085 Nightmare Ⅱ (双向BFS)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. HDU 3085 Nightmare Ⅱ 双向BFS

    题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...

  5. HDU 1043 Eight(反向BFS+打表+康托展开)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...

  6. Nightmare Ⅱ HDU - 3085 (双向bfs)

    Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...

  7. ACM-BFS之Open the Lock——hdu1195(双向BFS)

    这道题的0基础版本号,暴力BFS及题目详情请戳:http://blog.csdn.net/lttree/article/details/24658031 上回书说道,要用双向BFS来尝试一下. 最终A ...

  8. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

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

随机推荐

  1. TCP No-Delay

    Nagle 算法 由于TCP中包头的大小是固定的,所以在数据(Payload)大小很小的时候IP报文的有效传输率是很低的,Nagle算法就是将多个即将发送的小段的用户数据,缓存并合并成一个大段数据时, ...

  2. Jetson TK1 五:移植工控机程序到板上

    1.gazebo xml 2.王 chmod 777 chmod 777 /home/robot2/bzrobot_ws/src/bzrobot/bzrobot_control/bzrobot_con ...

  3. HDU_4770 Lights Against Dudely 状压+剪枝

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 Lights Against Dudely Time Limit: 2000/1000 MS ( ...

  4. 字符串哈希hash

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

  5. 应用程序中的server错误,没有名称为“ServiceBehavior”的服务行为

    应用程序中的server错误,没有名称为"ServiceBehavior"的服务行为         今天在阅读"创建和使用Web服务"的相关内容,在浏览器中查 ...

  6. HDU oj 开门人与关门人

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1234 #include<stdio.h> #include<string.h> ...

  7. Elasticsearch 学习笔记 Elasticsearch及Elasticsearch head安装配置

    一.安装与配置 1.到官网下载Elasticsearch,https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6. ...

  8. [转载]CodeIgniter配置之URL

    应该有很多项目中会有这样的情况,通过 http://pc.local 可以访问,若通过 http://localhost/pc/public 则会出现一些图片.样式显示不到,超链接出错的情况,项目的移 ...

  9. AVL树,红黑树,B-B+树,Trie树原理和应用

    前言:本文章来源于我在知乎上回答的一个问题 AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中? 看完后您可能会了解到这些数据结构大致的原理及为什么用在这些场景,文章并不涉及具体操作 ...

  10. OpenWrt inittab

    OpenWrt 启动时会执行 rc.d/ 下的脚本. 这篇文章 介绍了启动脚本里的规则. K50dropbear -> ../init.d/dropbear K85odhcpd -> .. ...