Uva227.Puzzle

来源:互联网 发布:女生下体有味道 知乎 编辑:程序博客网 时间:2024/06/05 17:27

Puzzle 

              Time limit: 3.000 seconds

 

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 #1Puzzle #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

 

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.

解题思路:本题考察的是字符串相关操作,在做题时,注意下行末的换行就可以了;
  第二组测试的命令字符串分成两组输入的,要注意处理方法,不要直接用gets()函数,采用getchar()一个一个读或者先把命令读到数组
  可以减少一些不必要的麻烦,再就是要处理好越界问题,即题目所说的非法输入。
  
AC代码:
#include <stdio.h>#include <string.h>char command[10000];int main(){char str[5][7];char dr[] = {'A','B','L','R'};int dir[][2] = {{-1,0},{1,0},{0,-1},{0,1}};int i, j, x, y, k, flag, T = 1;while(gets(str[0])){flag = 1;if(strcmp(str[0],"Z") == 0)break;for(i = 1;i < 5;++i)gets(str[i]);for(i = 0;i < 5;++i)for(j = 0;j < 5;++j)if(str[i][j] == ' '){x = i, y = j;i = j = 5;}if(T > 1)printf("\n");i = 0;while((command[i++] = getchar()) != '0');for(j = 0;j < i;++j){for(k = 0;k < 4;++k){if(command[j] == dr[k])break;}if(k > 3)continue;if(x+dir[k][0] < 0 || x+dir[k][0] > 4 || y+dir[k][1] < 0 || y+dir[k][1] > 4){flag = 0;break;}str[x][y] = str[x+dir[k][0]][y+dir[k][1]];x = x+dir[k][0], y = y+dir[k][1];}str[x][y] = ' ';printf("Puzzle #%d:\n",T);if(flag){for(i = 0;i < 5;++i)for(j = 0;j < 5;++j)printf( (j == 4) ? "%c\n":"%c ",str[i][j]);}else{printf("This puzzle has no final configuration.\n");}T++;getchar();}return 0;}


0 0
原创粉丝点击