E-puzzle (UVA-227)

Source: Internet
Author: User

Puzzle

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.


This problem actually has no technical content, as long as the requirements step by step down can be made out, but still have to pay attention to the details, like "every two output between" there is a blank line (and because this wa once = =), and notice that the input to the character must be carefully thought about. The characters that will occasionally mix your feet (because this wa several times = =sad). Also have to understand clear test instructions, this question requires only one character Z to terminate the program, do not understand the first character as Z when the program terminates. Pay attention to the details, a off the problem should be easy. The most important thing is, when you really can not find the wrong time, be sure to debug yourself, do not rely on others to give you the wrong, so that your level is not how much improvement. Okay, no more nonsense, put the code (the level is too low, can only write so long code to =)


#include <stdio.h>#include<string.h>intMain () {Charpuzzle[6][6],move[ +],temp; intI,j,k,si,sj,flag,count=0;  while(Gets (puzzle[0])!=null&&strcmp (puzzle[0],"Z")!=0) {Count++; Flag=1;  for(i=1; i<5; i++) {gets (puzzle[i]); }         for(i=0;i<5; i++)        {             for(j=0;j<5; j + +)            {                if(puzzle[i][j]==' ') {Si=i;sj=j; Break; }            }        }         for(i=0;; i++) {scanf ("%c",&Move[i]); if(move[i]=='0')                 Break; } k=i;  for(i=0; i<k; i++)        {            Switch(Move[i]) { Case 'A' :                if(si-1<0) {flag=0; }                Else{Temp=PUZZLE[SI][SJ]; PUZZLE[SI][SJ]=puzzle[si-1][SJ]; Puzzle[si-1][sj]=temp; Si= (si-1); }                 Break;  Case 'B' :                if(si+1>4) {flag=0; }                Else{Temp=PUZZLE[SI][SJ]; PUZZLE[SI][SJ]=puzzle[si+1][SJ]; Puzzle[si+1][sj]=temp; Si= (si+1); }                 Break;  Case 'L' :                if(sj-1<0) {flag=0; }                Else{Temp=PUZZLE[SI][SJ]; PUZZLE[SI][SJ]=puzzle[si][sj-1]; PUZZLE[SI][SJ-1]=temp; SJ= (sj-1); }                 Break;  Case 'R' :                if(sj+1>4) {flag=0; }                Else{Temp=PUZZLE[SI][SJ]; PUZZLE[SI][SJ]=puzzle[si][sj+1]; PUZZLE[SI][SJ+1]=temp; SJ= (sj+1); }            }            if(flag==0) Break; }        if(count!=1) printf ("\ n"); printf ("Puzzle #%d:\n", Count); if(flag==0) printf ("This puzzle have no final configuration.\n"); Else        {             for(i=0; i<5; i++)                 for(j=0; j<5; J + +)                {                    if(j==4) printf ("%c\n", Puzzle[i][j]); Else{printf ("%c", Puzzle[i][j]);    }}} GetChar (); }    return 0;}

E-puzzle (UVA-227)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.