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. BC 65 game

    主持人一直某个数字在1到n范围,假设甲乙已经知道,甲先猜乙后,都采用最优策略,主持人说偏大还是偏小,不断缩小范围,问最后乙能会获胜的X的取值的个数. 如果n为奇数,那么仅当x=n/2乙必然获胜,若为奇 ...

  2. information_schema.routines 学习

    information_schema.routines 用户查看mysql中的routine信息 1.information_schema.routines 表中的常用列: 1.

  3. Keil C51汉字显示的bug问题(0xFD问题)

    一.缘起 这两天改进MCU的液晶显示方法,采用“即编即显”的思路,编写了一个可以直接显示字符串的程序.如程序调用disstr("我是你老爸");液晶屏上就会显示“我是你老爸”. 二 ...

  4. POJ1988 并查集的使用

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 21157   Accepted: 7395 Ca ...

  5. No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)

    之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...

  6. 关于 Private strand flush not complete

    网友发来告警日志,原本是关于一个死锁的情形,而另外的一个问题则是从redo log buffer写出到redo log file出现了不能分配新的日志,Private strand flush not ...

  7. DWR常用<init-param>参数

    1 安全参数 allowGetForSafariButMakeForgeryEasier 开始版本:2.0 默认值:false 描述:设置成true使DWR工作在Safari 1.x , 会稍微降低安 ...

  8. 【转】android电池(四):电池 电量计(MAX17040)驱动分析篇

    关键词:android 电池  电量计  MAX17040 任务初始化宏 power_supply 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台: ...

  9. Unity 调用android插件

    1. Unity的Bundle Identifier必须和你的android报名一致 Activity和View的区别: Activity应该是一个展示页面,View是页面上一些按钮视图等等. 如何调 ...

  10. Han Move(细节题)

    Problem 1609 - Han Move Time Limit: 1000MS   Memory Limit: 65536KB    Total Submit: 620  Accepted: 1 ...