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 ...
随机推荐
- [2012-08-06]awk多文件合并并按文件名分段
以下代码满足这样的需求: 多个文件内容合并到一个文件A中(如果没有下面这条,使用cat就能解决) 文件A中每段内容之前保留原先的文件名 awk 'tmp!=FILENAME{tmp=FILENAME; ...
- 前端开发【第一篇:HTML】
HTML初识 1.什么是HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都 ...
- github+hexo搭建自己的博客网站(七)注意事项(避免read.me,CNAME文件的覆盖,手动改github page的域名)
详细的可以查看hexo博客的演示:https://saucxs.github.io/ 绑定域名可以查看:http://www.chengxinsong.cn 可以查看在github上生成的静态文件(如 ...
- 设置input的placeholder样式
自定义input默认placeholder样式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 inpu ...
- 通过编译lambda表达式来创建实例(可在反射时候用,效率比反射高一些)
原文地址:https://rogerjohansson.blog/2008/02/28/linq-expressions-creating-objects/ 据说编译lambda创建实例是比反射快.实 ...
- main方法快速编辑日历
public static void main(String[] args) { Scanner input=new Scanner (System.in); System.out.println(& ...
- tkinter第一章
tk1 ------------------------------------------------------------------------------------------ impor ...
- 团队作业8----第二次项目冲刺(beta阶段)5.25
Day7-05.25 1.每日会议 会议内容: 1.今日对整个项目进行了一个总结. 2.讨论了这次项目中的不足和每个人的贡献. 讨论照片:拍摄者 周迪 2.任务分配情况: 每个人的工作分配表: 队员 ...
- Sudoku Generator
Sudoku 算法 标签(空格分隔): 软工实践 设想:通过第一行,来生成2, 3行的排列,再通过1 - 3 生成4 - 6排列. 2 3 行的生成由上一行生成 公式为$grid[i][j] = gr ...
- 201521123072《Java程序设计》第6周学习总结
201521123072<Java程序设计>第6周学习总结 标签(空格分隔): java 1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画 ...