题目:

简单介绍一下八数码问题:
        在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图:

1 2 3
4 5 6
7 8  


        在上图中,由于右下角位置是空的,你可以移动数字,比如可以将数字6下移一位:

1 2 3   1 2 3
4 5 6 4 5  
7 8     7 8 6


        或者将数字8右移一位:

1 2 3   1 2 3
4 5 6 4 5 6
7 8     7   8


        1~8按顺序排列的情况称为“初始状态”(如最上方图)。“八数码问题”即是求解对于任意的布局,将其移动至“初始状态”的方法。 
        给定一个现有的九宫格布局,请输出将它移动至初始状态的移动方法的步骤。

输入:

 输入包含多组数据,处理至文件结束。每组数据占一行,包含8个数字和表示空位的‘x’,各项以空格分隔,表示给定的九宫格布局。 
        例如,对于九宫格

1 2 3
  4 6
7 5 8


        输入应为:1 2 3 x 4 6 7 5 8

 输出:

对于每组输入数据,输出一行,即移动的步骤。向上、下、左、右移动分别用字母u、d、l、r表示;如果给定的布局无法移动至“初始 状态”,请输出unsolvable。
        如果有效的移动步骤有多种,输出任意即可。

样例:

分析:双向BFS,简单来说就是同时进行两个BFS,但每个BFS的vis数组有了新的用途即判断另一个BFS是否达到此BFS扩展到的此刻的点,若抵达即连通。

unsolvable的情况用逆序数的奇偶性判断,因为目标状态12345678逆序数为0,所以当前态的逆序数必为偶

用康托展开记录字典序用于状态压缩(hash)

 #include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cctype>
#define PI acos(-1.0)
const int INF = 0x3f3f3f3f;
const int NINF = -INF - ;
typedef long long ll;
using namespace std;
int fac[] = {, , , , , , , , , };
int vis1[], vis2[];
int eda[]={ , , , , , , , , };
string path[];
int dx[] = {, -, , }, dy[] = {, , -, };
char p1[] = {'d', 'u', 'l', 'r'}, p2[] = {'u', 'd', 'r', 'l'};
struct node
{
int s[];
int cur, n;//cur目前x所在位置,n为hash值
}st;
int cantor(int *s)//康托展开
{
int sum = ;
for (int i = ;i < ; ++i)
{
int k = ;
for (int j = i + ; j < ; ++j)
if( s[j] < s[i]) k++;
sum += k * fac[ - i];
}
return sum;
}
void bfs()
{
memset(vis1, , sizeof(vis1));
memset(vis2, , sizeof(vis2));
queue<node> q1, q2;
st.n = cantor(st.s);
q1.push(st);
vis1[st.n] = ;
path[st.n] = "";//重要
node ed;
memcpy(ed.s, eda, sizeof(ed.s));
ed.n = cantor(ed.s);
ed.cur = ;
path[ed.n] = "";
q2.push(ed);
vis2[ed.n] = ;
while (q1.size() || q2.size())
{
node temp1 = q1.front();
q1.pop();
int x1 = temp1.cur / , y1 = temp1.cur % ;
for (int i = ; i < ; ++i)
{
int nx = x1 + dx[i], ny = y1 + dy[i];
if (nx < || nx > || ny < || ny > ) continue;
node rec = temp1;
rec.cur = nx * + ny;
swap(rec.s[temp1.cur], rec.s[rec.cur]);
rec.n = cantor(rec.s);
if (vis2[rec.n])
{
reverse(path[rec.n].begin(), path[rec.n].end());
cout << path[temp1.n] << p1[i] << path[rec.n] << endl;
return;
}
if (!vis1[rec.n])
{
vis1[rec.n] = ;
path[rec.n] = path[temp1.n];
path[rec.n] += p1[i];
q1.push(rec);
}
}
node temp2 = q2.front();
q2.pop();
int x2 = temp2.cur / , y2 = temp2.cur % ;
for (int i = ; i < ; ++i)
{
int nx = x2 + dx[i], ny = y2 + dy[i];
if (nx < || nx > || ny < || ny > ) continue;
node rec = temp2;
rec.cur = nx * + ny;
swap(rec.s[temp2.cur], rec.s[rec.cur]);
rec.n = cantor(rec.s);
if (vis1[rec.n])
{
reverse(path[temp2.n].begin(), path[temp2.n].end());
cout << path[rec.n] << p2[i] << path[temp2.n] << endl;
return;
}
if (!vis2[rec.n])
{
vis2[rec.n] = ;
path[rec.n] = path[temp2.n];
path[rec.n] += p2[i];
q2.push(rec);
}
}
}
} int main()
{
char c;
while(cin >> c)
{
if(c == 'x')
{
st.s[] = ;
st.cur = ;
}
else st.s[] = c - '';
for (int i = ; i < ; ++i)
{
cin >> c;
if ( c == 'x')
{
st.s[i] = ;
st.cur = i;
}
else st.s[i] = c - '';
}
int k = ;
for(int i = ; i < ; ++i)
{
if (st.s[i])
{
for (int j = i + ; j < ; ++j)
if (st.s[j] < st.s[i] && st.s[j]) k++;
}
}
if(k & ) cout<< "unsolvable" << endl;
else bfs();
}
return ;
}

