Sample Input
TRGSJ
XDOKI
M__VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS__
TUVWX
AAALLLL0
ABCDE
FGHIJ
KLMNO
PQRS__
TUVWX
AAAAABBRRRLL0
Z
没测试。。账号注册不上……
1 #include<iostream> 2 #include<stdio.h> 3 using namespace std; 4 char puzzle[6][6]; 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 } 25 return 1; 26 } 27 int main() 28 { 29 char c; 30 int x,y; 31 int Count=0; 32 while(true) 33 { 34 Count++; 35 for(int i=1;i<=5;i++) 36 { 37 for(int j=1;j<=5;j++) 38 { 39 c = getchar(); 40 if(c == EOF) return 0; 41 puzzle[i][j] = c; 42 if(c == ‘_‘) 43 { 44 x = i;y=j; 45 c = getchar(); 46 } 47 } 48 c = getchar(); 49 } 50 int p = 1; 51 52 while((c = getchar())!=‘0‘) 53 { 54 if(r(c,x,y) == 0) 55 { 56 p=0; 57 } 58 } 59 c = getchar(); 60 cout<<"Puzzle #"<<Count<<":"<<endl; 61 if(p == 0) 62 { 63 cout<<"This puzzle has no final configuration."<<endl; 64 }else{ 65 for(int i=1;i<=5;i++) 66 { 67 for(int j=1;j<=5;j++) 68 { 69 cout<<puzzle[i][j]; 70 if(j!=5) cout<<" "; 71 } 72 cout<<endl; 73 } 74 } 75 } 76 return 0; 77 }