zoukankan      html  css  js  c++  java
  • sicily 1150 简单魔板 sicily 1151 魔板 sicily 1515 魔板C

    这三道题目大体相同,只是数据处理方式不同,需要修改的地方很少,因此只以1150为例说明即可。

    Description

    魔板由8个大小相同方块组成,分别用涂上不同颜色,用18的数字表示。

    其初始状态是

    1 2 3 4

    8 7 6 5

    对魔板可进行三种基本操作:

    A操作(上下行互换):

    8 7 6 5

    1 2 3 4

    B操作(每次以行循环右移一个):

    4 1 2 3

    5 8 7 6

    C操作(中间四小块顺时针转一格):

    1 7 2 4

    8 6 3 5

    用上述三种基本操作,可将任一种状态装换成另一种状态。

    Input

    输入包括多个要求解的魔板,每个魔板用三行描述。

    第一行步数N不超过10的整数),表示最多容许的步数。

    第二、第三行表示目标状态,按照魔板的形状,颜色用18的表示。

     N 等于 -1 的时候,表示输入结束。

    Output

    对于每一个要求解的魔板,输出一行。

    首先是一个整数M,表示你找到解答所需要的步数。接着若干个空格之后,从第一步开始按顺序给出M步操作(每一步是ABC),相邻两个操作之间没有任何空格。

    注意:如果不能达到,则 M 输出 -1 即可。

    Sample Input

    4
    5 8 7 6
    4 1 2 3
    3
    8 7 6 5
    1 2 3 4
    -1

    Sample Output

    2 AB
    1 A
    
    评分:M超过N或者给出的操作不正确均不能得分。

    分析:

    本题因为题目的特殊性有两种解法,一种是比较普通的广搜法(BFS),另外一种是利用魔板性质构造哈希表的方法。两者相较,方法一更直观,普适性强些,方法二更巧妙,当然速度更快些。同时,本题又要注意数据处理,因为到1151和1515的时候,数据量变大,对数据存储和计算要求变高,所以并不推荐用数组存储魔板状态。观察下发现,魔板只有八个格子,完全可以将八个数字变成一串;而想到整数计算和存储性质仍不够理想,可以用3位二进制数表示单个的数字位,那样变化函数就变成了位运算,速度大幅增加,同时存储要求也降低了很多。(当然,用数组做也可以用一些数据压缩的办法,但是会让数据存储和运算部分变得复杂,并不推荐。)

    解法一:

    因为有三种变化方法,所以很直观的想到直接搜索。视本题要求为找出图中特定起点和重点间的通路,且通路长度有限制,自然会想到BFS方法。直接套用BFS方法,即可得出答案。

    解法二:

    本题题目很特殊,魔板的三种变化其实每一种都是可以循环一定次数再回到初始状态的,而且明显状态A到状态B的操作反过来就可以得到状态B到状态A的操作流程。同时,我们发现,解法一每次操作都要从相同的起点开始查找相同的路径,这样太浪费时间,可以制作一个哈希表存储广搜法的遍历结果。然后,将原先的初始状态作为目标状态,根据循环操作的性质不断回溯,找到如何构造它的方法,将这一操作序列反向输出就是答案。

    代码:

    解法一:直接广度优先搜索

     1 // Problem#: 1150
     2 // Submission#: 1786358
     3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
     4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
     5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
     6 #include <iostream>
     7 #include <queue>
     8 #include <string>
     9 #include <cstring>
    10 using namespace std;
    11 
    12 int  a[8] = { 1 , 2 , 3 , 4 , 8 , 7 , 6 , 5 } ;
    13 bool visit[1<<24];
    14 
    15 struct node{
    16     int code;
    17     string path;
    18     node(){
    19     code = 0;
    20     path = "";
    21     }
    22 }begin,end;
    23 
    24 inline  int  A( int  n) { return  (n & 4095 ) << 12 | n >> 12 ;}
    25 
    26 inline  int  B( int  n) { return  (( 7 << 9 | 7 << 21 ) & n) >> 9   |  ( ~ ( 7 << 9 | 7 << 21 ) & n) << 3 ;}
    27 
    28 inline  int  C( int  n) { return  ( 7 | 7 << 9 | 7 << 12 | 7 << 21 ) & n  |  (( 7 << 3 ) & n) << 3   |  (( 7 << 6 ) & n) << 12   |  (( 7 << 18 ) & n) >> 3   |  (( 7 << 15 ) & n) >> 12 ;}
    29 
    30 inline  int  zip( int  a[]) {
    31     int  s = 0 ;
    32     for ( int  i = 0 ;i < 8 ; ++ i) s |= (a[i] - 1 ) << ( 3 * i);
    33     return  s;
    34 }
    35 
    36 void bfs(int n){
    37     queue<node> buffer;
    38     buffer.push(begin);
    39     visit[begin.code] = true;
    40     node t;
    41     int v;
    42     while(!buffer.empty()){
    43     t = buffer.front();
    44     buffer.pop();
    45     if(t.path.size()>n){
    46         cout << "-1" << endl;
    47         return ;
    48     }
    49     if(t.code==end.code){
    50         cout << t.path.size() << " " << t.path << endl;
    51         return ;
    52     }
    53     node in;
    54     v = A(t.code);
    55     if(!visit[v]){
    56         visit[v] = true;
    57         in.code = v;
    58         in.path = t.path + "A";
    59         buffer.push(in);
    60     }
    61     v = B(t.code);
    62     if(!visit[v]){
    63         visit[v] = true;
    64         in.code = v;
    65         in.path = t.path + "B";
    66         buffer.push(in);
    67     }
    68     v = C(t.code);
    69     if(!visit[v]){
    70         visit[v] = true;
    71         in.code = v;
    72         in.path = t.path + "C";
    73         buffer.push(in);
    74     }
    75     }
    76 }
    77 
    78 int main(){
    79     int n;
    80     int temp[8],re;
    81     begin.code = zip(a);
    82 
    83     while(cin>>n && n!=-1){
    84     for( int i=0 ; i<8 ; i++ ) cin >> temp[i];
    85     end.code = zip(temp);
    86     memset(visit,0,sizeof(visit));
    87     bfs(n);
    88     }
    89     return 0;
    90 }

    解法二:哈希表

     1 // Problem#: 1150
     2 // Submission#: 1781837
     3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
     4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
     5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University
     6 #include <cstdio> 
     7 char  hash[ 1 << 24 ]; 
     8 inline  int  A( int  n) { return  (n & 4095 ) << 12 | n >> 12 ;} // 4095=2^12-1
     9 inline  int  B( int  n) { return  (( 7 << 9 | 7 << 21 ) & n) >> 9   |  ( ~ ( 7 << 9 | 7 << 21 ) & n) << 3 ;}
    10 inline  int  C( int  n) { return  ( 7 | 7 << 9 | 7 << 12 | 7 << 21 ) & n  |  (( 7 << 3 ) & n) << 3   |  (( 7 << 6 ) & n) << 12   |  (( 7 << 18 ) & n) >> 3   |  (( 7 << 15 ) & n) >> 12 ;}
    11 inline  int  zip( int  a[]) { 
    12     int  s = 0 ;
    13     for ( int  i = 0 ;i < 8 ; ++ i) s |= (a[i] - 1 ) << ( 3 * i);
    14     return  s;
    15 }
    16 int  a[] = { 1 , 2 , 3 , 4 , 8 , 7 , 6 , 5 } ; 
    17 const   int  QLen = 10000 ; 
    18 int  q[QLen],b = 0 ,e = 0 ; 
    19 inline  void  inc( int &  p){ if(++ p ==QLen) p=0;} 
    20 int main(){
    21    int i,j,n,bgn=zip(a);
    22     hash[bgn]='E';
    23       q[b]=bgn; inc(b);
    24       while(b!=e){
    25          i=q[e]; inc(e);
    26          j=A(i);
    27          if(!hash[j]) hash[j]='A', q[b]=j, inc(b);
    28          j=B(i);
    29          if(!hash[j]) hash[j]='B', q[b]=j, inc(b);
    30          j=C(i);
    31          if(!hash[j]) hash[j]='C', q[b]=j, inc(b);
    32       }
    33       char s[100];
    34       while(scanf("%d",&n),n!=-1){
    35          for(i=0; i<8; ++i) scanf("%d",&a[i]);
    36          for(i=zip(a),j=0; i!=bgn; ++j){
    37             s[j]=hash[i];
    38             switch(s[j]){
    39                case 'A': i=A(i);        break;
    40                case 'B': i=B(B(B(i)));    break;
    41                case 'C': i=C(C(C(i)));    break;
    42             }
    43          }
    44          if(j>n) printf("-1\n");
    45          else{
    46             printf("%d ",j);
    47             while(j--) putchar(s[j]);
    48             putchar('\n');
    49          }
    50       }
    51       return 0;
    52 }
  • 相关阅读:
    (一)Python入门-2编程基本概念:18字符串-驻留机制-内存分析-字符串同一判断-值相等判断
    (一)Python入门-2编程基本概念:07内置数据类型-基本算数运算符
    (一)Python入门-2编程基本概念:08整数-不同进制-其他类型转换成整数
    (一)Python入门-2编程基本概念:09浮点数-自动转换-强制转换-增强赋值运算符
    (一)Python入门-2编程基本概念:10时间表示-unix时间点-毫秒和微妙-time模块
    (一)Python入门-2编程基本概念:11布尔值-比较运算符-逻辑运算符及短路问题
    (一)Python入门-2编程基本概念:12同一运算符-整数缓存问题
    (一)Python入门-2编程基本概念:01程序的构成
    (一)Python入门-2编程基本概念:02对象的基本组成和内存示意图
    有关位运算的基础知识总结
  • 原文地址:https://www.cnblogs.com/ciel/p/2876755.html
Copyright © 2011-2022 走看看