Puzzle
| Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
A Children ' s puzzle that was popular the years ago consisted of a 5x5 frame which contained the small squares of equal size. A unique letter of the alphabet is printed on each small square. Since there were only squares within the frame, the frame also contained an empty position which is the same size as a Small square. A Square could is moved into that empty position if it were immediately to the right, to the left, above, or below the EMP Ty position. The object of the puzzle is to slide squares into the empty position so, the frame displayed the letters in Alphabeti Cal order.
The illustration below Represents a puzzle in it original configuration and in it configuration after the following sequence of 6 moves:
1) The square above 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 are described by their initial configuration and the sequence of moves on the puzzle. The first 5 lines of each puzzle description is 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 was indicated by a blank. Each display line contains exactly 5 characters, beginning with the character on the leftmost square (or a blank if the Le Ftmost Square is actually the empty frame position). The display lines would 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 posit Ion. A denotes that the square above the empty position moves; B denotes that the square below the empty position moves; L denotes the square to the left of the empty position moves; R denotes the square to the right of the empty position moves. It is possible this there is a illegal move, even when it's represented by one of the 4 move characters. If An illegal moves occurs, the puzzle is considered to has no final configuration. This sequence of moves is spread over several lines and 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 and then a message to that effect should follow. Otherwise that final configuration should is displayed.
Format each line for a final configuration so, there is a single blank character between the adjacent letters. Treat the empty square the same as a letter. For example, if the blank was an interior position and then it would 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 to 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.
#include <iostream> #include <cstdio> #include <cstring> #include <string>using namespace std; Char s[5][10];int loc_x, Loc_y;bool illeg;bool Getpuz () {for (int i = 0; i < 5; i++) {gets (s[i]); if (i = = 0 && St RCMP (S[i], "Z") = = 0) return false;} return true;} BOOL Illeg (int a, int b) {if (a<0 | | a>4 | | b<0 | | b>4) return True;return false; BOOL Move_a () {if (Illeg (Loc_x-1, loc_y)) return false;s[loc_x][loc_y] = s[loc_x-1][loc_y];s[loc_x-1][loc_y] = "; lo C_x--;return true;} BOOL Move_b () {if (Illeg (loc_x + 1, loc_y)) return false;s[loc_x][loc_y] = s[loc_x + 1][loc_y];s[loc_x + 1][loc_y] = "; lo C_x++;return true;} BOOL Move_l () {if (Illeg (loc_x, loc_y-1)) return false;s[loc_x][loc_y] = s[loc_x][loc_y-1];s[loc_x][loc_y-1] = "; loc_ Y--;return true;} BOOL Move_r () {if (Illeg (loc_x, loc_y+1)) return false;s[loc_x][loc_y] = s[loc_x][loc_y + 1];s[loc_x][loc_y + 1] = "; loc_ Y++;return true;} int main () {int cas = 0;int first = 1;while (Getpuz ()) {Illeg = 0;for (int i = 0; i < 5; i++) for (int j = 0; J < 5; J + +) {if (s[i][j] = = ") {loc_x = I;loc_y = J;break;}} char Op[100];int isend = 0;while (!isend) {cin >> op;for (int i = 0; I < strlen (OP); i++) {if (op[i] = = ' 0 ') {isend = 1; break;} BOOL Res=1;switch (Op[i]) {case ' A ': res=move_a (); Break;case ' B ': Res=move_b (); Break;case ' L ': res=move_l (); Break;case ' R ': Res=move_r (); break;} if (res = = false) Illeg = True;}} if (first) First=0;else cout << endl;printf ("Puzzle #%d:\n", ++cas); if (Illeg = true) cout << "This Puzzle ha s no final configuration. "<< endl;else for (int i = 0; i < 5; i++) {cout << s[i][0] <<" "<< s I [1] << "<< s[i][2] <<" "<< s[i][3] <<" "<< s[i][4] << Endl;} Gets (S[0]);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-227 Puzzle