【UVA - 227】Puzzle (模拟,水题)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/84590299

题干:

 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 ;
 }

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/84590299
今日推荐