zoukankan      html  css  js  c++  java
  • Eight II 初状态的转换&&bfs()预处理

    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
    ***************************************************************************************************************************
    初状态的转化&&&bfS()预处理
    ***************************************************************************************************************************
      1 #include<iostream>
      2 #include<cstring>
      3 #include<string>
      4 #include<cstdio>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<queue>
      8 using namespace std;
      9 
     10 struct S
     11 {
     12     char maze[3][3];
     13     int x,y;
     14     S(){}
     15     S(const char*str1)
     16     {
     17         for(int it=0,tx=0,ty=0;str1[it];it++)
     18         {
     19             maze[tx][ty]=str1[it];
     20             if(str1[it]=='X')
     21             {
     22                 x=tx;
     23                 y=ty;
     24             }
     25             ty++;
     26             if(ty==3)
     27             {
     28                 ty=0;
     29                 tx++;
     30             }
     31         }
     32     }
     33 };
     34 int pre[10][363000];
     35 char op[10][363000];
     36 const int fac[]={1,1,2,6,24,120,720,5040,40320};
     37 bool vis[363000];
     38 inline int inv_hash(S ts)//逆序对的hash函数
     39 {
     40     char str[10];
     41     int ans=0;
     42     for(int it=0;it<3;it++)
     43     {
     44         for(int jt=0;jt<3;jt++)
     45         {
     46             str[it*3+jt]=ts.maze[it][jt];
     47             int cnt=0;
     48             for(int kt=it*3+jt-1;kt>=0;kt--)
     49             {
     50                 if(str[kt]>str[it*3+jt])
     51                     cnt++;
     52             }
     53             ans+=fac[it*3+jt]*cnt;
     54         }
     55     }
     56     return ans;
     57 }
     58 S s;
     59 const int dir[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
     60 const char cdir[]="dlru";
     61 void bfs(int x)//每一个初状态下的bfs();
     62 {
     63     memset(pre[x],-1,sizeof(pre[x]));
     64     memset(vis,false,sizeof(vis));
     65     queue<S>Q;
     66     Q.push(s);
     67     vis[inv_hash(s)]=true;
     68     while(!Q.empty())
     69     {
     70         S u=Q.front();
     71         Q.pop();
     72         int ihu=inv_hash(u);
     73         for(int it=0;it<4;it++)
     74         {
     75             S v=u;
     76             v.x+=dir[it][0];
     77             v.y+=dir[it][1];
     78             if(v.x<0||v.x>=3||v.y<0||v.y>=3)
     79                 continue;
     80             v.maze[u.x][u.y]=v.maze[v.x][v.y];
     81             v.maze[v.x][v.y]='X';
     82             int ihv=inv_hash(v);
     83             if(vis[ihv])
     84                 continue;
     85             vis[ihv]=true;
     86             pre[x][ihv]=ihu;
     87             op[x][ihv]=cdir[it];
     88             Q.push(v);
     89         }
     90     }
     91 }
     92 
     93 char in[100];
     94 char stk[100];
     95 int cvr[10];
     96 int main()
     97 {
     98     //预处理
     99     s=S("X12345678");bfs(0);
    100     s=S("1X2345678");bfs(1);
    101     s=S("12X345678");bfs(2);
    102     s=S("123X45678");bfs(3);
    103     s=S("1234X5678");bfs(4);
    104     s=S("12345X678");bfs(5);
    105     s=S("123456X78");bfs(6);
    106     s=S("1234567X8");bfs(7);
    107     s=S("12345678X");bfs(8);
    108     int cas,T=0;
    109     scanf("%d",&cas);
    110     while(cas--)
    111     {
    112         int p;
    113         T++;
    114         scanf("%s",in);
    115         for(int i=0,j=0;in[i];i++)
    116         {
    117             if(in[i]!='X')
    118                cvr[in[i]-'0']=j++;//转换
    119             else
    120                 p=i;
    121         }
    122         scanf("%s",in);
    123         for(int i=0;in[i];i++)
    124         {
    125             if(in[i]=='X')continue;
    126             in[i]=cvr[in[i]-'0']+'1';
    127         }
    128         s=S(in);
    129         int ihs=inv_hash(s);
    130         //出状态的hash();
    131         int top=-1,tmp=ihs;
    132         while(tmp!=-1)
    133         {
    134             stk[++top]=op[p][tmp];
    135             tmp=pre[p][tmp];
    136         }
    137         printf("Case %d: %d
    ",T,top);
    138 while((--top)!=-1)
    139     {
    140              putchar(stk[top]);
    141     }
    142     puts("");
    143     }
    144    return 0;
    145 }
    View Code
  • 相关阅读:
    串口通信中接收数据时延迟处理与缓存处理的解决方案(C#)
    串口通讯接收数据的处理
    在C#程序设计中使用Win32类库
    C# Mutex对象学习经验
    我眼中的C# 3.0 Written by Allen Lee
    利用C#鼠标拖动TreeView节点
    richtextbox内文字自动滚动的例子
    在十六进制字符串与数值类型之间转换 C# 编程指南
    如何:指定符号位置和加载行为
    杂记20110321
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3452527.html
Copyright © 2011-2022 走看看