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 ...
随机推荐
- Retrofit2.0源码解析
欢迎访问我的个人博客 ,原文链接:http://wensibo.net/2017/09/05/retrofit/ ,未经允许不得转载! 今天是九月的第四天了,学校也正式开学,趁着大学最后一年的这大好时 ...
- ASP.NET Core 运行原理解剖[5]:Authentication
在现代应用程序中,认证已不再是简单的将用户凭证保存在浏览器中,而要适应多种场景,如App,WebAPI,第三方登录等等.在 ASP.NET 4.x 时代的Windows认证和Forms认证已无法满足现 ...
- c语言中的文件格式化读写函数fscanf和fprintf函数
很多时候我们需要写入数据到文件中时都觉得很困扰,因为格式乱七八槽的,可读性太差了,于是我们就想有没有什么函数可以格式化的从文件中输入和输出呢,还真有.下面我将讲解一下fscanf和fprintf的强大 ...
- CSS编码技巧
前面的话 本文将从DRY.currentColor.inherit和合理使用简写这几方面来详细介绍CSS编码技巧 DRY DRY,即don`t repeat yourself,尽量减少代码重复 在软件 ...
- MySQL(十一)之触发器
上一篇介绍的是比较简单的视图,其实用起来是相对比较简单的,以后有什么更多的关于视图的用法,到时候在自己补充.接下来让我们一起了解一下触发器的使用! 一.触发器概述 1.1.什么是触发器 触发器(Tri ...
- JTable用法-实例
前几篇文章介绍了JTable的基本用法,本文实现一个简单的JTable,算是前文的一个总结,并造福供拷贝党们. Swing-JTable用法-入门 Swing-JTable的渲染器与编辑器使用demo ...
- 201521123011 《java程序设计》 第7周学习总结
1. 本周学习总结 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 1.2 解释E remove(int index)源 ...
- 201521123103 《Java学习笔记》 第四周学习总结
一.本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. (1)多态性:相同形态,不同行为(不同的定义): (2)多态绑定:运行时能够自动地选择调用哪个 ...
- 201521123097《Java程序设计》第四周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 在本周的学习中,我知道了在类的定义里,还学习到了抽象类以及抽象方法的使用格式. 2. 书面作 ...
- 201521123028《Java程序设计》第4周学习总结
1. 本周学习总结 2. 书面作业 Q1.注释的应用 使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看. 对上周PTA的实验5-3中的矩形和圆形类做注释. Q2.面向对象 ...