第三章部分习题

 3-5

  1 #include<cstdio>
  2 #include<cstring>
  3 
  4 #define step 20
  5 char ins[step];
  6 
  7 int main()
  8 {
  9 
 10 /************************输入********************/
 11 
 12     char arr[5][5]={
 13         {'T','R','G','S','j'},
 14         {'X','D','O','K','I'},
 15         {'M','0','V','L','N'},
 16         {'W','P','A','B','E'},
 17         {'U','Q','H','C','F'}
 18     };
 19 
 20     for(int i=0;i<5;i++)
 21         {
 22             for(int j=0;j<5;j++)
 23             {
 24                 if(j==4)
 25                     printf("%c
",arr[i][j]);
 26                 else
 27                     printf("%c",arr[i][j]);
 28             }
 29         }    
 30 
 31     int space_x=0;
 32     int space_y=0;
 33 
 34     for(int i=0;i<5;i++)
 35         for(int j=0;j<5;j++)
 36             if(arr[i][j]=='0')
 37             {
 38                 space_x=j;
 39                 space_y=i;
 40             }
 41 
 42 /************************实现********************/
 43     int flag=1;
 44 
 45        scanf("%s",ins);             
 46 
 47        for(int i=0;ins[i]!='0';i++)
 48        {    
 49        
 50            switch (ins[i])
 51            {
 52                case 'A':
 53                {
 54                    if(space_y-1<0)
 55                        flag=0;
 56                    else
 57                    {
 58                        arr[space_y][space_x]=arr[space_y-1][space_x];
 59                        arr[space_y-1][space_x]='0';
 60                        space_y--;
 61                    }
 62                }
 63                break;
 64 
 65                case 'B':
 66                {
 67                    if(space_y+1==5)
 68                        flag=0;
 69                    else
 70                    {
 71                        arr[space_y][space_x]=arr[space_y+1][space_x];
 72                        arr[space_y+1][space_x]='0';
 73                        space_y++;
 74                    }
 75                }
 76                break;
 77 
 78                case 'R':
 79                {
 80                    if(space_x+1==5)
 81                        flag=0;
 82                    else
 83                    {
 84                        arr[space_y][space_x]=arr[space_y][space_x+1];
 85                        arr[space_y][space_x+1]='0';
 86                        space_x++;
 87                    }
 88 
 89                }
 90                break;
 91 
 92                case 'L':
 93                {
 94                    if(space_x-1<0)
 95                        flag=0;
 96                    else
 97                    {
 98                        arr[space_y][space_x]=arr[space_y][space_x-1];
 99                        arr[space_y][space_x-1]='0';
100                        space_x--;
101                    }
102                }
103                break;
104 
105                default :
106                flag=0;
107            }
108        }
109 
110        if(flag==1)
111        {
112            for(int i=0;i<5;i++)
113         {
114             for(int j=0;j<5;j++)
115             {
116                 if(j==4)
117                     printf("%c
",arr[i][j]);
118                 else
119                     printf("%c",arr[i][j]);
120             }
121         }
122        }
123        else
124            printf("This puzzle has no final configuration.
");
125 }
Yosoro
原文地址:https://www.cnblogs.com/tclan126/p/7169409.html