hdu 1043 A*
http://www.cnblogs.com/183zyz/archive/2011/08/12/2135827.html
#include<stdio.h>
#define N 363000
#include<string.h>
#include<queue>
using namespace std;
int visit[N];
char visit1[N];
int pre[N],st,a[10],end;
int dir[9]={1,1,2,6,24,120,720,5040,40320};
struct node {
int ma[10];//记录状态
int ans1;//记录状态值
int x,f,g;
bool operator <(const node &a)const {
return a.f<f;
}
};
int hash(int s[]) {//康拓展开逆序状态值
int i,j,cnt,sum;
sum=0;
for(i=1;i<=9;i++) {
cnt=0;
for(j=1;j<i;j++)
if(s[j]>s[i])cnt++;
sum+=cnt*dir[i-1];
}
return sum;
}
int ABS(int x) {
return x<0?(-x):x;
}
int h(int s[]) {//不算x时的曼哈顿距离
int curx,cury,endx,endy,sum,i,ans;
sum=0;
for(i=1;i<=9;i++) {
if(s[i]==9)continue;
ans=s[i];
curx=(i+2)/3;//算出当前的行
cury=(i-1)%3+1;//算出当前的列
endx=(ans+2)/3;//算出当前位置的值应该在的行
endy=(ans-1)%3+1;//算出当前位置的值应该在的列
sum=sum+ABS(curx-endx)+ABS(cury-endy);//算出距离
}
return sum;
}
void bfs() {
int ans,i;
priority_queue<node>q;
node cur,next;
cur.ans1=st=hash(a);//康拓展开查找状态
visit[cur.ans1]=1;
if(st==end)
return;
for(i=1;i<=9;i++) {//将当前的状态保存下来
cur.ma[i]=a[i];
if(a[i]==9)
cur.x=i;
}//记录当前的状态
cur.g=0;//记录深度
cur.f=h(a);//记录当前的f值
q.push(cur);//入列
while(!q.empty()) {
cur=q.top();//比较f值的大小,选取小的
q.pop();
if((cur.x+2)/3!=1) {//向上翻
next=cur;
next.x=cur.x-3;//翻转后的位置
next.ma[cur.x]=next.ma[next.x];//将上方的值存到空格中
next.ma[next.x]=9;//将上方的值置为空格即‘9’;
ans=hash(next.ma);//记录翻后的剩余状态
if(!visit[ans]) {//判断翻转后的状态是否存在
next.g++;
next.f=next.g+h(next.ma);////估价函数
visit[ans]=1;//表示状态值存在
next.ans1=ans;//将下次状态值记录下来
pre[ans]=cur.ans1;//将当前状态值记录下来
visit1[ans]='u';//记录
if(ans==end)return;
q.push(next);
}
}
if((cur.x+2)/3!=3) {//向下翻
next=cur;
next.x=cur.x+3;
next.ma[cur.x]=next.ma[next.x];
next.ma[next.x]=9;
ans=hash(next.ma);
if(!visit[ans]) {
next.g++;
next.f=next.g+h(next.ma);
visit[ans]=1;
next.ans1=ans;
pre[ans]=cur.ans1;
visit1[ans]='d';
if(ans==end) return;
q.push(next);
}
}
if(cur.x%3!=1) {//向左翻
next=cur;
next.x=cur.x-1;
next.ma[cur.x]=next.ma[next.x];
next.ma[next.x]=9;
ans=hash(next.ma);
if(!visit[ans]) {
next.g++;
next.f=next.g+h(next.ma);
visit[ans]=1;
next.ans1=ans;
pre[ans]=cur.ans1;
visit1[ans]='l';
if(ans==end)return;
q.push(next);
}
}
if(cur.x%3!=0) {//向右翻
next=cur;
next.x=cur.x+1;
next.ma[cur.x]=next.ma[next.x];
next.ma[next.x]=9;
ans=hash(next.ma);
if(!visit[ans]) {
next.g++;
next.f=next.g+h(next.ma);
visit[ans]=1;
next.ans1=ans;
pre[ans]=cur.ans1;
visit1[ans]='r';
if(ans==end)return;
q.push(next);
}
}
}
}
int check(int s[]) {//剪枝判是否有解
int i,j,cnt=0;
for(i=1;i<=9;i++) {
if(s[i]==9)
continue;
for(j=1;j<i;j++) {
if(s[j]==9)
continue;
if(s[j]>s[i])
cnt++;
}
}
return cnt ;
}
int main() {
int i,j,ans;
char str[50];
while(gets(str)) {
ans=0;
memset(visit,0,sizeof(visit));
for(i=0;str[i];i++) {
if(str[i]=='x')
a[++ans]=9;
else
if(str[i]!=' ')
a[++ans]=str[i]-'0';
}//处理数据
end=0;
ans=check(a);//判断是否有解,即ans值必须是偶数才有结果
if(ans%2) {
printf("unsolvable\n");
continue;
}
bfs();
j=0;
while(end!=st) {//找路径及处理方法
str[j++]=visit1[end];
end=pre[end];
}
for(i=j-1;i>=0;i--)//输出
printf("%c",str[i]);
printf("\n");
}
return 0;
}
hdu 1043 A*的更多相关文章
- POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)
思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...
- HDU 1043 Eight(八数码)
HDU 1043 Eight(八数码) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Descr ...
- Eight POJ - 1077 HDU - 1043 八数码
Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...
- 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 八数码问题 A*算法(经典问题)
HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...
- POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3
http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...
- hdu 1043 Eight 经典八数码问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...
- 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: 1000 ...
- HDU 1043 Eight(双向BFS+康托展开)
http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...
- HDU 1043 Eight(反向BFS+打表+康托展开)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 题目大意:传统八数码问题 解题思路:就是从“12345678x”这个终点状态开始反向BFS,将各 ...
随机推荐
- [转]VC++中对文件的写入和读取
本文转自:http://blog.csdn.net/fanghb_1984/article/details/7425705 本文介绍两种方法对文件进行读取和写入操作:1.采用fstream类:2.采用 ...
- AJPFX总结List的三个子类的特点
ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高. ...
- 洛谷P3773 [CTSC2017]吉夫特(Lucas定理,dp)
题意 满足$b_1 < b_2 < \dots < b_k$且$a_{b_1} \geqslant a_{b_2} \geqslant \dots \geqslant a_{b_k} ...
- [BZOJ1045][HAOI2008]糖果传递 数学
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1045 我们假设每一个小朋友的代价为$x[i]$,每一次都从前面一个小朋友那里拿,这种贪心跟 ...
- 安卓&IOS 手机添加O365 邮箱账户
手机添加O365 邮件账户 一.Android手机添加O365邮件账户 1. 找到手机上“电子邮件” 2. 打开设置 3. 点击添加账户 4. 选择“Exchange” 5. 输入O365的邮箱账户和 ...
- 盘点那些年,被Oracle收购的公司
微博上看到一图,很清晰.盘点那些年,被Oracle收购的公司,Oracle日益强大,都收购了哪些公司呢?别再以为只有Sun啦...看看你都知道哪些? ps:Strategic Acquisitions ...
- 固定table表头
<style> #box{ height:214px; width:500px; overflow-y:auto;/** 必须,否则当表格数据过多时,不会产生滚动条,而是自动延长该div的 ...
- (转)关于treap的板子理解
关于treap的板子理解: 关于结构体的定义:(一般平衡树无法理解的变量名):v:节点的值:size:子节点的个数(包括自己):cnt:相同的值的副本数:l:左儿子:r:右儿子: 右旋:父亲变成左儿子 ...
- Mathematics-基础:1+2+3+……+n
设Sn=1+2+3+……+n-1+n则有Sn=n+n-1+……+3+2+1两式相加得2Sn=(n+1)+(n+1)+……+(n+1)2Sn=n×(n+1)Sn=n×(n+1)/2
- python爬虫---实现项目(二) 分析Ajax请求抓取数据
这次我们来继续深入爬虫数据,有些网页通过请求的html代码不能直接拿到数据,我们所需的数据是通过ajax渲染到页面上去的,这次我们来看看如何分析ajax 我们这次所使用的网络库还是上一节的Reques ...