Puzzle,ACM/ICPC World Finals 1993, UVa227

来源:互联网 发布:录屏软件哪个好 编辑:程序博客网 时间:2024/04/25 07:04

ACM纯萌新,刚开始刷题还在用c语言写,感觉这道题用c++写好一点

(1)c语言读入带'\n'和' '的字符串时会很麻烦,在这个点上花费了较长时间

(2)关于清除缓冲区的问题,本来想用fflush(stdin),但是可移植性不佳,比赛可能没法用,于是改成:

char c = 0;

while ((c = getchar()) != '\n'&&c != EOF);

也算学到了新的清除缓冲区的方法。

(3)要多考虑鲁棒性,这个题目的输入不是很标准,所以要多注意程序本身的鲁棒性。

(4)感觉自己的代码还是有不少能简化的地方

在这道题上花的时间较多,但是也学到了不少东西。


Description

A children’s puzzle that was popular 30 years ago consisted of a 5×5 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.

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 <stdio.h>#include <string.h>#include <stdlib.h>//0 presents A(Above); 1 presents B(Below); 2 presents L(Left); 3 presents R(Right)int dx[4] = { -1,1,0,0 };int dy[4] = { 0,0,-1,1 };int main(void){//freopen("out.txt", "w", stdout);int kase = 0;while (1){int Space_X = 0, Space_Y = 0;char Puzzle_Begin[7][7];memset(Puzzle_Begin, 0, sizeof(Puzzle_Begin));for (int i = 1; i <= 5; i++){for (int j = 1; j <= 5; j++){Puzzle_Begin[i][j] = getchar();if (Puzzle_Begin[i][j] == 'Z')break;else if (Puzzle_Begin[i][j] == ' '){Space_X = i;Space_Y = j;}}if (Puzzle_Begin[1][1] == 'Z')break;char c = 0;while ((c = getchar()) != '\n'&&c != EOF);}if (Puzzle_Begin[1][1] == 'Z')break;if (kase != 0)printf("\n\n");int i = 0, status = 1;char ch = 0;char Command[1000];memset(Command, 0, sizeof(Command));while ((ch = getchar()) != '0')Command[++i] = ch;i = 0;while (Command[++i] != '0'){if (Command[i] == '\n')continue;if (Command[i] == 0)break;int Select = 0;switch (Command[i]){case 'A':Select = 0;break;case 'B':Select = 1;break;case 'L':Select = 2;break;case 'R':Select = 3;break;default:status = 0;}if (Puzzle_Begin[Space_X + dx[Select]][Space_Y + dy[Select]] == 0){status = 0;break;}else{Puzzle_Begin[Space_X][Space_Y] = Puzzle_Begin[Space_X + dx[Select]][Space_Y + dy[Select]];Puzzle_Begin[Space_X + dx[Select]][Space_Y + dy[Select]] = ' ';Space_X += dx[Select];Space_Y += dy[Select];}}if (status){printf("Puzzle #%d:\n", ++kase);for (int i = 1; i <= 4; i++){for (int j = 1; j <= 4; j++)printf("%c ", Puzzle_Begin[i][j]);printf("%c\n", Puzzle_Begin[i][5]);}for (int j = 1; j <= 4; j++)printf("%c ", Puzzle_Begin[5][j]);printf("%c", Puzzle_Begin[5][5]);}else{printf("Puzzle #%d:\nThis puzzle has no final configuration.", ++kase);}char c = 0;while ((c = getchar()) != '\n'&&c != EOF);}printf("\n");//fclose(stdout);system("pause");return 0;}




原创粉丝点击