HDU3567 Eight II —— IDA*算法-程序员宅基地

技术标签: java  php  数据结构与算法  

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567


 

Eight II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 3420    Accepted Submission(s): 742

Problem Description
Eight-puzzle, which is also called "Nine grids", comes from an old game. 

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.

We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.



A state of the board can be represented by a string S using the rule showed below.



The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.
 

 

Input
The first line is T (T <= 200), which means the number of test cases of this problem.

The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
 

 

Output
For each test case two lines are expected.

The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
 

 

Sample Input
2 12X453786 12345678X 564178X23 7568X4123
 

 

Sample Output
Case 1: 2 dd Case 2: 8 urrulldr
 

 

Author
zhymaoiing
 

 

Source

 

 




题解:

POJ1077 的强化版。

问:为什么加了vis判重比不加vis判重还要慢?

答:因为当引入vis判重时,就需要知道棋盘的状态,而计算一次棋盘的状态,就需要增加(8+7+……1)次操作,结果得不偿失。


更新:其实IDA*算法不能加vis判重,因为IDA*的本质就是dfs, 根据dfs的特性, 第一次被访问所用的步数并不一定是最少步数,所以如果加了vis判重,就默认取了第一次被访问时所用的步数,而这个步数不一定是最优的。所以第二份代码是错误的,即使过了oj的数据。



未加vis判重(202MS):

2017-09-10 10:25:57 Accepted 3567 202MS 1712K
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 #define ms(a,b) memset((a),(b),sizeof((a)))
 13 using namespace std;
 14 typedef long long LL;
 15 const int INF = 2e9;
 16 const LL LNF = 9e18;
 17 const int MOD = 1e9+7;
 18 const int MAXN = 1e6+10;
 19 
 20 //M为棋盘, pos_goal为目标状态的每个数字所在的位置, pos_goal[dig] = pos,
 21 //即表明:在目标状态中,dig所在的位置为pos。pos_goal与M为两个互逆的数组。
 22 int M[MAXN], pos_goal[MAXN];
 23 
 24 int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};
 25 int dir[4][2] = { 1,0, 0,-1, 0,1, -1,0 };
 26 char op[4] = {
      'd', 'l', 'r', 'u'  };
 27 
 28 int cantor(int s[])  //获得哈希函数值
 29 {
 30     int sum = 0;
 31     for(int i = 0; i<9; i++)
 32     {
 33         int num = 0;
 34         for(int j = i+1; j<9; j++)
 35             if(s[j]<s[i]) num++;
 36         sum += num*fac[8-i];
 37     }
 38     return sum+1;
 39 }
 40 
 41 int dis_h(int s[])  //获得曼哈顿距离
 42 {
 43     int dis = 0;
 44     for(int i = 0; i<9; i++)
 45     if(s[i]!=9)
 46     {
 47         int x = i/3, y = i%3;
 48         int xx = pos_goal[s[i]]/3, yy = pos_goal[s[i]]%3;   //此处须注意
 49         dis += abs(x-xx) + abs(y-yy);
 50     }
 51     return dis;
 52 }
 53 
 54 char path[100];
 55 int kase,  nextd;
 56 bool IDAstar(int loc, int depth, int pre, int limit)
 57 {
 58     int h = dis_h(M);
 59     if(depth+h>limit)
 60     {
 61         nextd = min(nextd, depth+h);
 62         return false;
 63     }
 64 
 65     if(h==0)
 66     {
 67         path[depth] = '\0';
 68         printf("Case %d: %d\n", kase, depth);
 69         puts(path);
 70         return true;
 71     }
 72 
 73     int x = loc/3;
 74     int y = loc%3;
 75     for(int i = 0; i<4; i++)
 76     {
 77         if(i+pre==3) continue;  //方向与上一步相反, 剪枝
 78         int xx = x + dir[i][0];
 79         int yy = y + dir[i][1];
 80         if(xx>=0 && xx<=2 && yy>=0 && yy<=2)
 81         {
 82             int tmploc = xx*3+yy;
 83             swap(M[loc], M[tmploc]);
 84             path[depth] = op[i];
 85             if(IDAstar(xx*3+yy, depth+1, i, limit))
 86                 return true;
 87             swap(M[loc], M[xx*3+yy]);
 88         }
 89     }
 90     return false;
 91 }
 92 
 93 int main()
 94 {
 95     int T;
 96     char str[50];
 97     scanf("%d",&T);
 98     for(kase = 1; kase<=T; kase++)
 99     {
100         int loc;
101         scanf("%s", str);
102         for(int i = 0; i<9; i++)
103         {
104             if(str[i]=='X') M[i] = 9, loc = i;
105             else  M[i] = str[i]-'0';
106         }
107 
108         scanf("%s", str);
109         for(int i = 0; i<9; i++)
110         {
111             if(str[i]=='X') pos_goal[9] = i;
112             else pos_goal[str[i]-'0'] = i;
113         }
114 
115         for(int limit = dis_h(M); ; limit = nextd)     //迭代加深搜
116         {
117             nextd = INF;
118             if(IDAstar(loc, 0, INF, limit))
119                 break;
120         }
121     }
122 }
View Code

 


加了vis判重(936MS)

