您的位置:首页 > 其它

UVA 227 - Puzzle

2016-01-17 10:19 423 查看

Puzzle

A children’s puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smallsquares of equal size. A unique letter of the alphabet was printed on each small square. Since therewere only 24 squares within the frame, the frame also contained
an empty position which was the samesize as a small square. A square could be moved into that empty position if it were immediately to theright, to the left, above, or below the empty position. The object of the puzzle was to slide squaresinto 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 afterthe 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 andthe sequence of moves on the puzzle. The first 5 lines of each puzzle description are the startingconfiguration. 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 otherlines follow in order. The empty position in a frame is indicated by a blank. Each display line containsexactly 5 characters, beginning with the character
on the leftmost square (or a blank if the leftmostsquare 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 squaremoves into the empty position. A denotes that the square above the empty position moves; B denotesthat the square below the empty position moves; L denotes that
the square to the left of the emptyposition moves; R denotes that the square to the right of the empty position moves. It is possible thatthere is an illegal move, even when it is represented by one of the 4 move characters. If an illegal moveoccurs, the puzzle
is considered to have no final configuration. This sequence of moves may be spreadover 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.). Ifthe puzzle has no final configuration, then a message to that effect should follow. Otherwise that finalconfiguration should be displayed.

Format each line for a final configuration so that there is a single blank character between twoadjacent letters. Treat the empty square the same as a letter. For example, if the blank is an interiorposition, 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.

题目大意就是说,根据所给指向移动格中空格所在位置,输出最后状态,然后就是输出时最后一次没有空行。(好久之前写的,一直木整理。。。)

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char mazz[6][6];
int x,y;
int judge(int x,int y)
{
if(x<0||x>=5||y<0||y>=5)
return 0;
return 1;
}
int change(int sx,int sy)
{
if(judge(x+sx,y+sy))
{
mazz[x][y]=mazz[x+sx][y+sy];
mazz[x+sx][y+sy]=' ';
x+=sx;
y+=sy;
return 0;
}
return 1;
}
int main()
{
char c;
int cnt=0;
while(gets(mazz[0]))
{
int flag=0;
if(strcmp(mazz[0],"Z")==0)
return 0;
for(int i=1;i<5;++i)
gets(mazz[i]);
for(int i=0;i<5;++i)
{
for(int j=0;j<5;++j)
{
if(mazz[i][j]==' '||mazz[i][j]=='\0')
x=i,y=j;
}
}
while(c=getchar())
{
if(c=='0')
break;
if(c=='A')
{
if(change(-1,0))
flag=1;
}
else if(c=='B')
{
if(change(1,0))
flag=1;
}
else if(c=='L')
{
if(change(0,-1))
flag=1;
}
else if(c=='R')
{
if(change(0,1))
flag=1;
}
}
if(cnt)
printf("\n");
printf("Puzzle #%d:\n",++cnt);
if(flag)
printf("This puzzle has no final configuration.\n");
else
{
for(int i=0;i<5;++i)
{
for(int j=0;j<5;++j)
{
if(!j)
printf("%c",mazz[i][j]);
else
printf(" %c",mazz[i][j]);
}
printf("\n");
}
}
getchar();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: