A*模板

可以发现无论怎么动x逆序对奇偶性不变,可用这个性质判unsolve

状态判重可用康托展开

估价函数h(n)的求法为计算图上每一个点到目标的曼哈顿距离之和

  1 #include<cmath>
  2 #include<queue>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<algorithm>
  6 using namespace std;
  7 int n,m,cnt,tot,stp;
  8 int dx[4]={0,1,0,-1};
  9 int dy[4]={1,0,-1,0};
 10 int val[10]={0,1,1,2,6,24,120,720,5040,40320};
 11 char sc[12][3];
 12 char ans[500005];
 13 bool vis[500005];
 14 struct node{
 15     int posx;
 16     int sta[4][4];
 17     int hs,hn,gn,fn;
 18 }fr,ed;
 19 struct PRE{
 20     char w;
 21     int p;
 22 }pre[500005];
 23 bool check(node x){
 24     int tmp[10];int tp=0;int ret=0;
 25     for(int i=1;i<=3;i++){
 26         for(int j=1;j<=3;j++){
 27             tmp[++tp]=x.sta[i][j];
 28         }
 29     }
 30     for(int i=1;i<=9;i++){
 31         if(!tmp[i])continue;
 32         for(int j=i+1;j<=9;j++){
 33             if(!tmp[j])continue;
 34             if(tmp[j]<tmp[i])ret++;
 35         }
 36     }
 37     return (ret&1);
 38 }
 39 int gethash(node x){
 40     int tmp[10];int tp=0;int ret=0,c=0;
 41     for(int i=1;i<=3;i++){
 42         for(int j=1;j<=3;j++){
 43             tmp[++tp]=x.sta[i][j];
 44         }
 45     }
 46     for(int i=1;i<=9;c=0,i++){
 47         for(int j=1;j<i;j++){
 48             if(tmp[j]<tmp[i])c++;
 49         }
 50         ret+=c*val[i];
 51     }
 52     return ret;
 53 }
 54 int gethn(node x){
 55     int ret=0;
 56     for(int i=1;i<=3;i++){
 57         for(int j=1;j<=3;j++){
 58             if(x.sta[i][j])ret+=abs(i-((x.sta[i][j]-1)/3+1))+abs(j-((x.sta[i][j]-1)%3+1));
 59         }
 60     }
 61     return ret;
 62 }
 63 bool operator<(node a,node b){
 64     return a.fn>b.fn;
 65 }
 66 void astar(){
 67     priority_queue<node>que;
 68     que.push(fr);
 69     for(int i=0;i<=500000;i++){
 70         pre[i].p=-1;
 71     }
 72     memset(vis,false,sizeof(vis));
 73     vis[fr.hs]=true;
 74     while(!que.empty()){
 75         node u=que.top();
 76         que.pop();
 77         int x=(u.posx-1)/3+1;
 78         int y=(u.posx-1)%3+1;
 79         for(int i=0;i<4;i++){
 80             node v=u;
 81             int xx=x+dx[i];
 82             int yy=y+dy[i];
 83             if(xx<1||xx>3||yy<1||yy>3)continue;
 84             swap(v.sta[x][y],v.sta[xx][yy]);
 85             v.posx=(xx-1)*3+yy;
 86             v.hs=gethash(v);
 87             if(!vis[v.hs]){
 88                 v.gn=u.gn+1;
 89                 v.hn=gethn(v);
 90                 v.fn=v.hn+v.gn;
 91                 pre[v.hs].p=u.hs;
 92                 if(i==0)pre[v.hs].w='r';
 93                 if(i==1)pre[v.hs].w='d';
 94                 if(i==2)pre[v.hs].w='l';
 95                 if(i==3)pre[v.hs].w='u';
 96                 vis[v.hs]=true;
 97                 if(v.hs==stp){
 98                     return;
 99                 }
100                 que.push(v);
101             }
102         }
103     }
104 }
105 void print(){
106     int now=stp;tot=0;
107     while(pre[now].p!=-1){
108         ans[++tot]=pre[now].w;
109         now=pre[now].p;
110     }
111     for(int i=tot;i>=1;i--){
112         printf("%c",ans[i]);
113     }
114     printf("\n");
115 }
116 int main(){
117     while(scanf("%s",sc[1]+1)>0){
118         cnt=0;
119         for(int i=2;i<=9;i++)scanf("%s",sc[i]+1);
120         for(int i=1;i<=3;i++){
121             for(int j=1;j<=3;j++){
122                 fr.sta[i][j]=sc[++cnt][1]-'0';
123                 if(fr.sta[i][j]>8||fr.sta[i][j]<1){
124                     fr.posx=(i-1)*3+j;
125                     fr.sta[i][j]=0;
126                 }
127                 ed.sta[i][j]=(i-1)*3+j;
128                 ed.sta[3][3]=0;
129             }
130         }
131         stp=gethash(ed);
132         fr.fn=fr.hn=gethn(fr);
133         fr.hs=gethash(fr);
134         if(stp==fr.hs){
135             printf("\n");
136             continue;
137         }
138         if(check(fr)){
139             printf("unsolvable\n");
140             continue;
141         }
142         astar();print();
143     }
144     return 0;
145 }

