zoukankan      html  css  js  c++  java
  • UVa

    给空格子上下左右的互换操作,问最后是怎样的

    注意一行的最后一个若是空格,需要自己加
    注意读取时 操作可能分好多行,一定要读取到 0 为止

      1 #include <iostream>
      2 #include <cstring>
      3 using namespace std;
      4 char map[50][50],op[1000],c,tmp;
      5 int k,t,x,y,cnt; 
      6 bool flag;
      7 void fuc()
      8 {
      9     flag=1;
     10     for(int i=0;op[i]!='0';i++)
     11     {
     12         if(flag==0) break;
     13         else if(op[i]=='A')
     14         {
     15             if(x-1>=0)
     16             {
     17                 tmp=map[x-1][y];
     18                 map[x-1][y]=' ';
     19                 map[x][y]=tmp;
     20                 x--;
     21             }
     22             else flag=0;
     23         } 
     24         else if(op[i]=='B')
     25         {
     26             if(x+1<=4)
     27             {
     28                 tmp=map[x+1][y];
     29                 map[x+1][y]=' ';
     30                 map[x][y]=tmp;
     31                 x++;
     32             }
     33             else flag=0;
     34         }
     35         else if(op[i]=='R')
     36         {
     37             if(y+1<=4)
     38             {
     39                 tmp=map[x][y+1];
     40                 map[x][y+1]=' ';
     41                 map[x][y]=tmp;
     42                 y++;
     43             }
     44             else flag=0;
     45         }
     46         else if(op[i]=='L')
     47         {
     48             if(y-1>=0)
     49             {
     50                 tmp=map[x][y-1];
     51                 map[x][y-1]=' ';
     52                 map[x][y]=tmp;
     53                 y--;
     54             }
     55             else flag=0;
     56         }
     57     }
     58 }
     59 int main()
     60 {
     61     k=1;
     62     while(gets(map[0]))
     63     {
     64         if(map[0][0]=='Z'&&map[0][1]=='') break;
     65         for(int i=1;i<5;i++) gets(map[i]);
     66         for(int i=0;i<5;i++)
     67         {
     68             if(strlen(map[i])!=5)
     69             {
     70                 map[i][4]=' ';
     71                 break;
     72             }
     73         }
     74         cnt=0;
     75         while(c=getchar())
     76         {
     77             if(c<='Z'&&c>='A') op[cnt++]=c;
     78             if(c=='0')
     79             {
     80                 op[cnt++]=c;
     81                 getchar();break;  //0 之后还有一个回车需要读取!!
     82             } 
     83         }
     84         for(int i=0;i<5;i++)
     85             for(int j=0;j<5;j++)
     86                 if(map[i][j]==' ')
     87                 {
     88                     x=i;y=j; break;
     89                 }
     90         fuc(); 
     91         if(k>1) puts("");
     92         printf("Puzzle #%d:
    ",k++);
     93         if(flag==0) puts("This puzzle has no final configuration.");
     94         else 
     95         {
     96             for(int i=0;i<5;i++)
     97             {
     98                 for(int j=0;j<4;j++)
     99                 {
    100                     printf("%c ",map[i][j]);
    101                 }
    102                 printf("%c
    ",map[i][4]);
    103             }
    104         }
    105     }
    106 }
    107 /*
    108 TRGSJ
    109 XDOKI
    110 M VLN
    111 WPABE
    112 UQHCF
    113 ARRBBL0
    114 ABCDE
    115 FGHIJ
    116 KLMNO
    117 PQRS
    118 TUVWX
    119 AAA
    120 LLLL0
    121 ABCDE
    122 FGHIJ
    123 KLMNO
    124 PQRS
    125 TUVWX
    126 AAAAABBRRRLL0
    127 Z
    128 */
    我自倾杯,君且随意
  • 相关阅读:
    CISCO DHCP全攻略详解
    CentOS配置远程日志服务器
    CentOS下的日志切割
    CentOS日志的简单介绍
    H3C Telnet 配置
    CentOS7图形界面与命令行界面切换
    思科4506E做ehterchannel故障排查
    EtherChannel Cisco 端口聚合详解
    算法练习2之单链表求和
    Retrofit2的GsonConverterFactory.create()和RxJava2CallAdapterFactory.create()的实现过程以及执行过程
  • 原文地址:https://www.cnblogs.com/nicetomeetu/p/5277192.html
Copyright © 2011-2022 走看看