题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=1043

题目描述:

  3*3的格子,填有1到8,8个数字,还有一个x,x可以上下左右移动,问最终能否移动到12345678x的状态?

  hint:每一个3*3的格子从上到右,从左到右,一行一行读。

解题思路:

  比较简单的八数码问题,大一暑假老师讲过,一直手懒脑懒并没有亲自尝试过。因为是多实例,先从12345678x的状态bfs出来所有可以到达的状态,并且记录下来路径。八数码最重要的就是保存状态,如果这个题目用十进制保存状态的话,至少要到达10^9的数量级。可以利用康拓展开hash搜索到的状态,空间可以缩小到9!(ง •̀_•́)ง

 #include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std; #define maxn 370000
struct node
{
string path;//路径
int s[]; //state
int loc; //9的位置
int ct; //康拓hash
};
int fac[] = {, , , , , , , *, **, ***};
//0!,1!,2!....9!
int dir[][] = {,, ,-, -,, ,}; // r, l, u, d
int vis[maxn];
char di[] = "lrdu"; //l, r, d, u
string path[maxn];
void init ()
{
memset (vis, , sizeof(vis));
} int cantor (int a[]) //康拓hash
{
int sum = ;
for (int i=; i<; i++)
{
int m = ;
for (int j=i+; j<; j++)
if (a[j] < a[i])
m ++;
sum += m * (fac[-i-]);
}
return sum + ;
} void bfs ()
{
node cur, next;
queue <node> Q; for(int i=; i<; i++)
cur.s[i] = i + ;
cur.s[] = ;
cur.ct = cantor (cur.s);
cur.loc = ; Q.push (cur);
vis[cur.ct] = ;
path[cur.ct] = ""; while (!Q.empty())
{
cur = Q.front();
Q.pop (); for (int i=; i<; i++)
{
int x = cur.loc / + dir[i][];
int y = cur.loc % + dir[i][];
if (x< || x> || y< || y>)
continue;
next = cur;
next.loc = x * + y;
next.s[cur.loc] = next.s[next.loc];
next.s[next.loc] = ;
next.ct = cantor (next.s);
if (!vis[next.ct])
{
vis[next.ct] = ;
path[next.ct] = di[i] + path[cur.ct];
Q.push (next);
}
}
} } int main ()
{
init ();
bfs ();
char s[];
int a[]; while (scanf ("%s", s) != EOF)
{
if (s[] == 'x')
a[] = ;
else
a[] = s[] - ''; for (int i=; i<; i++)
{
scanf ("%s", s);
if (s[] == 'x')
a[i] = ;
else
a[i] = s[] - '';
}
int m = cantor (a);
if (vis[m])
cout<<path[m]<<endl;
else
puts ("unsolvable");
}
return ;
}

Hdu 1043 Eight (八数码问题)的更多相关文章

  1. HDU 1043 Eight 八数码问题 A*算法(经典问题)

    HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...

  2. hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】

    <题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为 ...

  3. HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法

    先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...

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

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

  5. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

  6. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Problem Descr ...

  7. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  8. HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)

    题意  输出八数码问题从给定状态到12345678x的路径 用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了 #include <bits/stdc++.h ...

  9. HDU 1043 八数码(A*搜索)

    在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...

随机推荐

  1. node.js内存泄露问题记录

    先说一下.事情的来龙去脉. 公司开发一款游戏棋牌游戏,服务端的开发是IO密集型,开发的时候,考虑过使用python,java,node.js. 终于选择了node.js(node.js宣传的杀手功能. ...

  2. 解决移动端页面滚动后不触发touchend事件

    解决移动端页面滚动后不触发touchend事件 问题 在移动端页面进行优化时,一般使用touch事件替代鼠标相关事件.用的较多的是使用touchend事件替代PC端的click和mouseup事件. ...

  3. HDOJ--1869--六度分离(用三种算法写的,希望能比較出来他们之间的差别)

    六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. eclipse 添加库

    Window ->Preferences ->Java ->Build Path ->User Libraries New,起个名字,如myLibrary add jars,添 ...

  5. spark集成hivecontext配置

    spark版本:spark-1.6.0-bin-hadoop2.6hive版本:hive 1.2.1 1.hive-site.xml<property>  <name>hive ...

  6. POJ 3279 Dungeon Master

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21242   Accepted: 8265 D ...

  7. android 一个TextView设置多种颜色

    时候一个文本框为了强调内容需要显示不同颜色,用以下代码可以轻松实现 方法一:(适用于颜色变化多的情况)   //为文本框设置多种颜色 textView=(TextView)findViewById(R ...

  8. 一步一步学Silverlight 2系列(26):基本图形

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  9. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

  10. 多线程-threading模块2

    从上面例子中发现线程的创建是颇为麻烦的,每创建一个线程都需要创建一个 t(t1.t2....),如果创建的线程较多时这样极其不方便.下面对通过例子进行改进:   #coding:utf-8 impor ...