HDU 3567 Eight II

本文介绍了一个使用IDA*算法解决经典八数码问题的方法。通过输入两个状态序列,算法找到从初始状态到目标状态的最短操作序列,并确保输出的序列是字典序最小的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Eight II

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on  HDU. Original ID: 3567
64-bit integer IO format: %I64d      Java class name: Main
 
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

Source

 
解题:经典的八数码。。。。。IDA*大法好。。。。。。注意输出字典序最小的
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 3;
18 struct sta{
19     int x,y;
20     sta(int a = 0,int b = 0){
21         x = a;
22         y = b;
23     }
24 };
25 char mp[maxn][maxn];
26 sta s[11];
27 int nowx,nowy;
28 int h(){
29     int tmp = 0;
30     for(int i = 0; i < 9; i++){
31         int x = i/3,y = i%3;
32         if(mp[x][y] == 'X') continue;
33         tmp += abs(x - s[mp[x][y]-'0'].x) + abs(y - s[mp[x][y]-'0'].y);
34     }
35     return tmp;
36 }
37 int ans[200],limit;
38 const int dir[4][2] = {1,0,0,-1,0,1,-1,0};
39 const char d[4] = {'d','l','r','u'};
40 bool ok;
41 int IDAstar(int x,int y,int p,int cur){
42     int bound = INF,tmp;
43     int hv = h();
44     if(cur + hv > limit) return cur + hv;
45     if(hv == 0) {ok = true;return cur;}
46     for(int i = 0; i < 4; i++){
47         if(i == p) continue;
48         int tx = x + dir[i][0];
49         int ty = y + dir[i][1];
50         if(tx < 0 || tx >= 3 || ty < 0 || ty >= 3) continue;
51         swap(mp[x][y],mp[tx][ty]);
52         ans[cur] = i;
53         int nbound = IDAstar(tx,ty,3-i,cur+1);
54         if(ok) return nbound;
55         bound = min(bound,nbound);
56         swap(mp[x][y],mp[tx][ty]);
57     }
58     return bound;
59 }
60 int main() {
61     int t,cs = 1;
62     char ch;
63     scanf("%d",&t);
64     getchar();
65     while(t--){
66         for(int i = 0; i < 9; i++){
67             ch = getchar();
68             if(ch == 'X'){
69                 nowx = i/3;
70                 nowy = i%3;
71             }
72             mp[i/3][i%3] = ch;
73         }
74         getchar();
75         for(int i = 0; i < 9; i++){
76             ch = getchar();
77             if(ch == 'X') continue;
78             s[ch-'0'] = sta(i/3,i%3);
79         }
80         getchar();
81         limit = h();
82         ok = false;
83         while(!ok) limit = IDAstar(nowx,nowy,-10,0);
84         printf("Case %d: %d\n",cs++,limit);
85         for(int i = 0; i < limit; i++)
86             putchar(d[ans[i]]);
87         putchar('\n');
88     }
89     return 0;
90 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4003439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值