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请求,加快网页 ...
随机推荐
- 用Less循环生成样式
需求是这样的,我要给一个轮播图设置不同的背景图,由于每张图片的背景图路劲都不一样,所以需要对每个单独的元素自定义图片路径.然后想到Less语法有mixin机制,就这样实现了一个递归function,然 ...
- 国内ip信息库的组建
1.从 APNIC 分析得到国内的段 数据源位置:http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest 2.从QQ纯真库分析得到国 ...
- JAVA读、写EXCEL文件
採用jxl.jar包,网上下载,百度一下到出都是.希望能够帮助到大家. 接下来直接贴代码: <span style="font-size:18px;"> public ...
- RDBMS 数据库补丁集补丁号码高速參考-文档 ID 1577380.1
保存此文,高速查询补丁号 Oracle Database - Enterprise Edition - 版本号 8.1.7.0 和更高版本号 本文档所含信息适用于全部平台 补丁集/PSU 补丁号码 ...
- fragment的入门DEMO
效果图: 代码如下: 1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLay ...
- Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6786239 Android 系统的运行时库层代 ...
- 利用Python进行数据分析——数据规整化:清理、转换、合并、重塑(七)(1)
数据分析和建模方面的大量编程工作都是用在数据准备上的:载入.清理.转换以及重塑.有时候,存放在文件或数据库中的数据并不能满足你的数据处理应用的要求.很多人都选择使用通用编程语言(如Python.Per ...
- 函数nvl,nvl2,nullif,coalesce
NVL: Converts a null value to an actual valueNVL2:If expr1 is not null, NVL2 returns expr2. If expr1 ...
- Android学习笔记--服务(Service)
1.服务概述 1.服务是Android四大组件之一,在使用上可以分为本地服务和远程服务,本地服务是指在不影响用户操作的情况下在后台默默的执行一个耗时操作,例如下载,音频播放等.远程服务是指可以供其他应 ...
- js简单排序
简单的排序功能 HTML代码: <body> <input id="btn1" type="button" value="排序&qu ...