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 Input

2 3 4 1 5 x 7 6 8
题意 : 就是8数码问题,主要时搜索的路径寻找问题,把'x'转换为0,然后把当前这些数都存储为1个状态
分析: 这道题有两道相同的,分别是HDU和POJ,HDU上的数据加强了比POJ更麻烦。。
POJ:正向搜索就好(简单)
 #include <iostream>
#include <cstring> using namespace std; const int maxn = ;
typedef int State[];
State st[maxn];
int goal[] = {, , , , , , , , };
int dx[] = {-, , , };
int dy[] = { , , -, };
int head[maxn], nxt[maxn], fa[maxn];
char dir[maxn]; int Hash(State s) //哈希函数
{
int ret = , i;
for(i = ; i < ; i++) ret = ret * + s[i];
return ret % maxn;
} bool try_to_insert(int rear) //插入哈希表
{
int h = Hash(st[rear]);
for(int e = head[h]; e != -; e = nxt[e])
{
if(memcmp(st[e], st[rear], sizeof(st[e])) == ) return ;
}
nxt[rear] = head[h];
head[h] = rear;
return ;
} int bfs() //遍历
{
int frt = , rear = , i, z;
while(frt < rear)
{
State& s = st[frt];
if(memcmp(s, goal, sizeof(s)) == ) return frt;
for(z = ; s[z] != ; z++);
int x = z / ;
int y = z % ;
for(i = ; i < ; i++)
{
int newx = x + dx[i];
int newy = y + dy[i];
int newz = * newx + newy;
if(newx >= && newx < && newy >= && newy < )
{
State& news = st[rear];
memcpy(news, s, sizeof(s));
news[z] = s[newz];
news[newz] = ;
if(try_to_insert(rear))    //注意这里的路径输出的方式
{
fa[rear] = frt;
switch(i)
{
case : dir[rear] = 'u'; break;
case : dir[rear] = 'd'; break;
case : dir[rear] = 'l'; break;
case : dir[rear] = 'r'; break;
default: break;
}
rear++;
}
}
}
frt++;
}
return ;
} void print(int i) //输出
{
if(fa[i] == -) return;
print(fa[i]);
cout<<dir[i];
} int main()
{
char c[];
int i, ret;
while(cin>>c[]>>c[]>>c[]>>c[]>>c[]>>c[]>>c[]>>c[]>>c[])
{
for(i = ; i < ; i++) st[][i] = c[i] == 'x' ? : (int)(c[i]-'');
memset(head, -, sizeof(head));
fa[] = -;
ret = bfs();
if(ret)
{
print(ret);
}
else cout<<"unsolvable";
cout<<endl;
}
return ;
}

