Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏
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
Difficulty:
1.康拓展开:类似于状态压缩将一个数组映射为一个数。
适用条件:当知道这个数组的个数是一定的。
2.一位数组和二维数组的互换。
http://wenku.baidu.com/link?url=YeNqa-MsEwqOZTvOR__alZS12Xog8OZDlnIXVA5WORuM0lCPu9LmkFrSsbYFBuFVj_h5F0hf6I4NVZD076lwG_xEAmWJft2HtRKWMIWAkBa
思路:将二维数组看成一个状态,再进行bfs,同时要将二维数组先变形为一位数组再根据康拓展开变形为一个数。
打表,反向搜索。
注意:这里有多解,不止一个答案,方向顺序不同,答案不同。
#include <cstdio>
#include <iostream>
#include<cstring>
using namespace std;
struct Node
{
int a[9];
}Q[500000];//转态表示
int n,eid,cnt,flag,x0,ya,z0,tmpid,x,y;
int vis[500000];
Node s,t;
char ss[5];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
char dir[]="lrdu";//反向搜索,所以反着写。
char di[500000];
int A[9];
int net[500000];
int getid(int *a)//康拓展开
{
int v=0;
for(int i=0;i<=8;i++)
{
int cnt=0;
for(int j=i+1;j<=8;j++)
if(a[i]>a[j])
cnt++;
v+=cnt*A[8-i];
}
return v;
}
void bfs()
{
memset(vis,0,sizeof(vis));
int front=0,rear=0;
Q[rear++]=s;
//net[eid]=-1;
vis[eid]=1;
while(front<rear)
{
Node q=Q[front++];
tmpid=getid(q.a);//一维数组映射为一个数
for(int i=0;i<9;i++)
{
if(q.a[i]==0)
{x0=i/3;
ya=i%3;//一维数组转换为二维数组。
z0=i;
break;
}}
Node add=q;;
for(int i=0;i<4;i++)
{
x=x0+dx[i];
y=ya+dy[i];
if(x<0||x>=3||y<0||y>=3)
continue;
int z=x*3+y;//二维数组转换为一维数组
add.a[z0]=q.a[z];
add.a[z]=0;
int qid=getid(add.a);
if(vis[qid]==0)
{
vis[qid]=1;
net[qid]=tmpid;//转态转移
di[qid]=dir[i];//记录操作
Q[rear++]=add;
}
add=q;
}
//front++;
}
}
int main()
{ A[0]=1;
for(int i=1;i<=8;i++)
A[i]=A[i-1]*i;//康拓展模板
for(int i=0;i<=8;i++)
if(i==8)
s.a[i]=0;
else
s.a[i]=i+1;
eid=getid(s.a);
bfs();
while(~scanf("%s",ss))
{
if(ss[0]=='x')
s.a[0]=0;
else
s.a[0]=ss[0]-'0';
for(int i=1;i<=8;i++)
{
scanf("%s",ss);//可过滤空格
if(ss[0]=='x')
s.a[i]=0;
else
s.a[i]=ss[0]-'0';
}
int sid=getid(s.a);
if(vis[sid])
{
while(sid!=eid)//上面打的表中推出一步一步的转态转换。
{
putchar(di[sid]);
sid=net[sid];
}
}
else
printf("unsolvable");
puts("");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏的更多相关文章
- C/C++文字常量与常变量的概念与区别 分类: C/C++ 2015-06-10 22:56 111人阅读 评论(0) 收藏
以下代码使用平台是Windows 64bits+VS2012. 在C/C++编程时,经常遇到以下几个概念:常量.文字常量.符号常量.字面常量.常变量.字符串常量和字符常量,网上博客资料也是千篇千律,不 ...
- 快速幂取模 分类: ACM TYPE 2014-08-29 22:01 95人阅读 评论(0) 收藏
#include<stdio.h> #include<stdlib.h> //快速幂算法,数论二分 long long powermod(int a,int b, int c) ...
- Java 函数参数传递方式详解 分类: Java Game 2014-08-15 06:34 82人阅读 评论(0) 收藏
转:http://zzproc.iteye.com/blog/1328591 在阅读本文之前,根据自己的经验和理解,大家可以先思考并选择一下Java函数的参数传递方式: A. 是按值传递的? B. ...
- Hibernate检索方式 分类: SSH框架 2015-07-10 22:10 4人阅读 评论(0) 收藏
我们在项目应用中对数据进行最多的操作就是查询,数据的查询在所有ORM框架中也占有极其重要的地位.那么,如何利用Hibernate查询数据呢?Hibernate为我们提供了多种数据查询的方式,又称为Hi ...
- iOS自定义字体及类目 分类: ios技术 2015-05-15 16:34 195人阅读 评论(0) 收藏
1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...
- 全方位分析Objcetive-C Runtime 分类: ios技术 2015-03-11 22:29 77人阅读 评论(0) 收藏
本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 简介 与Runtime交互 ...
- IOS之富文本编辑 分类: ios技术 2015-03-06 22:51 89人阅读 评论(0) 收藏
之前做项目时遇到一个问题: 使用UITextView显示一段电影的简介,由于字数比较多,所以字体设置的很小,行间距和段间距也很小,一大段文字挤在一起看起来很别扭,想要把行间距调大,结 ...
- 【从0到1学Web前端】CSS伪类和伪元素 分类: HTML+CSS 2015-06-02 22:29 1065人阅读 评论(0) 收藏
1.CSS中的伪类 CSS 伪类用于向某些选择器添加特殊的效果. 语法: selector : pseudo-class {property: value} CSS 类也可与伪类搭配使用 select ...
- CSS_Spirte实现原理 分类: HTML+CSS 2015-04-28 22:58 531人阅读 评论(0) 收藏
CSS Spirte就是所谓的把很多的小图标合并成一张大的图片,然后使用CSS的background-position属性,来动态的定位自己需要图标的位置.这样做的目的主要是减少HTTP请求,加快网页 ...
随机推荐
- Gray Code 解答
Question The gray code is a binary numeral system where two successive values differ in only one bit ...
- hdu4536-XCOM Enemy Unknown(爆搜)
XCOM-Enemy Unknown是一款很好玩很经典的策略游戏. 在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选择3 ...
- POJ 1742 Coins(多重背包) DP
参考:http://www.hankcs.com/program/cpp/poj-1742-coins.html 题意:给你n种面值的硬币,面值为a1...an,数量分别为c1...cn,求问,在这些 ...
- RCU 机制 [转IBM]
2005 年 7 月 01 日 本文详细地介绍了 Linux 2.6 内核中新的锁机制 RCU(Read-Copy Update) 的实现机制,使用要求与典型应用. 一.引言 众所周知,为了保护共享数 ...
- handsontable的单元格操作方法
1.为handsontable添加钩子方法 addHook(key,callback):key为钩子方法名 <span style="font-size:18px;"> ...
- [SASS] Make a responsive arrow box
Check the page:http://www.cssarrowplease.com/ In HTML: {{type}} is tow way binding in Angular, three ...
- vs2013中国集
在TOOLS的菜单条下的最后一项.进去后在输入框输入Language.按Enter.选择语言,然后确定就可以. 如图 假设没有点击下拉框底下的链接 就会调挑转到语言包下载界面 下载须要的语言就可以, ...
- EasyUI 1.3.1以下的组合验证
适用于EasyUI 1.3.1以下的, 1.3.2已经自带组合验证(如validType:['validator1','validator2']) $.extend($.fn.validatebox. ...
- Chrome开发者工具详解(1):Elements、Console、Sources面板
Chrome开发者工具面板 面板上包含了Elements面板.Console面板.Sources面板.Network面板. Timeline面板.Profiles面板.Application面板.Se ...
- ASP.NET中多个相同name的控件在后台正确取值
有兽, 页面上可能有多个相同name的Html表单控件, 一般在后台使用Request.Form[“name”]取值,并用‘,’分隔. 但是当值中包含逗号时, 取值就会出现异常, ...