HDU3567 Eight II —— IDA*算法

来源:互联网 发布:java登录界面与数据库 编辑:程序博客网 时间:2024/05/16 08:28

题目链接: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
212X45378612345678X564178X237568X4123
 

Sample Output
Case 1: 2ddCase 2: 8urrulldr
 

Author
zhymaoiing
 

Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC




题解:

POJ1077 的强化版。

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

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


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



未加vis判重(202MS):

2017-09-10 10:25:57Accepted3567202MS1712K

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <set>#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 1e6+10;//M为棋盘, pos_goal为目标状态的每个数字所在的位置, pos_goal[dig] = pos,//即表明:在目标状态中,dig所在的位置为pos。pos_goal与M为两个互逆的数组。int M[MAXN], pos_goal[MAXN];int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};int dir[4][2] = { 1,0, 0,-1, 0,1, -1,0 };char op[4] = {'d', 'l', 'r', 'u'  };int cantor(int s[])  //获得哈希函数值{    int sum = 0;    for(int i = 0; i<9; i++)    {        int num = 0;        for(int j = i+1; j<9; j++)            if(s[j]<s[i]) num++;        sum += num*fac[8-i];    }    return sum+1;}int dis_h(int s[])  //获得曼哈顿距离{    int dis = 0;    for(int i = 0; i<9; i++)    if(s[i]!=9)    {        int x = i/3, y = i%3;        int xx = pos_goal[s[i]]/3, yy = pos_goal[s[i]]%3;   //此处须注意        dis += abs(x-xx) + abs(y-yy);    }    return dis;}char path[100];int kase,  nextd;bool IDAstar(int loc, int depth, int pre, int limit){    int h = dis_h(M);    if(depth+h>limit)    {        nextd = min(nextd, depth+h);        return false;    }    if(h==0)    {        path[depth] = '\0';        printf("Case %d: %d\n", kase, depth);        puts(path);        return true;    }    int x = loc/3;    int y = loc%3;    for(int i = 0; i<4; i++)    {        if(i+pre==3) continue;  //方向与上一步相反, 剪枝        int xx = x + dir[i][0];        int yy = y + dir[i][1];        if(xx>=0 && xx<=2 && yy>=0 && yy<=2)        {            int tmploc = xx*3+yy;            swap(M[loc], M[tmploc]);            path[depth] = op[i];            if(IDAstar(xx*3+yy, depth+1, i, limit))                return true;            swap(M[loc], M[xx*3+yy]);        }    }    return false;}int main(){    int T;    char str[50];    scanf("%d",&T);    for(kase = 1; kase<=T; kase++)    {        int loc;        scanf("%s", str);        for(int i = 0; i<9; i++)        {            if(str[i]=='X') M[i] = 9, loc = i;            else  M[i] = str[i]-'0';        }        scanf("%s", str);        for(int i = 0; i<9; i++)        {            if(str[i]=='X') pos_goal[9] = i;            else pos_goal[str[i]-'0'] = i;        }        for(int limit = dis_h(M); ; limit = nextd)     //迭代加深搜        {            nextd = INF;            if(IDAstar(loc, 0, INF, limit))                break;        }    }}



加了vis判重(936MS)

2017-09-10 10:26:10Accepted3567936MS5620K

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <set>#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 1e6+10;int M[MAXN], pos_goal[MAXN];int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};int dir[4][2] = { 1,0, 0,-1, 0,1, -1,0 };char op[4] = {'d', 'l', 'r', 'u'  };int cantor(int s[])  //获得哈希函数值{    int sum = 0;    for(int i = 0; i<9; i++)    {        int num = 0;        for(int j = i+1; j<9; j++)            if(s[j]<s[i]) num++;        sum += num*fac[8-i];    }    return sum+1;}int dis_h(int s[])  //获得曼哈顿距离{    int dis = 0;    for(int i = 0; i<9; i++)    if(s[i]!=9)    {        int x = i/3, y = i%3;        int xx = pos_goal[s[i]]/3, yy = pos_goal[s[i]]%3;        dis += abs(x-xx) + abs(y-yy);    }    return dis;}char path[100];int kase,  nextd, vis[MAXN];bool IDAstar(int loc, int depth, int pre, int limit){    int h = dis_h(M);    if(depth+h>limit)    {        nextd = min(nextd, depth+h);        return false;    }    if(h==0)    {        path[depth] = '\0';        printf("Case %d: %d\n", kase, depth);        puts(path);        return true;    }    int x = loc/3;    int y = loc%3;    for(int i = 0; i<4; i++)    {        if(i+pre==3) continue;  //方向与上一步相反, 剪枝        int xx = x + dir[i][0];        int yy = y + dir[i][1];        if(xx>=0 && xx<=2 && yy>=0 && yy<=2)        {            int tmploc = xx*3+yy;            swap(M[loc], M[tmploc]);            int status = cantor(M);            if(!vis[status])            {                vis[status] = 1;                path[depth] = op[i];                if(IDAstar(xx*3+yy, depth+1, i, limit))                    return true;                vis[status] = 0;            }            swap(M[loc], M[xx*3+yy]);        }    }    return false;}int main(){    int T;    char str[50];    scanf("%d",&T);    for(kase = 1; kase<=T; kase++)    {        int loc;        scanf("%s", str);        for(int i = 0; i<9; i++)        {            if(str[i]=='X') M[i] = 9, loc = i;            else  M[i] = str[i]-'0';        }        scanf("%s", str);        for(int i = 0; i<9; i++)        {            if(str[i]=='X') pos_goal[9] = i;            else pos_goal[str[i]-'0'] = i;        }        vis[cantor(M)] = 1;        for(int limit = dis_h(M); ; limit = nextd)     //迭代加深搜        {            nextd = INF;            ms(vis,0);            if(IDAstar(loc, 0, INF, limit))                break;        }    }}


原创粉丝点击