hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043
http://poj.org/problem?id=1077
Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9173 Accepted Submission(s): 2473 Special Judge
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.
1 2 3
is described by this list:
1 2 3 x 4 6 7 5 8
{1,2,3,4,...,n}表示1,2,3,...,n的排列如 {1,2,3} 按从小到大排列一共6个。123 132 213 231 312 321 。
代表的数字 1 2 3 4 5 6 也就是把10进制数与一个排列对应起来。
他们间的对应关系可由康托展开来找到。
如我想知道321是{1,2,3}中第几个大的数可以这样考虑 :
第一位是3,当第一位的数小于3时,那排列数小于321 如 123、 213 ,小于3的数有1、2 。所以有2*2!个。再看小于第二位2的:小于2的数只有一个就是1 ,
所以有1*1!=1 所以小于321的{1,2,3}排列数有2*2!+1*1!=5个。所以321是第6个大的数。 2*2!+1*1!+0*0!就是康托展开。
再举个例子:1324是{1,2,3,4}排列数中第几个大的数:第一位是1小于1的数没有,是0个 0*3! 第二位是3小于3的数有1和2,但1已经在第一位了,
所以只有一个数2 1*2! 。第三位是2小于2的数是1,但1在第一位,所以有0个数 0*1! ,所以比1324小的排列有0*3!+1*2!+0*1!=2个,1324是第三个大数。
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h> using namespace std;
#define N 363000 struct Nod
{
int b[];
int x,y,pos;
}nd1,nd2; int fac[] = {,,,,,,,,,}; //康拓展开用到的数组
//康托展开:
int cantor(int* a, int k)
{
int i, j, tmp, num = ;
for (i = ; i < k; i++) {
tmp = ;
for (j = i + ; j < k; j++)
if (a[j] < a[i])
tmp++;
num += fac[k - i - ] * tmp;
}
return num;
} int mark[N],pre[N];
char dir[N];
int cx[]={,,,-};
int cy[]={-,,,}; void exchange(int *a,int x,int y)
{
int temp=a[x];
a[x]=a[y];
a[y]=temp;
} void bfs(int *b,int x,int y)
{
queue<Nod> q;
memset(mark,,sizeof(mark));
memset(pre,-,sizeof(pre));
nd1.x=x;
nd1.y=y;
memcpy(nd1.b,b,sizeof(int)*);
int i,temp;
temp = cantor(b,);
mark[temp] = ;
nd1.pos = temp;
q.push(nd1);
while(!q.empty())
{
nd2 = q.front();
q.pop();
for(i=;i<;i++)
{
nd1.x = nd2.x + cx[i];
nd1.y = nd2.y + cy[i];
if(nd1.x>=&&nd1.x<&&nd1.y>=&&nd1.y<)
{
memcpy(nd1.b,nd2.b,sizeof(int)*);
exchange(nd1.b,nd1.x*+nd1.y,nd2.x*+nd2.y);
temp = cantor(nd1.b,);
nd1.pos = temp;
if(mark[temp]==) continue;
mark[temp] = ;
pre[temp] = nd2.pos;
if(cx[i]==)
{
if(cy[i]==) dir[temp] = 'l'; //因为是从目标状态开始搜索的,最后需要倒序输出,然后方向也应该是相反输出
else dir[temp] = 'r'; //dir[temp] = 'l' 的反方向
}
if(cy[i]==)
{
if(cx[i]==) dir[temp] = 'u'; //同上
else dir[temp] = 'd'; //同上
}
q.push(nd1);
}
}
}
} int main()
{
char str[];
int a[],b[]={,,,,,,,,}; //末状态,从末状态开始搜索
bfs(b,,); //预先搜索所有状态
while(~scanf("%s",str))
{
if(str[]=='x') a[]=;
else a[]=str[]-'';
int i;
for(i=;i<;i++)
{
scanf("%s",str);
if(str[]=='x') a[i]=;
else a[i]=str[]-'';
}
int temp = cantor(a,); //得到起始状态,然后回溯到末状态
if(!mark[temp]) //所有状态里面若没有起始状态,则为不可达
{
printf("unsolvable\n");
continue;
}
while(temp) //回溯过程
{
printf("%c",dir[temp]);
temp = pre[temp];
}
putchar();
}
return ;
}
hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)的更多相关文章
- HDU - 1043 - Eight / POJ - 1077 - Eight
先上题目: Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 【HDU - 1043】Eight(反向bfs+康托展开)
Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8 在上图中,由于右下角位置是空的 ...
- 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 ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 4531 bfs/康拓展开
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...
- bnuoj 1071 拼图++(BFS+康拓展开)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...
- 九宫重拍(bfs + 康拓展开)
问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12 ...
- 8数码,欺我太甚!<bfs+康拓展开>
不多述,直接上代码,至于康拓展开,以前的文章里有 #include<iostream> #include<cstdio> #include<queue> using ...
随机推荐
- 通过pod导入第三方框架
项目导入第三方框架的时候,如果直接拖到项目中的话还需要去修改某些设置以及导入头文件等,当项目比较大的时候这个方法就比较笨拙了,我们可以通过pod来简化这写流程: 在使用pod之前我们需要安装Cocoa ...
- 网站压力测试工具webbench 安装与使用
webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好用,安装使用也特别方便,并且非常小. 主要是 -t 参数用着比较爽,下面参考了张宴的文章 ...
- mysql 数据备份还原
悲剧的一天,不小心将数据库删了... 命令行备份数据库 1.mysqldump命令进行备份.该命令将连接MySQL服务器并创建SQL转储文件,该文件包含了重新创建数据库所必需的所有SQL语句.该命令的 ...
- Unit Testing PowerShell Code with Pester
Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerS ...
- C语言位运算符:与、或、异或、取反,左移和右移
C语言位运算符:与.或.异或.取反.左移和右移 个位操作运算符.这些运算符只能用于整型操作数,即只能用于带符号或无符号的char,short,int与long类型. ,则该位的结果值为1,否则为0 | ...
- (转)战斗bug技巧全攻略
原文地址:http://www.cnblogs.com/manuosex/p/3736077.html 程序员不是有一幅这样的对联吗 上联:一个项目两部电脑三餐盒饭只为四千工资搞得五脏俱损六神无主仍然 ...
- 【转】The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
[转]The content of element type "configuration" must match "(properties?,settings?,typ ...
- CS0016: 未能写入输出文件“c:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\data\34aae060\b7daa87d\App_Web_addadvice.aspx.cdcab7d2.ekhlcbjd.dll”--“目录名无效。 ”
产生原因: 应用程序运行时产生的临时文件需要存放到c:"windows"temp 文件夹下 而运行基于microsoft .net framework 框架下的应用程序 需要对te ...
- 第二十七篇、使用MVVM布局页面
思路:架构的设计模式主要有这么两种 >MVC :这种方式用得很多,也很是常见,不在过多的介绍 >MVVM:使用这种 常常需要导入第三方框架,常见的是响应式框架 >主要讲一下ViewM ...
- 10_控制线程_线程让步yield
[线程让步yield()方法] yield()方法可以让当前正在执行的线程暂停,但它不会阻塞该线程,它只是将该线程从运行状态转入就绪状态. 只是让当前的线程暂停一下,让系统的线程调度器重新调度一次. ...