题目:

简单介绍一下八数码问题:
        在一个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. (转)OL2中设置鼠标的样式

    http://blog.csdn.net/gisshixisheng/article/details/49496289 概述: 在OL2中,鼠标默认是箭头,地图移动时,鼠标样式是移动样式:很多时候,为 ...

  2. uva 540 (Team Queue UVA - 540)

    又是一道比较复杂的模拟题.题中有两种队列,一种是总队列,从前向后.其他的是各个团体的小队列,因为入队的人如果有队友的话,会优先进入团体队列. 所以我们先设置两个队列和一个map,设置map倒是可以不用 ...

  3. Huawei-R&S-网络工程师实验笔记20190609-VLAN划分综合(Hybrid端口)

    >Huawei-R&S-网络工程师实验笔记20190609-VLAN划分综合(Hybrid端口) >>实验开始,先上拓扑图参考: >>>实验目标:分别实现主 ...

  4. vue 根据网站路由判断页面主题色

    需求: 不同品牌对应不同版本配色 做法: 根据域名带的参数判断进入哪个品牌,对应哪个版本 在main.js中 import Vue from 'vue' import App from './App' ...

  5. 【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap

    [链接] 我是链接,点我呀:) [题意] 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出). 你是灭霸你要用以下方法消灭这k个复仇者: 一开始你获取整个区间[1..2^n ...

  6. 4、ceph-deploy之配置使用对象存储

    从firefly(v0.80)版本开始,ceph存储显著的简化了安装和配置Ceph Object Gateway, Gateway进程嵌入到Civetweb,所以你需要安装一个web服务,或者配置Fa ...

  7. (40). springboot + devtools(热部署)【从零开始学Spring Boot】

    我们之前在在()Spring Boot热部署[从零开始学Spring Boot] (http://412887952-qq-com.iteye.com/blog/2291518 )讲过通过使用spri ...

  8. 2.1.5、SparkEnv中创建MapOutputTracker

    SparkEnv中创建MapOutputTracker def registerOrLookupEndpoint( name: String, endpointCreator: => RpcEn ...

  9. noip模拟赛 Chtholly Nota Seniorious

    题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg こんなにも.たくさんの幸せをあの人に分けてもらった だから.きっと 今の.私は 谁が何と ...

  10. 使用git bash向github远程仓库提交代码

    1.登录github,创建仓库. 2.切换到要提交的文件目录下. 3.打开git bash 3.1.初始化仓库 git init 3.2.将本地仓库与远程仓库关联 git remote add ori ...