Eight

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11226    Accepted Submission(s): 3013
Special Judge

Problem 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
 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
using namespace std;
typedef struct eight
{
char a[][];
int statu,nowx,nowy;
} eight;
eight s,e;
int f[],flag[],ok;
int father1[],father2[];
int move1[],move2[],last;
queue<eight>q1,q2;
void init()
{
f[]=;
int i;
for(i=; i<; i++)f[i]=f[i-]*i;
}
void work(eight &a)
{
int i,j,k=,ans=,t;
char b[];
for(i=; i<; i++)
{
for(j=; j<; j++)
{
if(a.a[i][j]=='x')a.nowx=i,a.nowy=j;
b[k++]=a.a[i][j];
}
}
k=;
for(i=; i<; i++)
{
t=;
for(j=i+; j<; j++)
if(b[j]<b[i])t++;
ans+=f[k--]*t;
}
a.statu=ans;
}
void copy(eight &a,eight &b)
{
int i,j;
for(i=; i<; i++)
for(j=; j<; j++)a.a[i][j]=b.a[i][j];
}
int w[][]= {{,},{,-},{,},{-,}};
void ex_BFS(eight &now,int n,int check)
{
int i,r,c;
eight in;
for(i=; i<; i++)
{ r=now.nowx+w[i][];
c=now.nowy+w[i][];
if(r>=&&r<&&c>=&&c<)
{
copy(in,now);
swap(in.a[r][c],in.a[now.nowx][now.nowy]);
work(in);
if(flag[in.statu]!=n)
{
if(n==)
{
q1.push(in);
father1[in.statu]=now.statu;
move1[in.statu]=i;
}
else
{
q2.push(in);
father2[in.statu]=now.statu;
move2[in.statu]=i;
}
if(flag[in.statu]==check)
{
ok=;
last=in.statu;
return ;
}
flag[in.statu]=n;
}
}
}
}
bool TBFS(eight &s,eight &e)
{
memset(flag,,sizeof(flag));
memset(move1,-,sizeof(move1));
memset(move2,-,sizeof(move2));
memset(father1,-,sizeof(father1));
memset(father2,-,sizeof(father2));
eight now;
ok=;
while(!q1.empty())q1.pop();
while(!q2.empty())q2.pop();
q1.push(s),flag[s.statu]=;
q2.push(e),flag[e.statu]=;
while((!q1.empty())||(!q2.empty()))
{
now=q1.front();
q1.pop();
ex_BFS(now,,);
if(ok)return ; now=q2.front();
q2.pop();
ex_BFS(now,,);
if(ok)return ;
}
return ;
}
bool check1(eight &s)
{
int i,j,k=;
char b[];
for(i=; i<; i++)
for(j=; j<; j++)
b[k++]=s.a[i][j];
int ans=;
for(i=; i<; i++)
{
if(b[i]!='x')
for(j=; j<i; j++)
if(b[j]!='x'&&b[i]<b[j])ans++;
}
return (ans&);
}
void printpath()
{
deque<char>q;
while(!q.empty())q.pop_back();
int now=last;
while(father1[now]!=-)
{
if(move1[now]==)q.push_back('r');
else if(move1[now]==)q.push_back('l');
else if(move1[now]==)q.push_back('d');
else if(move1[now]==)q.push_back('u');
now=father1[now];
}
while(!q.empty())
{
cout<<q.back();
q.pop_back();
} now=last;
while(father2[now]!=-)
{
if(move2[now]==)cout<<'l';
else if(move2[now]==)cout<<'r';
else if(move2[now]==)cout<<'u';
else if(move2[now]==)cout<<'d';
now=father2[now];
}
}
int main()
{
int i,j;
init();
char w;
while(cin>>w)
{
char an='';
for(i=; i<; i++)
for(j=; j<; j++)
{
if(i||j)
cin>>s.a[i][j];
e.a[i][j]=an++;
}
s.a[][]=w; e.a[][]='x';
if(check1(s))
{
cout<<"unsolvable"<<endl;
continue;
}
work(s);
work(e);
if(s.statu==e.statu)
{
cout<<endl;
continue;
}
if(TBFS(s,e))
{
printpath();
cout<<endl;
}
else
cout<<"unsolvable"<<endl;
} }

Eight hdu 1043 八数码问题 双搜的更多相关文章

  1. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

  2. HDU 1043 八数码(A*搜索)

    在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...

  3. HDU 1043 八数码(八境界)

    看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...

  4. HDU 1043 八数码问题的多种解法

    一.思路很简单,搜索.对于每一种状态,利用康托展开编码成一个整数.于是,状态就可以记忆了. 二.在搜索之前,可以先做个优化,对于逆序数为奇数的序列,一定无解. 三.搜索方法有很多. 1.最普通的:深搜 ...

  5. HDU 1043 八数码 Eight A*算法

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

  6. hdu 1043 八数码问题

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

  7. C++ 八数码问题宽搜

    C++ 八数码问题宽搜 题目描述 样例输入 (none) 样例输出 H--F--A AC代码 #include <iostream> #include <stdio.h> #i ...

  8. hdu.1043.Eight (打表 || 双广 + 奇偶逆序)

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

  9. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

随机推荐

  1. C# 爬虫 Jumony html解析

    前言 前几天写了个爬虫,然后认识到了自己的不足.感谢 "倚天照海- -" ,我通过你推荐的文章,意外的发现了html解析的类库——Jumony. 研究了2天,我发现这个东西简单粗暴 ...

  2. DOS命令运行java文件,批量引用jar包

    进入class文件目录 cd:\workspace\workspace_goodsownersystem\workspace_goodsownersystem\goodsownersystem\tar ...

  3. Oracle:解锁scott用户及设置密码

    关于Oracle 10g scott用户解锁的方法两则 解决方法一. 首先确认已经安装oracle 数据库和客户端 在客户端DOS下执行如下语句: 注意提示符号 c:\sqlplus /nolog s ...

  4. UIButton和UIimageView

    1.按钮控件使用的类是UIButton 点击按钮会触发某个事件 2.按钮控件的初始化 UIButton *button = [UIButton buttonWithType:UIButtonTypeC ...

  5. pip install python 如何快速安装模块

    之前python安装模块要在网络上下载,从python2.7.9之后,以及python3,python就自带pip 这个命令,能够快速的安装模块 1,  首先打开python的主文件夹 2.在主文件夹 ...

  6. 结对编程-四则运算-GUI

     201421123022 王若凡        201421123026  欧阳勇 https://git.coding.net/ttoyy/sizeyunsuan-GUI.git a.需求分析: ...

  7. 团队作业4---第一次项目冲刺(ALpha)版本 第七天

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 a.完成所有基础功能 b.正在进行测试调试 四.困难与问题 1.随着测试出现了大大小小的一些BUG,但是由于原来写的时候思维定 ...

  8. 201521123072《Java程序设计》第6周学习总结

    201521123072<Java程序设计>第6周学习总结 标签(空格分隔): java 1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画 ...

  9. 201521123093 java 第五周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 答:接口:1.所有的默认方法都是public abstract; 2.属性都是p ...

  10. 201521123017 《Java程序设计》第4周学习总结

    1. 本周学习总结 2. 书面作业 Q1.注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图)   Q2.面向对象设计(大作业1,非常重要) 2.1 将 ...