转载于:https://www.cnblogs.com/lnxcj/p/10008254.html

HDU - 1043 Eight (A*搜索)相关推荐

  1. hdu 1043 Eight 经典八数码问题

    hdu 1043 Eight 经典八数码问题 题意描述:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 算法分析:经典的 ...

  2. HDU 1043 Eight(八数码)

    HDU 1043 Eight(八数码) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. Eight HDU - 1043

    Eight HDU - 1043 题意:给定一个3*3的方阵,要求通过交换x方格与其相邻方格位置的方式,使方阵上的数字由小到大排列,且x在右下角.输出具体交换步骤

  4. hdu 1043 Eight 搜索,哈希

    很早之前做过,总结一下康拓展开哈希大法.当初要是懂了这玩意北京网赛那题一定能出.... http://acm.hdu.edu.cn/showproblem.php?pid=1043题目链接 http: ...

  5. Eight HDU - 1043(八数码+搜索)

    题意: 就是还原八数码.输出操作. 题目: The 15-puzzle has been around for over 100 years; even if you don't know it by ...

  6. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  7. hdu 4722(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. 1 #include<iost ...

  8. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  9. hdu 1043 ,pku 1077 Eight ,八数码问题

    某位神牛曾说过,此题是涉及到人生完不完整的一道题.. Goodness大牛曾总结了 八数码的八重境界 : http://www.cnblogs.com/goodness/archive/2010/05 ...

最新文章

  1. rpm包安装apache发布多个虚拟主机
  2. 使用访问控制列表控制用户登录
  3. java幂等性原理_Java接口幂等性设计原理解析
  4. Python实现Adaboost
  5. Flask学习之基础知识与功能
  6. mysql 入门 jdbc
  7. 《微课实战:Camtasia Studio入门精要》——第2章 录制视频 2.1 录制视频基本常识...
  8. 老码农:我为什么建议程序员尽早进入大厂?
  9. 获得密钥_《哪吒》公映密钥延期一个月?关于“密钥延期”的全揭秘来了
  10. 行人和人脸识别数据集
  11. html图片轮播_前端轮播图怎么做?JavaScript来帮你轻松搞定
  12. 第三部份:glibc升级到glibc-2.30
  13. 21_nips_深度学习损失景观的嵌入原则
  14. 我的python学习之路
  15. APP的包名和签名获取工具
  16. 地下城英雄 java_英雄小组
  17. 好用的手机投屏电脑软件
  18. PS2接口协议及代码分析
  19. 选择手机群控系统有哪些需要注意的地方?
  20. SAP 基本单位与物料计量单位间的转换

热门文章

  1. 学习笔记之sed用法
  2. c语言main的性质,关于main()
  3. idea安装drl插件
  4. 【赠书活动】赠送清华社的《好好学Java:从零基础到项目实战》
  5. Jeecg-Boot 1.1 发布,基于 SpringBoot+Ant Design 的快速开发平台
  6. three.js 坐标系、camera位置属性、点、线、面
  7. 从 1 到完美,用 node 写一个命令行工具
  8. 纠错帖:Zuul Spring Cloud Gateway Linkerd性能对比
  9. onInterceptTouchEvent / onTouchEvent响应事件的详析
  10. zookeeper watcher机制