zoukankan      html  css  js  c++  java
  • HDU 3567 Eight II

    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
    ",cs++,limit);
    85         for(int i = 0; i < limit; i++)
    86             putchar(d[ans[i]]);
    87         putchar('
    ');
    88     }
    89     return 0;
    90 }
    View Code
  • 相关阅读:
    Java最近版本新特性使用介绍
    Android开发指南-框架主题-安全和许可
    Android启动组件的三种主流及若干非主流方式
    Android自定义权限和使用权限
    Android 安全架构及权限控制机制剖析
    物联网操作系统Hello China V1.76(PC串口版)版本发布
    android 点九PNG技术 适应不同分辨率 完美显示效果
    mysl lock table read
    mysl lock table read
    mysql DBI 事务控制
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4003439.html
Copyright © 2011-2022 走看看