• 紫书 习题3-5 谜题


    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    const char inst[] = "ABLR";
    const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    int main(void)
    {
        int t = 0;
        char s[5][6];
        char c;
        while ((s[0][0] = getchar()) != 'Z') {
            int bi = 0, bj = 0;
            for (int i = 0; i < 5; i ++) {
                for (int j = 0; j < 5; j ++) {
                    if (!i && !j) continue;
                    s[i][j] = getchar();
                    if (s[i][j] == ' ') {bi = i, bj = j;}
                }
                getchar();
            }
            bool legal = true;
            while ((c = getchar()) != '0') {
                if (legal == false || c == '
    ') continue;
                int k;
                for (k = 0; k < 4; k ++) {
                    if (c == inst[k]) break;
                }
                if (k == 4)
                    legal = false;
                else {
                    int ni = bi+dir[k][0], nj = bj+dir[k][1];
                    if (0 <= ni && ni < 5 && 0 <= nj && nj < 5) {
                        swap(s[bi][bj], s[ni][nj]);
                        bi = ni, bj = nj;
                    } else
                        legal = false;
                }
            }
            if (++t > 1) printf("
    ");
            printf("Puzzle #%d:
    ", t);
            if (legal == false)
                printf("This puzzle has no final configuration.
    ");
            else {
                for (int i = 0; i < 5; i ++) {
                    for (int j = 0; j < 5; j ++) {
                        printf("%c%c", s[i][j], j == 4 ? '
    ' : ' ');
                    }
                }
            }
            getchar();
        }
    
        return 0;
    }
    

      

     1 # include <iostream>
     2 # include <cstdio>
     3 
     4 using namespace std;
     5 
     6 int main(){
     7 
     8     char a[5][7],t;
     9     int n,m,i,j,k;
    10 
    11 
    12 
    13     int cases = 0;
    14     char modol[1001];
    15     while(gets(a[0])){
    16 
    17         if(a[0][0]=='Z') break; 
    18 
    19         for(i=1;i<5;i++){
    20             gets(a[i]);
    21         }
    22 
    23 
    24         //查找空格的位置
    25         int x = 0,y = 0; 
    26         for(i=0;i<5;i++)
    27             for(j=0;j<5;j++)
    28                 if(a[i][j]==' '){
    29                  x=i,y=j;
    30                  break;//记录空格这个点 
    31                  } 
    32 
    33         //scanf不读取
    和空格
    34         int cnt = 0;
    35 
    36 
    37         while(~scanf("%c",&modol[cnt])) 
    38             if(modol[cnt]!='0')  cnt++;
    39             else break;
    40              /*
    41         char ch;
    42         while(scanf("%c",&ch))
    43             if(ch=='
    ') continue;
    44             else if(ch==' ') continue;
    45             else if(ch!='0') {
    46                 modol[cnt]=ch; 
    47                 cnt++;
    48             }
    49             else break;
    50             */  
    51         //gets与scanf("%c")   getcahr 的区别 
    52         int flag=0, x1 = x , y1=y;
    53         modol[cnt] = 0;
    54         getchar();
    55 
    56         for(i=0;modol[i];i++){
    57             switch(modol[i]){
    58                 case 'A' : x1 =  x - 1;y1=y; break;
    59                 case 'B' : x1 =  x + 1;y1=y; break;
    60                 case 'L' : x1 =  x;y1=y-1; break;
    61                 case 'R' : x1 =  x;y1=y+1; break;
    62             }
    63 
    64             if(x1<0||y1<0||x1>4||y1>4){
    65                 flag = 1;break;
    66             }else{
    67                 a[x][y] = a[x1][y1];
    68                 a[x1][y1] = ' ';
    69                 x = x1;
    70                 y = y1;
    71             }
    72 
    73         }
    74 
    75         if(cases++) printf("
    "); 
    76             printf("Puzzle #%d:
    ",cases);
    77 
    78         if(flag){
    79             printf("This puzzle has no final configuration.
    ");
    80         }else{
    81             for(i=0;i<5;i++){
    82                 printf("%c",a[i][0]);
    83                 for(j=1;j<5;j++){
    84                     printf(" %c",a[i][j]);
    85                 }
    86                 printf("
    ");
    87             }
    88 
    89 
    90 
    91         }
    92 
    93 
    94 
    95     }
    96 
    97 
    98     return 0;
    99 } 
  • 相关阅读:
    Go语言踩过的坑---记录GOPATH在GOLAND中的坑
    反射小例子
    制作pip包
    mac常用软件安装链接
    YCSB压测elasticsearch
    SSO和Auth2.0
    JAVA内部类的四大作用
    修改fastadmin,添加模糊查询
    AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message
    taro3.x: 封装实现chat emit on
  • 原文地址:https://www.cnblogs.com/Roni-i/p/7203402.html
Copyright © 2020-2023  润新知