Eight hdu 1043 八数码问题 双搜
Eight
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11226 Accepted Submission(s): 3013
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
x 4 6
7 5 8
is described by this list:
1 2 3 x 4 6 7 5 8
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
using namespace std;
typedef struct eight
{
char a[][];
int statu,nowx,nowy;
} eight;
eight s,e;
int f[],flag[],ok;
int father1[],father2[];
int move1[],move2[],last;
queue<eight>q1,q2;
void init()
{
f[]=;
int i;
for(i=; i<; i++)f[i]=f[i-]*i;
}
void work(eight &a)
{
int i,j,k=,ans=,t;
char b[];
for(i=; i<; i++)
{
for(j=; j<; j++)
{
if(a.a[i][j]=='x')a.nowx=i,a.nowy=j;
b[k++]=a.a[i][j];
}
}
k=;
for(i=; i<; i++)
{
t=;
for(j=i+; j<; j++)
if(b[j]<b[i])t++;
ans+=f[k--]*t;
}
a.statu=ans;
}
void copy(eight &a,eight &b)
{
int i,j;
for(i=; i<; i++)
for(j=; j<; j++)a.a[i][j]=b.a[i][j];
}
int w[][]= {{,},{,-},{,},{-,}};
void ex_BFS(eight &now,int n,int check)
{
int i,r,c;
eight in;
for(i=; i<; i++)
{ r=now.nowx+w[i][];
c=now.nowy+w[i][];
if(r>=&&r<&&c>=&&c<)
{
copy(in,now);
swap(in.a[r][c],in.a[now.nowx][now.nowy]);
work(in);
if(flag[in.statu]!=n)
{
if(n==)
{
q1.push(in);
father1[in.statu]=now.statu;
move1[in.statu]=i;
}
else
{
q2.push(in);
father2[in.statu]=now.statu;
move2[in.statu]=i;
}
if(flag[in.statu]==check)
{
ok=;
last=in.statu;
return ;
}
flag[in.statu]=n;
}
}
}
}
bool TBFS(eight &s,eight &e)
{
memset(flag,,sizeof(flag));
memset(move1,-,sizeof(move1));
memset(move2,-,sizeof(move2));
memset(father1,-,sizeof(father1));
memset(father2,-,sizeof(father2));
eight now;
ok=;
while(!q1.empty())q1.pop();
while(!q2.empty())q2.pop();
q1.push(s),flag[s.statu]=;
q2.push(e),flag[e.statu]=;
while((!q1.empty())||(!q2.empty()))
{
now=q1.front();
q1.pop();
ex_BFS(now,,);
if(ok)return ; now=q2.front();
q2.pop();
ex_BFS(now,,);
if(ok)return ;
}
return ;
}
bool check1(eight &s)
{
int i,j,k=;
char b[];
for(i=; i<; i++)
for(j=; j<; j++)
b[k++]=s.a[i][j];
int ans=;
for(i=; i<; i++)
{
if(b[i]!='x')
for(j=; j<i; j++)
if(b[j]!='x'&&b[i]<b[j])ans++;
}
return (ans&);
}
void printpath()
{
deque<char>q;
while(!q.empty())q.pop_back();
int now=last;
while(father1[now]!=-)
{
if(move1[now]==)q.push_back('r');
else if(move1[now]==)q.push_back('l');
else if(move1[now]==)q.push_back('d');
else if(move1[now]==)q.push_back('u');
now=father1[now];
}
while(!q.empty())
{
cout<<q.back();
q.pop_back();
} now=last;
while(father2[now]!=-)
{
if(move2[now]==)cout<<'l';
else if(move2[now]==)cout<<'r';
else if(move2[now]==)cout<<'u';
else if(move2[now]==)cout<<'d';
now=father2[now];
}
}
int main()
{
int i,j;
init();
char w;
while(cin>>w)
{
char an='';
for(i=; i<; i++)
for(j=; j<; j++)
{
if(i||j)
cin>>s.a[i][j];
e.a[i][j]=an++;
}
s.a[][]=w; e.a[][]='x';
if(check1(s))
{
cout<<"unsolvable"<<endl;
continue;
}
work(s);
work(e);
if(s.statu==e.statu)
{
cout<<endl;
continue;
}
if(TBFS(s,e))
{
printpath();
cout<<endl;
}
else
cout<<"unsolvable"<<endl;
} }
Eight hdu 1043 八数码问题 双搜的更多相关文章
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- HDU 1043 八数码(A*搜索)
在学习八数码A*搜索问题的时候须要知道下面几个点: Hash:利用康托展开进行hash 康托展开主要就是依据一个序列求这个序列是第几大的序列. A*搜索:这里的启示函数就用两点之间的曼哈顿距离进行计算 ...
- HDU 1043 八数码(八境界)
看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...
- HDU 1043 八数码问题的多种解法
一.思路很简单,搜索.对于每一种状态,利用康托展开编码成一个整数.于是,状态就可以记忆了. 二.在搜索之前,可以先做个优化,对于逆序数为奇数的序列,一定无解. 三.搜索方法有很多. 1.最普通的:深搜 ...
- HDU 1043 八数码 Eight A*算法
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu 1043 八数码问题
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- C++ 八数码问题宽搜
C++ 八数码问题宽搜 题目描述 样例输入 (none) 样例输出 H--F--A AC代码 #include <iostream> #include <stdio.h> #i ...
- hdu.1043.Eight (打表 || 双广 + 奇偶逆序)
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
随机推荐
- Git安装配置(完整版)
首先安装Windows客户端的git和TortoiseGit. 安装这两个软件还是蛮重要的,很多选项不能乱选. 为了写个完整的博客,我是装了又卸,卸了又装. 1.安装git 下载:https://gi ...
- 小而美的 React Form 组件
背景 之间在一篇介绍过 Table 组件< React 实现一个漂亮的 Table > 的文章中讲到过,在企业级后台产品中,用的最多且复杂的组件主要包括 Table.Form.Chart, ...
- Linq--一个集合中查找另一个集合,需熟悉这种写法
//获取科室与病区授权的护士信息 public List<SYS_ZGKSBQDYK> GetUserWardMapByWardCode(string wardCode) ...
- mysql:ip地址连接
2. 为用户授权 授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 2.1登录MYSQL(有ROOT权限),这里以ROO ...
- chrome开发工具指南(二)
Application 面板 使用 App Manifest 窗格检查您的网络应用清单和触发 Add to Homescreen 事件. 使用 Service Worker 窗格执行与服务工作线程相关 ...
- nginx.conf配置文件的简单说明
#nginx 监听原理 先监听端口 --> 再配置域名 -->匹配到就访问local 否则 没有匹配到域名就默认访问第一个监听端口的local地址# vi nginx.conf user ...
- Maven Scope取值的含义
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt257 maven依赖关系中Scope的作用 Dependency Scope ...
- ssh keys管理工具
原文地址:https://rtyan.github.io/%E5%B7%A5%E5%85%B7/2017/09/12/ssh-keys-manager.html 引言 我有两个github账户,一个是 ...
- 第3阶段——内核启动分析之创建si工程和分析stext启动内核函数(4)
目标: (1)创建Source Insight 工程,方便后面分析如何启动内核的 (2)分析uboot传递参数,链接脚本如何进入stext的 (3) 分析stext函数如何启动内核: (3.1) ...
- Angular+ionic2 web端 启动程序出现短暂 白屏或黑屏 的处理小妙招
在ionic2项目启动是会出现短暂的白屏或者黑屏,虽然很短,但是用户体验不太好.上网查了相关的资料,都是针对打包APP的解决办法,针对浏览器端使用的项目没有效果,所以自己写了样式,巧妙的避开这个问题, ...