HDU : 这道题时多组输入,所以不能向上面一样在线写,而是要从最终状态开始倒着把所有状态搜索一遍,之后只需要输入初始状态打表判断输出路径即可;

  学习到的知识有两个:bfs()路径查找类 + 康拓展开,路径的输出:

 /*************************************************************************
> File Name: search.cpp
> Author : PrayG
> Mail: 996930051@qq,com
> Created Time: 2016年07月20日 星期三 10时56分09秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
#include<cmath>
using namespace std;
const int maxn = ;
int fac[] = {,,,,,,,,,};
int dx[] = {,,-,},dy[] = {,,,-};//drul
char ind[] = "uldr";//与上面相反
string path[maxn];//记录路径
bool vis[maxn];
int aim = ;//123456780 的康拓展开 struct node
{
int s[]; //记录状态
int sit0;  //0 的位置
int val;   //康拓展开的值
string path;  // 路径
}; int cant(int s[])  //康拓展开
{
int code = ;
for(int i = ; i < ; i++)
{
int cnt = ;
for(int j= i+ ; j < ; j++)
{
if(s[i] > s[j])
{
cnt++;
}
}
code += fac[-i] * cnt;
}
return code;
} void bfs()
{
memset(vis,false,sizeof(vis));
queue<node> que;
node cnt1,cnt2;
for(int i = ; i < ;i++)
cnt1.s[i] = i+;
cnt1.s[] = ;
cnt1.sit0 = ;
//printf("aim = %d\n",aim);
cnt1.val = aim;
cnt1.path = "";
path[aim] = "";
que.push(cnt1);
while(!que.empty())
{
cnt1 = que.front();
que.pop();
int x = cnt1.sit0 / ;
int y = cnt1.sit0 % ;
for(int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
int nz = nx * + ny;
if(nx < || nx > || ny < || ny >)
continue;
cnt2 = cnt1;
cnt2.s[cnt1.sit0] = cnt2.s[nz];
cnt2.s[nz] = ;
cnt2.sit0 = nz;
cnt2.val = cant(cnt2.s);
if(!vis[cnt2.val])
{
vis[cnt2.val] = true;
cnt2.path = ind[i] + cnt1.path;
que.push(cnt2);
path[cnt2.val] = cnt2.path;
}
} }
} int main()
{
bfs();
char t;
while(cin >> t)
{
node st;
if(t == 'x'){
st.s[] = ;
st.sit0 = ;
}
else
st.s[] = t - '';
for(int i = ; i< ; i++)
{
cin >> t;
if(t == 'x')
{
st.s[i] = ;
st.sit0 = i;
}
else
st.s[i] = t -'';
}
st.val = cant(st.s);
if(vis[st.val])
{
cout << path[st.val] << endl;
}
else
cout << "unsolvable" << endl;
}
return ;
}
 
 

Eight hdu 1043 poj 1077的更多相关文章

  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. Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

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

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

  5. HDU - 1043 - Eight / POJ - 1077 - Eight

    先上题目: Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)

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

  7. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)

    Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...

  8. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

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

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

随机推荐

  1. 紫书 例题 10-4 UVa 10791(唯一分解定理)

    首先分解,然后可以发现同一个因子ai不能存在于两个以上的数中 因为求的是最小公倍数,如果有的话就可以约掉 所以数字必然由ai的pi次方的乘积组成,那么显然,在 a最小为2,而b大于2的情况下a*b&g ...

  2. Noip-pj2018游记

    2019/1/3 搬运于我的luogu博客 2018/10/9 没有去试机,在学校搞文化课去了.准考证是让学校的信息课老师帮我拿的 回家后随手A了P1198 P3870 P2846 P1531 感觉真 ...

  3. Android APP弱网测试问题和解决分析

    最近做了一次移动APP的弱网和中断测试,接下来分享一下遇到的一些问题: 1.现象:用户登录应用时下载初始化数据,下载过程中因网速太慢点击取消并重新登录,数据初始化完成后出现重复,造成数据不一致. 原因 ...

  4. jsp urlrewrite 中正則表達式不包括某个字符串写法

    因在程序中须要做城市间跳转,可是页面中包括的css.scripts和图片等路径是要排除在外的. 这就须要在正则中指定当遇到哪些 字符时须要略过. 正则例如以下: /((? !css)(?!script ...

  5. 微信iOS SDK文档总结

    至今共19个类.分3大类. (1)请求与响应类:微信终端和第三方程序:第三方程序和微信server. BaseReq:全部请求类的基类. GetMessageFromWXReq:微信终端向第三方程序请 ...

  6. 1.Swift教程翻译系列——关于Swift

    英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 我本来是做JAVA的.可是有一颗折腾的心,苹果公布Swift以后就下载了苹果的开 ...

  7. lucene LZ4 会将doc存储在一个chunk里进行Lz4压缩 ES的_source便如此

    默认情况下,Elasticsearch 用 JSON 字符串来表示文档主体保存在 _source 字段中.像其他保存的字段一样,_source 字段也会在写入硬盘前压缩.The _source is ...

  8. 3.c语言结构体成员内存对齐详解

    一.关键一点 最关键的一点:结构体在内存中是一个矩形,而不是一个不规则形状 二.编程实战 #include <stdlib.h> #include <stdio.h> stru ...

  9. ZooKeeper Recipes and Solutions

    原文地址:http://zookeeper.apache.org/doc/current/recipes.html 参考:https://zookeeper.apache.org/doc/trunk/ ...

  10. 基于python3-sklearn,Flask 的回归预测系统

    看到一副图片挺有意思,放在片头 序 "傍晚小街路面上沁出微雨后的湿润,和煦的西风吹来,抬头看看天边的晚霞,嗯明天又是一个好天气.走到水果摊旁,挑了个根蒂蜷缩.敲起来声音浊响的青绿西瓜,一边满 ...