首页 > 其他 > 详细

Uva 227-Puzzle

时间:2015-02-08 16:41:13      阅读:354      评论:0      收藏:0      [点我收藏+]
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.

本题思路很清晰但有一些细节要注意:
1.输入每一行后面有一个换行符‘\n‘,不要把它读进数组里
2.输入完一行指令后,指令要重置为初始状态
3.别忘了输入完指令‘0‘后有一个‘\n‘在输入流中
 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     char a[5][5], o  =  ;   //o为输入命令
 8     int p = -1, q = -1, Count = 0, sign = 0,one = 0;           //p为行坐标,q为列坐标,sign为非法标记
 9     while(1)
10     {
11         ++Count;
12         for(int i = 0; i < 5; i++)
13         {                     //输入
14 
15          for(int j = 0; j < 5; j ++)
16          {
17 
18              cin >> noskipws >> a[i][j];
19              if(a[i][j] ==  )
20              {
21                  p = i;
22                  q = j;
23 
24              }
25              if(a[i][j] == Z) return 0;
26          }
27                       cin.get();        //1.每输入一行,吸收一个换行符
28         }
29 
30         //getchar();
31         //for(int i = 0;i < 5; i++)
32         //  gets(a[i]);
33 
34         while(o != 0)
35         {                   //移动
36             cin >> o;
37             switch(o)
38             {
39              case A:
40                 a[p][q] = a[p-1][q];
41                 a[--p][q] =  ;
42                 break;
43              case B:
44                 a[p][q] = a[p+1][q];
45                 a[++p][q] =  ;
46                 break;
47              case L:
48                 a[p][q] = a[p][q-1];
49                 a[p][--q] =  ;
50                 break;
51              case R:
52                 a[p][q] = a[p][q+1];
53                 a[p][++q] =  ;
54                 break;
55              default:
56                 break;
57             }
58             if(p < 0 || p > 4 || q < 0 || q > 4)
59                 sign = 1;                     //非法标记
60         }
61         o =  ;                 //2.
62         if(one) cout << endl;
63         else one = 1;
64         cin.get();              //3.卧槽
65         cout << "Puzzle #" << Count << ":" << endl;
66         if(!sign)
67         {
68            for(int i = 0; i < 5; i++)
69            {                                  //输出
70               for(int j = 0; j < 5; j++)
71               {
72                 cout << a[i][j] <<  ;
73               }
74                cout << endl;
75            }
76         }
77         else
78             cout << "This puzzle has no final configuration." << endl;
79 
80     }
81     return 0;
82 
83 }

 


 

Uva 227-Puzzle

原文:http://www.cnblogs.com/ekinzhang/p/4280118.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!