Topic Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=163
Puzzle
Time limit:3.000 seconds
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 sequ Ence 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 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.
To solve this problem, there are a few points to note
1. About the data input preservation problem of puzzle, because there is enter at the end of each line, taking into account that the last data with ' Z ' as the ending character, (in particular, the problem of the final program to exit gracefully) with the Get () function is good, get read all the characters before the newline character ' \ n ', It is returned to the function that called it after it is added to the end, and then the enter character is read and discarded.
2. Secondly, with regard to the data input problem of the transfer sequence, it is necessary to read all the sequences before ' 0 ', using scanf ("%c", str[i++]), but with the GetChar () function to read all non-whitespace characters before ' 0 ' (using isspace ( ))。
3. Be careful to eat white space characters with GetChar after saving the call sequence, and finally note that the output format is OK.
The AC code is as follows:
#include <stdio.h> #include <string.h> #include <ctype.h>//2. #include <stdlib.h>char str[100] ; int main () {//Freopen ("C:\\users\\aliez\\desktop\\input.txt", "R", stdin);//Freopen ("c:\\users\\aliez\\desktop\\ Output.txt "," w ", stdout); Char puz[5][5],temp; int ch,c,len,m,n,t = 0,real,k; while (1) {real = 1; k = 0; memset (str, ' n ', sizeof (str)); memset (Puz, ", sizeof (Puz)); for (int i = 0;i < 5;i++) {gets (puz[i]); if (puz[0][0] = = ' Z ') return 0; 1.break; 2.exit (0); if (puz[i][4] = = ' + ') puz[i][4] = "; }/*1.if (puz[0][0] = = ' Z ') break;*/for (int i = 0;i < 5;i++) {for (int j = 0;j < 5;j++) {if (puz[i][j] = = ") {m = i; n = j; }}} while ((c = GetChar ()) = ' 0 ') {if (!isspace (c)) str[k++] = c; } while (1) {c = GetChar (); if (Isspace (c)) break; }//The following comment section is the test code//puts (puz[0]);//puts (str);//printf ("m=%d,n=%d\n", m,n); len = strlen (str); int x = m, y = n; for (int i = 0, I < len;i++) {switch (Str[i]) {case ' A ': x-- ; Break Case ' B ': x + +; Break Case ' L ': y--; Break Case ' R ': y++; Break default:real = 0; Break } if (X < 0| | X >4| | Y < 0| | Y > 4) {real = 0; Break } else{Puz[m][n] = puz[x][y]; Puz[x][y] = "; m = x; n = y; }} if (t++) printf ("\ n"); printf ("Puzzle #%d:\n", T); if (real) {for (int i = 0;i < 5;i++) {printf ("%c", puz[i][0]); for (int j = 1;j < 5;j++) printf ("%c", Puz[i][j]); printf ("\ n"); }} else printf ("This puzzle have no final configuration.\n"); } return 0;}
Uva 227-puzzle Problem Solving report