zoukankan      html  css  js  c++  java
  • [未完待续](c++实现)八数码Ⅱ

    本题预计涉及:dfs搜索,康托展开(为了寻找映射来判重),曼哈顿距离

     

    Eight II

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)
    Total Submission(s): 8374    Accepted Submission(s): 1890


    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
     
    Author
    zhymaoiing
     
    Source
     
     
     
    --------时间分界线----2021.4.19----
    在初次应用了康托展开和dfs搜索后发现结果离答案还差一些(即输出的应是最短路径,而非寻找最短路径过程中走过的所有路径)
    今天肝不动了,先把初次的代码安排上┭┮﹏┭┮呜呜
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<queue>
      5 #define Judge(xx,yy) (xx>=0&&yy>=0&&xx<3&&yy<3)
      6 #define Maxn 362880
      7 using namespace std;
      8 
      9 int start[9],goal[9];
     10 int exist[Maxn];
     11 char step[Maxn];
     12 int factorial[]={1,1,2,6,24,120,720,5040,40320,362880}; 
     13 struct Node{
     14     int num;//步数 
     15     int state[9];//状态 
     16 };
     17 int dir[4][2]={
     18     {1,0},//
     19     {0,1},//
     20     {-1,0},//
     21     {0,-1} //
     22 }; 
     23 bool Contor(int a[],int m)//康拓展开得到下标 
     24 {
     25     long long site=0,cnt;//cnt记录比当前下标所在位更大的数的个数 
     26     for(int i=0; i<m; ++i)
     27     {
     28         cnt=0;
     29         for(int k=i+1; k<m; ++k)
     30         {
     31             if(a[i]<a[k]){
     32                  cnt++; 
     33             }
     34         }
     35         site+=cnt*factorial[m-i-1];//精髓 
     36     }
     37     if(!exist[site]){
     38         exist[site]=1;
     39         return true;
     40     }else{
     41         return false;
     42     }
     43 }
     44 
     45 int bfs()
     46 {
     47     queue<Node> Q;
     48     Node head;
     49     head.num=0;
     50     memcpy(head.state,start,sizeof(head.state));
     51     Contor(head.state,9);
     52     
     53     Q.push(head);
     54     int foot=0;//步骤 step的下标 
     55     while(!Q.empty())
     56     {
     57         int zeroSite; 
     58         head=Q.front();
     59         if(!memcmp(goal,head.state,sizeof(goal)))
     60         {
     61             return head.num;
     62         }
     63         
     64         //调出X所在的一维坐标
     65         for(zeroSite=0;zeroSite<9;++zeroSite)
     66         {
     67             if(head.state[zeroSite]==0){
     68                 break;
     69             }
     70         }
     71         Q.pop();
     72             
     73         int x=zeroSite%3;//第几列
     74         int y=zeroSite/3;//第几行
     75         int newZ;//移动后X所在的一维下标
     76         int newX,newY;
     77 
     78         for(int i=0; i<4; ++i)
     79         {
     80             newY=y+dir[i][1];
     81             newX=x+dir[i][0];
     82             newZ = 3*newY+newX;
     83             if(Judge(newX,newY)){
     84                 Node next;
     85                 memcpy(&next,&head,sizeof(struct Node));
     86                 swap(next.state[zeroSite],next.state[newZ]);
     87                 if(Contor(next.state,9)){
     88                     if(i==0){
     89                         step[foot]='r';
     90                     }else if(i==1){
     91                         step[foot]='d';
     92                     }else if(i==2){
     93                         step[foot]='l';
     94                     }else{
     95                         step[foot]='u';
     96                     }
     97                     foot++;
     98                     next.num++;
     99                     Q.push(next);
    100                 }
    101             }
    102         }     
    103     }
    104     step[foot]='';
    105 }
    106 int main()
    107 {
    108     char startCh[10],goalCh[10];
    109     int N;
    110     cin>>N;
    111     for(int i=1;i<=N;++i)
    112     {
    113         memset(exist,0,sizeof(int));
    114         memset(step,0,sizeof(char));
    115         scanf("%s",startCh);
    116         for(int j=0; j<9; ++j)
    117         {
    118             if(startCh[j]!='X'){
    119                 start[j]=startCh[j]-'0';
    120             }else{
    121                 start[j]=0;
    122             }
    123         }
    124         scanf("%s",goalCh);
    125         for(int j=0; j<9; ++j)
    126         {
    127             if(goalCh[j]!='X'){
    128                 goal[j]=goalCh[j]-'0';
    129             }else{
    130                 goal[j]=0;
    131             }
    132         }
    133 
    134         cout<<"Case"<<' '<<i<<':'<<' '<<bfs()<<endl;
    135         printf("%s",step);
    136     }
    137     return 0;
    138 }

    运行结果如下:

    似乎还需要一些未掌握的方法.......(待续……)

    天涯犹在,不诉薄凉。
  • 相关阅读:
    NETCore EF 数据库连接正确nuget和MySql错误异常
    JS 对象属性名排序
    NET 在一个数组中查找另一个数组所在起始位置(下标从0开始,未找到返回-1)
    NET 判断是否为回文
    NET/Regex 处理连续空格
    NET 已知excel表格前面26个是a到z,27是aa28是ab,以此类推,N是多少
    Regex 首字母转大写/小写,全大写,全小写
    .NETCore下访问img、js等静态资源404解决办法
    WPF-后台代码使用Behavior
    Socket-服务器端与客户端互相通信
  • 原文地址:https://www.cnblogs.com/Knight02/p/14678895.html
Copyright © 2011-2022 走看看