2017-09-10 10:26:10 Accepted 3567 936MS 5620K
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 #include <stack>
  9 #include <map>
 10 #include <string>
 11 #include <set>
 12 #define ms(a,b) memset((a),(b),sizeof((a)))
 13 using namespace std;
 14 typedef long long LL;
 15 const int INF = 2e9;
 16 const LL LNF = 9e18;
 17 const int MOD = 1e9+7;
 18 const int MAXN = 1e6+10;
 19 
 20 int M[MAXN], pos_goal[MAXN];
 21 
 22 int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};
 23 int dir[4][2] = { 1,0, 0,-1, 0,1, -1,0 };
 24 char op[4] = {
      'd', 'l', 'r', 'u'  };
 25 
 26 int cantor(int s[])  //获得哈希函数值
 27 {
 28     int sum = 0;
 29     for(int i = 0; i<9; i++)
 30     {
 31         int num = 0;
 32         for(int j = i+1; j<9; j++)
 33             if(s[j]<s[i]) num++;
 34         sum += num*fac[8-i];
 35     }
 36     return sum+1;
 37 }
 38 
 39 int dis_h(int s[])  //获得曼哈顿距离
 40 {
 41     int dis = 0;
 42     for(int i = 0; i<9; i++)
 43     if(s[i]!=9)
 44     {
 45         int x = i/3, y = i%3;
 46         int xx = pos_goal[s[i]]/3, yy = pos_goal[s[i]]%3;
 47         dis += abs(x-xx) + abs(y-yy);
 48     }
 49     return dis;
 50 }
 51 
 52 char path[100];
 53 int kase,  nextd, vis[MAXN];
 54 bool IDAstar(int loc, int depth, int pre, int limit)
 55 {
 56     int h = dis_h(M);
 57     if(depth+h>limit)
 58     {
 59         nextd = min(nextd, depth+h);
 60         return false;
 61     }
 62 
 63     if(h==0)
 64     {
 65         path[depth] = '\0';
 66         printf("Case %d: %d\n", kase, depth);
 67         puts(path);
 68         return true;
 69     }
 70 
 71     int x = loc/3;
 72     int y = loc%3;
 73     for(int i = 0; i<4; i++)
 74     {
 75         if(i+pre==3) continue;  //方向与上一步相反, 剪枝
 76         int xx = x + dir[i][0];
 77         int yy = y + dir[i][1];
 78         if(xx>=0 && xx<=2 && yy>=0 && yy<=2)
 79         {
 80             int tmploc = xx*3+yy;
 81             swap(M[loc], M[tmploc]);
 82             int status = cantor(M);
 83             if(!vis[status])
 84             {
 85                 vis[status] = 1;
 86                 path[depth] = op[i];
 87                 if(IDAstar(xx*3+yy, depth+1, i, limit))
 88                     return true;
 89                 vis[status] = 0;
 90             }
 91             swap(M[loc], M[xx*3+yy]);
 92         }
 93     }
 94     return false;
 95 }
 96 
 97 int main()
 98 {
 99     int T;
100     char str[50];
101     scanf("%d",&T);
102     for(kase = 1; kase<=T; kase++)
103     {
104         int loc;
105         scanf("%s", str);
106         for(int i = 0; i<9; i++)
107         {
108             if(str[i]=='X') M[i] = 9, loc = i;
109             else  M[i] = str[i]-'0';
110         }
111 
112         scanf("%s", str);
113         for(int i = 0; i<9; i++)
114         {
115             if(str[i]=='X') pos_goal[9] = i;
116             else pos_goal[str[i]-'0'] = i;
117         }
118 
119         vis[cantor(M)] = 1;
120         for(int limit = dis_h(M); ; limit = nextd)     //迭代加深搜
121         {
122             nextd = INF;
123             ms(vis,0);
124             if(IDAstar(loc, 0, INF, limit))
125                 break;
126         }
127     }
128 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538577.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30791095/article/details/98781135

智能推荐

使用nginx解决浏览器跨域问题_nginx不停的xhr-程序员宅基地

文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr

在 Oracle 中配置 extproc 以访问 ST_Geometry-程序员宅基地

文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc

Linux C++ gbk转为utf-8_linux c++ gbk->utf8-程序员宅基地

文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8

IMP-00009: 导出文件异常结束-程序员宅基地

文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束

python程序员需要深入掌握的技能_Python用数据说明程序员需要掌握的技能-程序员宅基地

文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求

Spring @Service生成bean名称的规则(当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致)_@service beanname-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname

随便推点

二叉树的各种创建方法_二叉树的建立-程序员宅基地

文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include&lt;stdio.h&gt;#include&lt;string.h&gt;#include&lt;stdlib.h&gt;#include&lt;malloc.h&gt;#include&lt;iostream&gt;#include&lt;stack&gt;#include&lt;queue&gt;using namespace std;typed_二叉树的建立

解决asp.net导出excel时中文文件名乱码_asp.net utf8 导出中文字符乱码-程序员宅基地

文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码

笔记-编译原理-实验一-词法分析器设计_对pl/0作以下修改扩充。增加单词-程序员宅基地

文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词

android adb shell 权限,android adb shell权限被拒绝-程序员宅基地

文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限

投影仪-相机标定_相机-投影仪标定-程序员宅基地

文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定

Wayland架构、渲染、硬件支持-程序员宅基地

文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland

推荐文章

热门文章

相关标签