Eight Puzzle

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/contest/show/61

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. To simplify this problem, you should print the minimum steps only.

Input

There are multiple test cases.

For each test case, 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.Otherwise, output an integer which equals the minimum steps.

Sample Input

1 2 x 4 5 3 7 8 6

Sample Output

2

HINT

Any violent algorithm may gain TLE. So a smart method is expected.

The data used in this problem is unofficial data prepared by hzhua. So any mistake here does not imply mistake in the offcial judge data.

题意

八数码问题

题解:

爆搜题
八数码:
1.A*搜索,是否AC看脸
2.双向BFS
3.单次BFS预处理之后,O(1)回答

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** map<int,int> vis1;
map<int,int> vis2;
char s[];
int dx[]={,-,,};
int dy[]={,,,-};
struct node
{
int m[][];
int x,y;
int ha;
int t;
};
node st;
int check()
{
int a[],t=;
for(int i=;i<;i++)
for(int j=;j<;j++)
a[t++]=st.m[i][j];
t=;
for(int i=;i<;i++)
for(int j=i+;j<;j++)
if(a[i]&&a[j]&&a[i]>a[j])
t++;
return t&;
}
int get_ha(node b)
{
int sum=;
int a[],t=;
for(int i=;i<;i++)
for(int j=;j<;j++)
a[t++]=b.m[i][j]; ll N=;
long long h=;
for(int i=;i<;i++)
h=(h*+(a[i])+N)%N;
return h%N;
}
int main()
{
char ch;
node aimm;
aimm.m[][]=;
aimm.m[][]=;
aimm.m[][]=;
aimm.m[][]=;
aimm.m[][]=;
aimm.m[][]=;
aimm.m[][]=;
aimm.m[][]=;
aimm.m[][]=;
aimm.x=;
aimm.y=;
aimm.t=;
int aim=get_ha(aimm);
aimm.ha=aim;
while(cin>>ch)
{
vis1.clear();
vis2.clear();
s[]=ch;
for(int i=;i<;i++)
{
cin>>ch;
s[i]=ch;
}
int m=;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(s[m]=='x')st.m[i][j]=,st.x=i,st.y=j;
else st.m[i][j]=s[m]-'';
m++;
}
} if(check())
{
printf("unsolvable\n");
continue;
}
st.t=;
st.ha=get_ha(st);
vis1[st.ha]=;
vis2[aimm.ha]=;
queue<node> q;
queue<node> q2;
q2.push(aimm);
q.push(st);
while(!q.empty()||!q2.size())
{
if(!q.empty())
{
node now=q.front();
/*
cout<<now.t<<" "<<1<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<now.m[i][j];
}
cout<<endl;
}
cout<<endl;
*/
if(vis2[now.ha])
{
//cout<<now.t<<" "<<"1231231"<<endl;
printf("%d\n",now.t+vis2[now.ha]-);
break;
}
q.pop();
for(int i=;i<;i++)
{
node next=now;
next.t++;
if(next.x+dx[i]<||next.x+dx[i]>=)
continue;
if(next.y+dy[i]<||next.y+dy[i]>=)
continue;
swap(next.m[next.x][next.y],next.m[next.x+dx[i]][next.y+dy[i]]);
next.ha=get_ha(next);
next.x+=dx[i];
next.y+=dy[i];
if(vis1[next.ha]>)
continue;
vis1[next.ha]=next.t;
q.push(next);
}
}
if(!q2.empty())
{
node now=q2.front();
/*
cout<<now.t<<" "<<2<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<now.m[i][j];
}
cout<<endl;
}
cout<<endl;
*/
if(vis1[now.ha])
{
//cout<<now.t<<" "<<"12312131"<<endl;
printf("%d\n",now.t+vis1[now.ha]-);
break;
}
q2.pop();
for(int i=;i<;i++)
{
node next=now;
next.t++;
if(next.x+dx[i]<||next.x+dx[i]>=)
continue;
if(next.y+dy[i]<||next.y+dy[i]>=)
continue;
swap(next.m[next.x][next.y],next.m[next.x+dx[i]][next.y+dy[i]]);
next.ha=get_ha(next);
next.x+=dx[i];
next.y+=dy[i];
if(vis2[next.ha]>)
continue;
vis2[next.ha]=next.t;
q2.push(next);
}
}
}
}
}

2015 UESTC 搜索专题F题 Eight Puzzle 爆搜的更多相关文章

  1. 2015 UESTC 搜索专题E题 吴队长征婚 爆搜

    吴队长征婚 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descri ...

  2. 2015 UESTC 搜索专题C题 基爷与加法等式 爆搜DFS

    基爷与加法等式 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  3. 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp

    秋实大哥の恋爱物语 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...

  4. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  5. 2015 UESTC 搜索专题N题 韩爷的梦 hash

    韩爷的梦 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descrip ...

  6. 2015 UESTC 搜索专题M题 Palindromic String 马拉车算法

    Palindromic String Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/s ...

  7. 2015 UESTC 搜索专题J题 全都是秋实大哥 kmp

    全都是秋实大哥 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  8. 2015 UESTC 搜索专题D题 基爷的中位数 二分

    基爷的中位数 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Descr ...

  9. 2015 UESTC 搜索专题A题 王之迷宫 三维bfs

    A - 王之迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

随机推荐

  1. 大美西安writeup

    http://202.112.51.184:10080/ admin/admin 弱口令登入 发现注入 但是这个注入实在是不知道怎么利用.很蛋疼.后来get了一个姿势. 先-1让前面的不被下载然后后面 ...

  2. 31 - gogs安装-git基础

    目录 1 Gogs安装 2 Git介绍 3 使用Github仓库 3.1 Git配置 3.2 远程仓库 4 Git基本使用 4.1 创建版本库 4.2 查看工作区状态 4.3 查看修改内容 4.4 查 ...

  3. WPF之换肤

    WPF之换肤 设计原理 WPF换肤的设计原理,利用资源字典为每种皮肤资源添加不同的样式,在后台切换皮肤资源文件. 截图 上图中,第一张图采用规则样式,第二张图采用不规则样式,截图的时候略有瑕疵. 资源 ...

  4. Spring中的@Transactional事务注解

    事务注解方式 @Transactional 当标于类前时, 标示类中所有方法都进行事物处理 , 例子: @Transactional public class TestServiceBean impl ...

  5. git clone的

    git clone git@e.coding.net:wudi360/*******.git

  6. CGIC简明教程(转摘)

    CGIC简明教程 本系列的目的是演示如何使用C语言的CGI库“CGIC”完成Web开发的各种要求. *********************************     基础知识       1 ...

  7. 安装scrapy 出错 building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required.

    安装Scrapy出现错误: building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required. ...

  8. HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...

  9. hdu 1850(尼姆博弈)

    Being a Good Boy in Spring Festival Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32 ...

  10. JAVA邻接矩阵实现拓扑排序

    由于一直不适用邻接表 ,现在先贴一段使用邻接矩阵实现图的拓扑排序以及判断有无回路的问题.自己做的图.将就看吧. package TopSort; import java.util.LinkedList ...