HDU1043 Eight的更多相关文章

  1. ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)

    八数码问题也称为九宫问题.(本想查查历史,结果发现居然没有词条= =,所谓的历史也就不了了之了) 在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个 ...

  2. HDU-1043 Eight八数码 搜索问题(bfs+hash 打表 IDA* 等)

    题目链接 https://vjudge.net/problem/HDU-1043 经典的八数码问题,学过算法的老哥都会拿它练搜索 题意: 给出每行一组的数据,每组数据代表3*3的八数码表,要求程序复原 ...

  3. 后缀数组:HDU1043 Longest Common Substring

    Longest Common Substring Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  4. HDU1043 Eight(BFS)

    Eight(South Central USA 1998) Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  5. 【双向广搜+逆序数优化】【HDU1043】【八数码】

    HDU上的八数码 数据强的一B 首先:双向广搜 先处理正向搜索,再处理反向搜索,直至中途相遇 visit 和 队列都是独立的. 可以用一个过程来完成这2个操作,减少代码量.(一般还要个深度数组) 优化 ...

  6. hdu1043

    #include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#inclu ...

  7. POJ1077&&HDU1043(八数码,IDA*+曼哈顿距离)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30127   Accepted: 13108   Special ...

  8. hdu-1043 bfs+康拓展开hash

    因为是计算还原成一种局面的最短步骤,应该想到从最终局面开始做bfs,把所有能到达的情况遍历一遍,把值存下来. bfs过程中,访问过的局面的记录是此题的关键,9*9的方格在计算过程中直接存储非常占内存. ...

  9. hdu-1043(八数码+bfs打表+康托展开)

    参考文章:https://www.cnblogs.com/Inkblots/p/4846948.html 康托展开:https://blog.csdn.net/wbin233/article/deta ...

  10. HDU1043 Eight(八数码:逆向BFS打表+康托展开)题解

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. eclipse Errors during build

    eclipse在运行main方法或者运行ant里的clean方法时,总是会报下面的错,需要点击第二次才能正常运行 今天终于把这个问题解决了,解决方案如下 项目右键,点properties 点击buil ...

  2. Windows如何正确的修改administrator用户名

    无论是服务器还是电脑.修改默认的administrator的用户总是好的.话不多少.直接上图 1.win+r  输入:gpedit.msc 2.按照我下图的圈圈找到重命名系统管理员账户并自定义 3.重 ...

  3. css页面布局总结

    W3C标准:是万维网制定的一系列标准,包括结构化标准语言(html.xml),表现 标准语言(css),行为标准语言(DOM,ECMAScript<javascript>)组成.这个标准倡 ...

  4. 【转】关于JMeter线程组中线程数,Ramp-Up Period,循环次数之间的设置概念

    关于JMeter线程组中线程数,Ramp-Up Period,循环次数之间的设置概念 笔者是个刚刚踏入压力测试领域不到2个月的小菜,这里分享一下线程组中3个参数之间关系的个人见解,不喜请!喷!,望大家 ...

  5. Linux之iptables(四、网络防火墙及NAT)

    网络防火墙 iptables/netfilter网络防火墙: (1) 充当网关 (2) 使用filter表的FORWARD链 注意的问题: (1) 请求-响应报文均会经由FORWARD链,要注意规则的 ...

  6. 弹层蒙版(mask),ios滚动穿透,我们项目的解决方案

    问题描述 项目开发遇到一个ios独有的问题,在wkwebview中稳定复现 问题: 弹出一个蒙版,当在蒙版上面滑动的时候蒙版后面的内容滚动了 这当然是ios的bug,但是经过我们测试iphone7也会 ...

  7. 第十五节:pandas之concat()级联

    Pandas 提供了concat()函数可以轻松的将Series.DataFrame对象进行合并在一起. pandas.concat(obj , axis=0 , join="inner&q ...

  8. 利用定时器 1和定时器0控制led1和led2分别 2hz和0.5hz闪烁

    //利用定时器 1和定时器0控制led1和led2分别 2hz和0.5hz闪烁 #include<reg52.h> #define uchar unsigned char #define ...

  9. c#获取文字全拼音

    class Program { /// <summary> /// 获得拼音 /// </summary> /// <param name="str_Spell ...

  10. 【Codeforces 1114C】Trailing Loves (or L'oeufs?)

    [链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/8116 ...