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. 代码收藏系列--jquery--筛选器、事件绑定技巧

    Jquery筛选器的一些常用技巧,比如过滤属性等 /* 过滤获取没有含data-xsui-grid-colspan的节点 */$(this).find('.xsui-grid-cell:not([da ...

  2. 洛谷P1396 营救

    题目描述 “咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动的热泪盈眶,开起了门…… 妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小 ...

  3. 使用jvisualvm工具来监控java运行情况

    jvisualvm是jdk自带的工具.所以要先安装jdk   1.jvisualvm工具的路径: 通过which jvisualvm来查看 /usr/local/jdk1.7.0_79/bin/jvi ...

  4. gcc和MinGW的异同

    cygwin/gcc和MinGW都是gcc在windows下的编译环境,但是它们有什么区别,在实际工作中如何选择这两种编译器. cygwin/gcc完全可以和在linux下的gcc化做等号,这个可以从 ...

  5. github访问很慢的问题

    公司一直用着svn, 之前也的确用过github的版本管理,但是一直都是可视化的操作 这几天面试了几名前端,问了一下发现他们在之前的公司里都是用git的, 于是今天好好温故了一下怎么用命令行进行一下g ...

  6. codeforces 872E. Points, Lines and Ready-made Titles

    http://codeforces.com/contest/872/problem/E E. Points, Lines and Ready-made Titles time limit per te ...

  7. GridControl详解(十)BandedGridView

    转换结果: 运行结果呈现:

  8. windows下gitlab配置 生成ssh key

    Git-1.9.5-preview20141217 1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C "e ...

  9. 【POJ】3070 Fibonacci

    [算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...

  10. 计算1到N中各个数字出现的次数 --数位DP

    题意:给定一个数n,问从1到n中,0~9这10个数字分别出现了多少次.比如366这个数,3出现了1次,6出现了2次. 题解:<剑指offer>P174:<编程之美>P132 都 ...