Eight
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29935   Accepted: 13029   Special Judge

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 a description of a configuration of the 8 puzzle. The 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.

Sample Input

 2  3  4  1  5  x  7  6  8 

Sample Output

ullddrurdllurdruldr

题意:经典八数码
题解:预处理终点到所有状态的路径。康拓展开保存状态
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N = ;
int fab[] = {,,,,,,,,};
bool vis[N];
struct Node{
int a[];
int Hash;
int _x; ///x所在位置
};
struct Way{
char c;
int pre;
}way[N];
int contor(Node s){
int sum = ;
for(int i=;i>=;i--){
int cnt = ;
for(int j=i-;j>=;j--){
if(s.a[i]<s.a[j]) cnt++;
}
sum+=fab[i-]*cnt;
}
return sum;
}
int dir[][] = {{-,},{,},{,-},{,}}; ///上下左右
bool change(Node &s,int _x,int k){
int x = (_x-)/+;
int y = _x%==?:_x%;
int nextx = x+dir[k][];
int nexty = y+dir[k][];
if(nextx<||nexty>||nexty<||nexty>) return false;
swap(s.a[_x],s.a[(nextx-)*+nexty]);
s._x = (nextx-)*+nexty;
return true;
}
void bfs(){
for(int i=;i<N;i++){
way[i].pre = -;
}
memset(vis,false,sizeof(vis));
Node s;
for(int i=;i<=;i++){
s.a[i] = i;
}
s.Hash = ,s._x = ;
vis[] = ;
queue<Node> q;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
Node next;
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'd';
q.push(next);
}
}
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'u';
q.push(next);
}
}
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'r';
q.push(next);
}
}
next = now;
if(change(next,next._x,)){
int k = contor(next);
if(!vis[k]){
vis[k] = true;
next.Hash = k;
way[k].pre = now.Hash;
way[k].c = 'l';
q.push(next);
}
}
}
}
char str[];
char ans[];
int t = ;
void dfs(int k){
if(way[k].pre==-) return;
dfs(way[k].pre);
ans[t++]=way[k].c;
}
int main()
{
bfs();
while(scanf("%s",str)!=EOF){
Node s;
s.a[] = (str[]=='x')?:str[]-'';
for(int i=;i<=;i++){
scanf("%s",str);
s.a[i] = (str[]=='x')?:str[]-'';
}
int k = contor(s);
ans;
t = ;
dfs(k);
if(t==){
printf("unsolvable\n");
continue;
}
for(int i=t-;i>=;i--){
printf("%c",ans[i]);
}
printf("\n");
}
return ;
}

poj 1077(BFS预处理+康托展开)的更多相关文章

  1. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  2. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  3. HDU 1043 Eight 【经典八数码输出路径/BFS/A*/康托展开】

    本题有写法好几个写法,但主要思路是BFS: No.1 采用双向宽搜,分别从起始态和结束态进行宽搜,暴力判重.如果只进行单向会超时. No.2 采用hash进行判重,宽搜采用单向就可以AC. No.3 ...

  4. HDU_1430——魔板,预处理,康托展开,置换,string类的+操作

    Problem Description 在魔方风靡全球之后不久,Rubik先生发明了它的简化版——魔板.魔板由8个同样大小的方块组成,每个方块颜色均不相同,可用数字1-8分别表示.任一时刻魔板的状态可 ...

  5. HDU - 1430 魔板 (bfs预处理 + 康托)

    对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...

  6. POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)

    思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...

  7. HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二

    类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...

  8. POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3

    http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...

  9. POJ 1077 Eight (BFS+康托展开)详解

    本题知识点和基本代码来自<算法竞赛 入门到进阶>(作者:罗勇军 郭卫斌) 如有问题欢迎巨巨们提出 题意:八数码问题是在一个3*3的棋盘上放置编号为1~8的方块,其中有一块为控制,与空格相邻 ...

随机推荐

  1. ssh后台执行

    1 执行scp命令,开始传输2 scp传输开始后,用ctrl+z,再以bg命令将其转入后台运行. 3以exit命令安全退出即可令scp继续执行,不受关闭shell的影响.

  2. Mysql(一) 基本操作

    一.介绍 1.数据库 数据库,通俗的讲,即为存储数据的“仓库”.不过,数据库不仅只是存储,还对所存储的数据做相应的管理,例如,访问权限,安全性,并发操作,数据的备份与恢复,日志等.实际上,我们所提及的 ...

  3. scala(三)

    一.面向对象编程——类 1.定义一个简单的类 class HelloWorld { private var name = "leo" def sayHello() { print( ...

  4. 【极值问题】【CF33C】 Wonderful Randomized Sum

    传送门 Description 给你一个数列\(A\),你可以选择任意一个前缀和任意一个后缀,前缀后缀可重合.给他们乘\(-1\).求最大能获得的序列和. Input 第一行是一个数\(n\)代表数列 ...

  5. static_cast 和 dynamic_cast

    1.static_cast static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保证 ...

  6. 《用Apache HttpClient实现URL重定向》

    作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 很多网站都使用了URL重定向技术,把一个原始请求从一个位置路由到另一个位置.原因可能是多方面的,比如域名转 ...

  7. 深入浅出CSS(一):line-height与vertical-align的性质

    [测试代码] <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  8. 学习 C++的用途,(前辈总结)

    C++准确说是一门中级语言,介于汇编和高级语言之间吧,要求程序员了解计算机的内部数据存储.个人认为,作为学生还是花功夫学C++,因为<设计模式><数据结构>这些课程基本上还是C ...

  9. JAVA中反射机制二

    声明:如需转载请说明地址来源:http://www.cnblogs.com/pony1223 反射二 利用反射创建对象 1.利用反射创建对象,首先我们创建一个类,类里面,我们知道构造函数有默认的构造函 ...

  10. [php]数组建立方式

    1.$a[0]=..; $a[1]=..; $a[2]=..; $a[3]=..; 2.$a=array(1,2,3,4,5); 3.自定义数组 $a['logo']="qq"; ...