1. 程式人生 > >【UVA - 227】Puzzle (模擬,水題)

【UVA - 227】Puzzle (模擬,水題)

題幹:

 Puzzle 
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
TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAAAABBRRRLL0
Z
Sample Output
Puzzle #1:
T R G S J
X O K L I
M D V B N
W P   A E
U Q H C F

Puzzle #2:
  A B C D
F G H I E
K L M N J
P Q R S O
T U V W X

Puzzle #3:
This puzzle has no final configuration.

 

題目大意:

  有一個5*5的網格,其中恰好有一個格子是空的,其他格子均含有一個字母,一種有四種指令:A,B,L,R,分別表示把空格上,下,左,右的相鄰字母移到空格中去.輸入初始網格和指令序列(以數字0結束),指令不一定都在同一行中,但是一定以數字0結束,輸出操作後的指令,如果存在任一非法指令,輸出"This puzzle has no final configuration.".

解題報告:

   模擬就好了,,,但是這個讀入是真的坑啊,,,因為有的行直接就是四個字元,,你要給他補充上一個空格。第二個讀入的坑就是需要你用gets讀入因為可能中間有空格。第三個坑就是每個樣例之間需要getchar一下,吃掉多出來的那個空格。還有就是輸出格式,,不要多輸出一行回車那樣、、

 

AC程式碼:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char maze[50][50];
int main()
{
	int iCase = 0;
	while(1) {
		int xx,yy;
		gets(maze[0]);
		if(maze[0][0] == 'Z') return 0 ;
		int flag = 1;
		for(int i = 1; i<5; i++) {
			for(int j = 0; j<5; j++) {
				maze[i][j] = ' ';
			}
		}
		for(int i = 1; i<=4; i++) {
			gets(maze[i]);
			if(strlen(maze[i]) < 5) maze[i][4] =' ';
		}
		for(int i = 0; i<5; i++) {
			for(int j = 0; j<5; j++) {
				if(maze[i][j] == ' ') {
					xx=i,yy=j;break;
				}
			}
		}
//		for(int i = 0; i<5; i++) {
//			printf("%s\n",maze[i]);
//		}
		char ch;
		while(1) {
			ch=getchar();
			if(ch == '0') break;
			if(flag == 0) continue;
			if(ch == 'A' && xx>0) {
				maze[xx][yy] = maze[xx-1][yy];xx--;
			}
			else if(ch == 'B' && xx < 4) {
				maze[xx][yy] = maze[xx+1][yy],xx++;
			}
			else if(ch == 'L' && yy > 0) {
				maze[xx][yy] = maze[xx][yy-1];yy--;
			}
			else if(ch == 'R' && yy < 4) {
				maze[xx][yy] = maze[xx][yy+1];yy++;
			}
			else if(ch == '\n') continue;
			else flag=0;
		}
		maze[xx][yy]=' ';
		if(iCase) puts("");		
		printf("Puzzle #%d:\n",++iCase);
		if(flag == 0) puts("This puzzle has no final configuration.");
		else {
			for(int i = 0; i<5; i++) {
				for(int j = 0; j<5; j++) {
					printf("%c%c",maze[i][j],j == 4 ? '\n' : ' ');
				}
//				printf("111111\n");
			}
		}
		getchar();
	}

	
	
	return 0 ;
 }