Sample Input
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS
TUVWX
AAAAABBRRRLL0
Z
简单的模拟题。告诫一下自己注意这种情况:
1 #include<iostream> 2 #include<stdio.h> 3 using namespace std; 4 char puzzle[7][7]; 5 bool r(char c,int &x,int &y) 6 { 7 if(c == 'A'){ 8 if(x == 1) return 0; 9 puzzle[x][y] = puzzle[x-1][y]; 10 puzzle[x-1][y] = ' '; 11 x--; 12 }else if(c == 'B'){ 13 if(x == 5) return 0; 14 puzzle[x][y] = puzzle[x+1][y]; 15 puzzle[x+1][y] = ' ';x++; 16 }else if(c == 'L') 17 { if(y==1) return 0; 18 puzzle[x][y] = puzzle[x][y-1]; 19 puzzle[x][y-1] = ' ';y--; 20 }else if(c=='R'){ 21 if(y == 5) return 0; 22 puzzle[x][y] = puzzle[x][y+1]; 23 puzzle[x][y+1] = ' ';y++; 24 }else{ 25 return 0; 26 } 27 return 1; 28 } 29 int main() 30 { 31 char c; 32 int x,y; 33 int Count=0; 34 while(true) 35 { 36 Count++; 37 for(int i=1;i<=5;i++) 38 { 39 for(int j=1;j<=5;j++) 40 { 41 c = getchar(); 42 while(c == ' ' || c == ' ') 43 { 44 c = getchar(); 45 } 46 if(c == 'Z') return 0; 47 puzzle[i][j] = c; 48 if(c == ' ') 49 { 50 x = i;y=j; 51 } 52 } 53 } 54 int p = 1; 55 while((c = getchar()) != '0') 56 { 57 while(c == ' ' || c == ' ') 58 { 59 c = getchar(); 60 } 61 if(c == '0') break; 62 if(r(c,x,y) == 0) 63 { 64 p=0; 65 } 66 } 67 c = getchar(); 68 if(Count != 1) cout<<endl; 69 cout<<"Puzzle #"<<Count<<":"<<endl; 70 if(p == 0) 71 { 72 cout<<"This puzzle has no final configuration."<<endl; 73 }else{ 74 for(int i=1;i<=5;i++) 75 { 76 for(int j=1;j<=5;j++) 77 { 78 cout<<puzzle[i][j]; 79 if(j!=5) cout<<" "; 80 } 81 cout<<endl; 82 } 83 } 84 } 85 return 0; 86 }
结果: TRGSJ XDOKI M VLN WPABE UQHCF ARRBBL0 Puzzle #1: T R G S J X O K L I M D V B N W P A E U Q H C F ABCDE FGHIJ KLMNO PQRS TUVWX AAA LLLL0 Puzzle #2: A B C D F G H I E K L M N J P Q R S O T U V W X ABCDE FGHIJ KLMNO PQRS TUVWX AAAAABBRRRLL0 Puzzle #3: This puzzle has no final configuration. Z Process returned 0 (0x0) execution time : 8.777 s Press any key to continue.