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. [C++程序设计]指针数组和指向指针的指针

    指针数组的概念 一维指针数组的定义形式为 类型名*数组名[数组长度]; 例如 int *p[4]; 可以用指针数组中各个元素分别指向若干个字符串,使字符串处理更加方便灵活

  2. Robberies(HDU 2955 DP01背包)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. windows平台使用Microsoft Visual C++ Compiler for Python 2.7编译python扩展

    在windows平台上安装python c extension的扩展包是件很痛苦的事情,一般通过安装vc/vs系列来编译C扩展,不过安装包都比较大.或者通过mingw编译,不过有时会在兼容性上出现点问 ...

  4. Dropdownlist的onchange事件应用

    function selectDpList(dp) { var sIndex = dp.selectedIndex;//返回选中是第几项 0,1.... var sText = dp.options[ ...

  5. Linux下的命令行上网

    对于网页浏览器现在大多数人用links/elinks,对了,还有个老牌一点的文本浏览器Lynx,links/elinks也是从Lynx中fork出来的. 以上所说的虽然能字符界面来浏览网页,但是不能显 ...

  6. cf479E Riding in a Lift

    E. Riding in a Lift time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. iOS 之点击按钮改变状态的图片

    .h  文件中 @property (strong, nonatomic) IBOutletUIButton *publishBtton; @property (strong, nonatomic) ...

  8. Windows SVN变化邮件通知(Python2.7实现)

    1,新增文件post-commit.bat 内容: rem REPOS-PATH (the path to this repository) set REPOS=%1 rem REV (the num ...

  9. Java获取客户端真实IP地址的两种方法

    在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...

  10. 设置MySQL数据表主键

    设置MySQL数据表主键: 使用“primary key”关键字创建主键数据列.被设置为主键列不允许出现重复的值,很多情况下与“auto_increment”递增数字相结合.如下SQL语句所示: My ...