Eight(South Central USA 1998)

Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:

 1  2  3  4
5 6 7 8
9 10 11 12
13 14 15 x

where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

 1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
r-> d-> r->

The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and 
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three 
arrangement. 

 

Input

You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus 'x'. For example, this puzzle

1 2 3 
x 4 6 
7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

 

Output

You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line. Do not print a blank line between cases. 
 

Sample Input

2 3 4 1 5 x 7 6 8
 

Sample Output

ullddrurdllurdruldr
 
题目简单翻译:
八数码
给你一个八数码:(格式是一个九宫格,x代表空),问怎么操作能达到目标,即:
1 2 3
4 5 6
7 8 x
u代表空格向上交换,d代表空格向下交换,l代表空格向左交换,r代表空格向右交换。
例如:
给你一个八数码:
1 2 3
x 4 6
7 5 8
则把它变成目标需要三步:
1 2 3   r   1 2 3   d   1 2 3  r   1 2 3
x 4 6  --->  4 x 6  --->  4 5 6  --->  4 5 6
7 5 8      7 5 8      7 x 8      7 8 x
所以这个样例应该输出:
rdr
如果不能到达目标就输出“unsolvable”.
 
解题思路:广度优先搜索(BFS)
因为是多组数据,我们可以先求出所有情况,然后每次询问的时候直接输出结果就好了。
求出所有结果,我们就可以根据结果,来逆向bfs,直到所有的情况都求到。
 
代码:
 #include<cstdio>
#include<string>
#include<queue>
#include<cstring>
using namespace std;
struct node
{
int t[][],x,y,Can;
int Last_Can,dir;
} St[];
int fac[]= {,,,,,,,,};
//康托展开的数组
//康托展开就是把一组数据按照字典序排列的那组数据的序号 int vis[];
queue<int> Q;
char dr[]="rlud";
int dx[]= {,,,-};
int dy[]= {-,,,};
//方向数组,与实际的方向相反,因为是逆向操作
int Cantor(int *t)//对一组数据求康拓值
{
int rev=;
for(int i=; i<; i++)
{
int counted=;
for(int j=i+; j<; j++)
if(t[i]>t[j]) counted++;
rev+=counted*fac[-i];
}
return rev;
}
bool check(int x,int y) //检查这个点是不是在矩形内
{
return x>=&&x<&&y>=&&y<;
}
void solve()//bfs求出所有的情况,并储存下来父节点
{
while(!Q.empty()) Q.pop();
node st;
st.x=st.y=;
int s[][]= {,,,,,,,,};
int t[];
for(int i=; i<; i++)
for(int j=; j<; j++)
t[i*+j]=s[i][j];
for(int i=; i<; i++)
for(int j=; j<; j++)
st.t[i][j]=s[i][j];
int StCan=Cantor(t);
st.Can=StCan;
st.Last_Can=-;
st.dir=-;
memset(vis,,sizeof vis);
vis[StCan]=;
St[StCan]=st;
Q.push(StCan);
int Sum=;
while(!Q.empty())
{
Sum++;
int TempCan=Q.front();
Q.pop();
for(int i=; i<; i++)
{
node e=St[TempCan];
int curx=e.x+dx[i];
int cury=e.y+dy[i];
if(check(curx,cury))
{
int c=e.t[curx][cury];
e.t[curx][cury]=e.t[e.x][e.y];
e.t[e.x][e.y]=c;
e.x=curx;
e.y=cury;
int t[];
for(int i=; i<; i++)
for(int j=; j<; j++)
t[i*+j]=e.t[i][j];
e.Can=Cantor(t);
e.Last_Can=TempCan;
e.dir=i;
if(!vis[e.Can])
{
vis[e.Can]=;
St[e.Can]=e;
Q.push(e.Can);
}
}
}
}
}
int main()
{
char c[];
int t[],x=;
solve();
while(scanf("%s",c)!=EOF)
{
if(c[]=='x') c[]='';
t[]=c[]-'';
for(int i=;i<;i++)
{
scanf("%s",c);
if(c[]=='x') c[]='';
t[i]=c[]-'';
}
int AnsCan=Cantor(t);
if(vis[AnsCan])
{
int p=AnsCan;
while(St[p].Last_Can+)
{
printf("%c",dr[St[p].dir]);
p=St[p].Last_Can;
}
printf("\n");
}
else
printf("unsolvable\n");
}
return ;
}

Eight

 

HDU1043 Eight(BFS)的更多相关文章

  1. Eight HDU-1043 (bfs)

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

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

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

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

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

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

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

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

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

  6. HDU1043 八数码(BFS + 打表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...

  7. hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数

    题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...

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

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

  9. 【HDU - 1043】Eight(反向bfs+康托展开)

    Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8   在上图中,由于右下角位置是空的 ...

随机推荐

  1. adb shell am pm

    adb shell am instrument [options] <COMPONENT> 作用:启动对instrument实例的监视. 参数[options]: -e <key&g ...

  2. php get_ini 和 get_cfg_var 的区别

    get_ini 和 get_cfg_var 都是用来获取 php 配置信息的函数. 区别是 get_ini 是用来获取当前运行的配置信息,get_cfg_var 是用来获取配置文件(php.ini)的 ...

  3. 8051_asm.uew

    /L20"8051 Assembly" AASM_LANG Line Comment = ; Nocase String Chars = ' File Extensions = S ...

  4. 《Programming WPF》翻译 目录

    原文:<Programming WPF>翻译 目录 注:第1.2章我只做了笔记,没有翻译,请大家阅读时注意. 还有就是,这本书的英文版本下载:[O'Reilly] Programming ...

  5. gcc的stdcall扩展

    MSVC上: 如果是函数调用约定直接就写 __stdcall 之类的就行了 如果是gcc上: 函数的扩展调用约定要这样写 __attribute__((__stdcall__)),默认为__attri ...

  6. COM组件开发实践(八)---多线程ActiveX控件和自动调整ActiveX控件大小(下)

    源代码下载:MyActiveX20081229.rar 声明:本文代码基于CodeProject的文章<A Complete ActiveX Web Control Tutorial>修改 ...

  7. Word Break I II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  8. TCP/IP协议详解概述

    TCP/IP协议详解卷1--第一章概述--读书笔记 作者:vpoet 日期:2015/06/25 注:本系列的文章只是作者对TCP/IP协议的理解,难免会出现纰漏或者不完整,当然也有可能很肤浅,希望大 ...

  9. python定时器爬取豆瓣音乐Top榜歌名

    python定时器爬取豆瓣音乐Top榜歌名 作者:vpoet mail:vpoet_sir@163.com 注:这些小demo都是前段时间为了学python写的,现在贴出来纯粹是为了和大家分享一下 # ...

  10. GridFS

    GridFS是一个建立在MongoDB文档基础之上的轻量级的文件存储规范. GridFS的一个基本思想就是可以将一个大文件分成很多块.每块作为一个单独的文档存储. GridFS支持在文档中存储二进制数 ...