游客 Signup | Login
中文 | En

3777 - 【START】2015暑期训练——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.

Examples

Input

TRGSJ
XDOKI
M VLN
WPABE
UQHCF
ARRBBL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAA
LLLL0
ABCDE
FGHIJ
KLMNO
PQRS 
TUVWX
AAAAABBRRRLL0
Z

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.

Solution C

#include<stdio.h>
#include<string.h>
int main()
{
	
    int i,j,k,t1,t2,t,count,f=1,x; 
	char a[5][6],m[100],tmp[100];
	gets(a[0]);
	while(a[0][0]!='Z')
	{
		count=0;
		gets(a[1]);
		gets(a[2]);    
		gets(a[3]);
		gets(a[4]);
		gets(m);
		x=strlen(m);
		while(m[x-1]!='0')
		{
			scanf("%s",m+x);
			gets(tmp);
			x=strlen(m);
		}
		
		t1=9;
		t2=9;
		for(j=0;j<5;j++)
		{
			for(k=0;k<5;k++)
			{
				if(a[j][k]==' ') 
				{
					t1=j;
					t2=k;
					break;
				}
			}
			if(t1!=9&&t2!=9) break;
		}
		
		j = t1;
		k = t2;
		
		for(i=0;m[i]!='0';i++)
		{
			
			switch(m[i])
			{
				case 'A': if(j>0)
				          {
				              t=a[j][k];
				              a[j][k]=a[j-1][k];
				              a[j-1][k]=t;
				              j--;
				          }
				          else 
						  {
						  	if(f!=1) printf("\n");
						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
						  	  f++;
						  	  count++;
				          }break;
				case 'B': if(j<5)
				          {
				              t=a[j][k];
				              a[j][k]=a[j+1][k];
				              a[j+1][k]=t;
				              j++;
				          }
				          else 
						  {
						  	  if(f!=1) printf("\n");
						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
						  	  f++;
						  	  count++;
				          }break;
				case 'R': if(k<5)
				          {
				              t=a[j][k];
				              a[j][k]=a[j][k+1];
				              a[j][k+1]=t;
				              k++;
				          }
				          else 
						  {
						  	if(f!=1) printf("\n");
						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
						  	  f++;
						  	  count++;
				          }
				          break;
				case 'L': if(k>0)
				          {
				              t=a[j][k];
				              a[j][k]=a[j][k-1];
				              a[j][k-1]=t;
				              k--;
				          }
				          else 
						  {
						  	if(f!=1) printf("\n");
						  	  printf("Puzzle #%d:\nThis puzzle has no final configuration.\n",f);
						  	  f++;
						  	  count++;
				          }break;
			}
			if(count!=0) break;
		}
		if(count==0) 
		{
			if(f!=1) printf("\n");
			printf("Puzzle #%d:\n",f);
	        for(i=0;i<5;i++)
	        {
	        	printf("%c %c %c %c %c\n",a[i][0],a[i][1],a[i][2],a[i][3],a[i][4]);
	        }
	        f++;
		}
		gets(a[0]);
	}
	return 0;
}

Solution C++


// UVa227
// 2015-2-16

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;

#define maxn 5

// 输入命令行,可以解决跨行输入命令行的问题 
void readcommand(string &str)
{
	getline(cin, str);
	while(str[str.length()-1] != '0')
	{
		string temp_str;
		getline(cin, temp_str);
		str += temp_str;
	}
}

// 判断移动后的位置合不合理
int  reasonable_site(int x, int y)
{
	if(x < 0 || x > maxn-1 || y < 0 || y > maxn-1)
	{
		return 0;
	}
	return 1;
}

int main()
{
	int kcase = 0;
	
	// 用于存储信息 
	string my_grid[maxn];
	string my_command;	// 用于存储移动的命令 
	
	// 打开文件写入,提交的时候去掉 
	// freopen("1.in", "r", stdin);
	
	while(1)
	{
		int x0, y0;		// 用于记录空格的下标 
		
		for(int i = 0; i < maxn; i++)
		{
			getline(cin, my_grid[i]);	// 这样读取以回车结束的空行,单独用cin也不读入空格 
			if(my_grid[i][0] == 'Z') exit(0);	// 输入Z结束程序 
			
			// 找到空格
			for(int j = 0; j < maxn; j++)
			{
				if(my_grid[i][j] == ' ')
				{
					x0 = i;
					y0 = j;
					break;
				}
			} 
		}
		
		// 输入命令 
		readcommand(my_command); 
		
		// 用于记录输入的命令是否合法
		int flag = 1; 
				
		// 执行命令
		for(int i = 0; i < my_command.length(); i++)
		{
			// 记录变换前的坐标 
			int x1 = x0;
			int y1 = y0;
				
			switch(my_command[i])
			{
				// A 表示向上x0-1 
				case 'A': x0--; break;
				case 'B': x0++; break;
				case 'L': y0--; break;
				case 'R': y0++;	break;
				case '0': break;
				default: flag = 0; break;		
			}
			
			// 命令不合法或者位置不对则执行错误,进行下一次输入 
			if(!flag || !reasonable_site(x0, y0))
			{
				break;
			} 
			else
			{
				char temp_ch = my_grid[x0][y0];
				my_grid[x0][y0] = my_grid[x1][y1];
				my_grid[x1][y1] = temp_ch;
			}
			
		} 
		
		if(kcase)
		{
			// 每个Puzzle之间的回车
			cout << endl; 
		}
		
		// 输出结果 
		cout << "Puzzle #" << ++kcase << ":" << endl;
		if(!flag || !reasonable_site(x0, y0)) 
		{
			cout << "This puzzle has no final configuration." << endl;
			continue;
		}
		
		for(int i = 0; i < maxn; i++)
		{
			for(int j = 0; j < maxn; j++)
			{
				cout << my_grid[i][j];
				if(j < maxn-1) cout << " ";
			}
			cout << endl;
		}
		
	}
	
	return 0;
}
Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题