首页 > 代码库 > UVa 227 / UVALive 5166 Puzzle 谜题 (结构体)
UVa 227 / UVALive 5166 Puzzle 谜题 (结构体)
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
A children‘s puzzle that was popular 30 years ago consisted of a 5x5 frame which contained 24 small squares of equal size. A unique letter of the alphabet was printed on each small square. Since there were only 24 squares within the frame, the frame also contained an empty position which was the same size as a small square. A square could be moved into that empty position if it were immediately to the right, to the left, above, or below the empty position. The object of the puzzle was to slide squares into the empty position so that the frame displayed the letters in alphabetical order.
The illustration below represents a puzzle in its original configuration and in its configuration after the following sequence of 6 moves:
1) The square above the empty position moves.
2) The square to the right of the empty position moves.
3) The square to the right of the empty position moves.
4) The square below the empty position moves.
5) The square below the empty position moves.
6) The square to the left of the empty position moves.
Write a program to display resulting frames given their initial configurations and sequences of moves.
Input
Input for your program consists of several puzzles. Each is described by its initial configuration and the sequence of moves on the puzzle. The first 5 lines of each puzzle description are the starting configuration. Subsequent lines give the sequence of moves.
The first line of the frame display corresponds to the top line of squares in the puzzle. The other lines follow in order. The empty position in a frame is indicated by a blank. Each display line contains exactly 5 characters, beginning with the character on the leftmost square (or a blank if the leftmost square is actually the empty frame position). The display lines will correspond to a legitimate puzzle.
The sequence of moves is represented by a sequence of As, Bs, Rs, and Ls to denote which square moves into the empty position. A denotes that the square above the empty position moves; B denotes that the square below the empty position moves; L denotes that the square to the left of the empty position moves; R denotes that the square to the right of the empty position moves. It is possible that there is an illegal move, even when it is represented by one of the 4 move characters. If an illegal move occurs, the puzzle is considered to have no final configuration. This sequence of moves may be spread over several lines, but it always ends in the digit 0. The end of data is denoted by the character Z.
Output
Output for each puzzle begins with an appropriately labeled number (Puzzle #1, Puzzle #2, etc.). If the puzzle has no final configuration, then a message to that effect should follow. Otherwise that final configuration should be displayed.
Format each line for a final configuration so that there is a single blank character between two adjacent letters. Treat the empty square the same as a letter. For example, if the blank is an interior position, then it will appear as a sequence of 3 blanks - one to separate it from the square to the left, one for the empty position itself, and one to separate it from the square to the right.
Separate output from different puzzle records by one blank line.
Note: The first record of the sample input corresponds to the puzzle illustrated above.
Sample Input
TRGSJXDOKIM VLNWPABEUQHCFARRBBL0ABCDEFGHIJKLMNOPQRS TUVWXAAALLLL0ABCDEFGHIJKLMNOPQRS TUVWXAAAAABBRRRLL0Z
Sample Output
Puzzle #1:T R G S JX O K L IM D V B NW P A EU Q H C FPuzzle #2: A B C DF G H I EK L M N JP Q R S OT U V W XPuzzle #3:This puzzle has no final configuration.
大致题意:有一个5*5的网格,其中恰好有一个格子是空的,其他格子各有一个字母。一共有4种指令:A,B,L,R,分别表示把空格上、下、左、右相邻的字母移到空格中。输入初始网格和指令序列(以数字0结束),输出指令执行完毕后的网格。如果有非法指令,应输出“This puzzle has no final configuration"。
思路:曾经做过大量搜索,想到用结构体来控制上下左右,但是时间长来,忘了怎么做来,做了一早上,代码如下。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 7 struct zb 8 { 9 int x,y; 10 } xy; 11 12 void findxy( string s, int x ) 13 { 14 int len=s.size(); 15 for( int i=0; i!=len; ++i ) 16 { 17 if(s[i]==‘ ‘) 18 { 19 xy.x=x; 20 xy.y=i; 21 return; 22 } 23 } 24 return; 25 } 26 27 int main() 28 { 29 string s,st; 30 vector<string> v; 31 int ti(1); 32 while(true) 33 { 34 v.clear(); 35 getline(cin,s); 36 if(s[0]==‘Z‘) 37 break; 38 if(ti>1) 39 cout<<endl; // 输出中间加空行 40 v.push_back(s); 41 findxy(s,0); 42 for(int i=1; i!=5; ++i) 43 { 44 getline(cin,s); 45 v.push_back(s); 46 findxy(s,i); 47 } 48 // 读完矩阵,并找到空格坐标。 49 s=""; 50 while(true) 51 { 52 getline(cin,st); 53 s+=st; 54 if(st[st.size()-1]==‘0‘) break; 55 } 56 // 读完判断序列 ABLR 57 bool cando=true; 58 int len=s.size(); 59 for( int i=0; i!=len; ++i ) 60 { 61 char c=s[i]; 62 if(c==‘0‘) 63 break; 64 int line=xy.x,row=xy.y; 65 if(c==‘A‘) 66 { 67 if(line==0) 68 { 69 cando=false; 70 break; 71 } 72 else 73 { 74 --xy.x; 75 swap(v[line][row],v[line-1][row]); 76 } 77 } 78 if(c==‘B‘) 79 { 80 if(line==4) 81 { 82 cando=false; 83 break; 84 } 85 else 86 { 87 ++xy.x; 88 swap(v[line][row],v[line+1][row]); 89 } 90 } 91 if(c==‘L‘) 92 { 93 if(row==0) 94 { 95 cando=false; 96 break; 97 } 98 else 99 {100 --xy.y;101 swap(v[line][row],v[line][row-1]);102 }103 }104 if(c==‘R‘)105 {106 if(row==4)107 {108 cando=false;109 break;110 }111 else112 {113 ++xy.y;114 swap(v[line][row],v[line][row+1]);115 }116 }117 }118 // 走完119 cout<<"Puzzle #"<<ti<<":"<<endl;120 if(cando)121 {122 for( int i=0; i!=5; ++i )123 {124 for( int j=0; j!=4; ++j )125 cout<<v[i][j]<<" ";126 cout<<v[i][4]<<endl;127 }128 }129 else cout<<"This puzzle has no final configuration."<<endl;130 ++ti;131 }132 return 0;133 }
另一种代码如下:
1 #ifndef HEAD_FILE 2 #pragma comment(linker, "/STACK:36777216") 3 #include <algorithm> 4 #include <bitset> 5 #include <cassert> 6 #include <cmath> 7 #include <cstdio> 8 #include <cstring> 9 #include <ctime> 10 #include <functional> 11 #include <iomanip> 12 #include <iostream> 13 #include <list> 14 #include <map> 15 #include <queue> 16 #include <set> 17 #include <stack> 18 #include <string> 19 #include <vector> 20 #define cst const 21 using namespace std; 22 #endif // HEAD_FILE 23 #ifndef TYPEDEF 24 typedef long long llint; 25 typedef double lf; 26 typedef unsigned uint; 27 typedef unsigned long long ullint; 28 #endif // TYPEDEF 29 30 char puzzle[15][15]; 31 32 int x, y; 33 inline bool moveL() { 34 if (y == 0) return false; 35 puzzle[x][y] = puzzle[x][y - 1]; 36 puzzle[x][-- y] = ‘ ‘; 37 return true; 38 } 39 40 inline bool moveR() { 41 if (y == 4) return false; 42 puzzle[x][y] = puzzle[x][y + 1]; 43 puzzle[x][++ y] = ‘ ‘; 44 return true; 45 } 46 47 inline bool moveA() { 48 if (x == 0) return false; 49 puzzle[x][y] = puzzle[x - 1][y]; 50 puzzle[-- x][y] = ‘ ‘; 51 return true; 52 } 53 54 inline bool moveB() { 55 if (x == 4) return false; 56 puzzle[x][y] = puzzle[x + 1][y]; 57 puzzle[++ x][y] = ‘ ‘; 58 return true; 59 } 60 61 int main() { 62 #ifndef ONLINE_JUDGE 63 64 // freopen("in.txt", "r", stdin); 65 // freopen("out.txt", "w", stdout); 66 #endif 67 68 for (int i_case = 1; fgets(puzzle[0], sizeof puzzle[0], stdin); i_case ++) { 69 if (puzzle[0][0] == ‘Z‘ && strlen(puzzle[0]) == 2) break; 70 if ( i_case != 1 ) puts(""); 71 for (int i = 1; i < 5; i ++) { 72 fgets(puzzle[i], sizeof puzzle[0], stdin); 73 } 74 for (int i = 0; i < 5; i ++) { 75 if ( strlen(puzzle[i]) < 5 ) { 76 x = i; y = 5; break; 77 } else { 78 for ( int j = 0; j < 5; j ++) { 79 if (!isalpha(puzzle[i][j])) { 80 x = i; y = j; break; 81 } 82 } 83 } 84 } 85 bool flag = true; 86 for (; ;) { 87 char ch = getchar(); 88 if ( ch == ‘0‘ ) break; 89 switch(ch) { 90 case ‘A‘: if ( !moveA() ) flag = false; break; 91 case ‘B‘: if ( !moveB() ) flag = false; break; 92 case ‘L‘: if ( !moveL() ) flag = false; break; 93 case ‘R‘: if ( !moveR() ) flag = false; break; 94 default: break; 95 } 96 } 97 printf("Puzzle #%d:\n", i_case); 98 if (!flag) cout << "This puzzle has no final configuration." << endl; 99 else {100 for (int i = 0; i < 5; i ++) {101 for (int j = 0; j < 5; j ++) {102 char ch = puzzle[i][j];103 putchar(isalpha(ch) ? ch : ‘ ‘);104 putchar(j == 4 ? ‘\n‘ : ‘ ‘);105 }106 }107 }108 getchar();109 }110 return 0;111 }
经过优化后的代码:
1 //18MS 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 int main() 7 { 8 int m[5][2]={0,0,-1,0,0,1,1,0,0,-1}; 9 char s[7][7],a,b;10 int x,y,i,j,k=1;11 while(gets(s[1]+1)&&s[1][1]!=‘Z‘){12 for(i=1;i<=5;i++)13 if(s[1][i]==‘ ‘){14 x=1;y=i;15 }16 for(i=2;i<=5;i++){17 gets(s[i]+1);18 for(j=1;j<=5;j++)19 if(s[i][j]==‘ ‘){20 x=i;y=j;21 }22 }23 j=1;24 while(1){25 a=getchar();26 if(a==‘0‘)27 break;28 if(j==0)29 continue;30 switch(a){31 case ‘A‘:i=1;break;32 case ‘R‘:i=2;break;33 case ‘B‘:i=3;break;34 case ‘L‘:i=4;break;35 default:i=0;36 }37 x+=m[i][0],y+=m[i][1];38 if(x==0||x==6||y==0||y==6){39 j=0;40 continue;41 }42 swap(s[x][y],s[x-m[i][0]][y-m[i][1]]);43 }44 getchar();45 if(k!=1)46 printf("\n");47 printf("Puzzle #%d:\n",k++);48 if(j==0)49 printf("This puzzle has no final configuration.\n");50 else{51 for(i=1;i<=5;i++){52 for(j=1;j<=5;j++){53 if(j!=1)54 printf(" ");55 printf("%c",s[i][j]);56 }57 printf("\n");58 }59 }60 }61 }
UVa 227 / UVALive 5166 Puzzle 谜题 (结构体)