首页 > 代码库 > HDU 3567 Eight II
HDU 3567 Eight II
Eight II
Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 356764-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.
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.
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.
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
Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
解题:经典的八数码。。。。。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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 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 }
HDU 3567 Eight II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。