Uva227.Puzzle

题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=163

#   Problem Verdict Language Run Time Submission Date
13767338 227 Puzzle Accepted C++ 0.026 2014-06-19 02:39:04
13766276 227 Puzzle Wrong answer C++ 0.135 2014-06-18 16:48:26

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 #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.


解题思路:简单模拟题。花点时间敲就好。坑爹的是输入的时候每次gets可能会吃上一行的回车。注意一下。

     自己在做的时候写了两版,第一版比较冗杂,最后WA也不知道什么原因(没有读到eof怎么也应该是TLE)。

     第二版的输入输出方法是对拍网上大神的,并自己重敲了一会。还要好好的去学习简化代码的方法。AC才是硬道理啊!


 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cctype>
 5 #include <cmath>
 6 #include <string>
 7 #include <algorithm>
 8 #include <numeric>
 9 
10 using namespace std;
11 
12 int main() {
13     string Map[5];
14     //char Map[5];
15     int x_s, y_s, cnt = 0;
16     while(1) {
17         /*getline(cin, Map[0]);
18         for(int i = 0; i < 5; i++) {
19             if(Map[0][i] == ' ') {
20                 x_s = 0; y_s = i;
21             }
22         }*/
23         //memset(Map, 1, sizeof(Map));
24         if(cnt) getchar();
25         if(cnt != 0) cout << endl;
26         for(int i = 0; i < 5; i++) {
27             //gets(Map[i]);
28             getline(cin, Map[i]);
29 
30             if(!i && Map[0].size() == 1) {
31                 return 0;
32             }
33             for(int j = 0; j < 5; j++) {
34                 if(Map[i][j] == ' ') {
35                     x_s = i; y_s = j;
36                 }
37             }
38         }
39 
40 
41         string op_now, op = "";
42         while(1) {
43             cin >> op_now;
44             op += op_now;
45             if(op_now[op_now.size() - 1] == '0') {
46                 break;
47             }
48         }
49 
50         bool flag = false;
51         for(int i = 0; op[i] != '0'; i++) {
52             if(op[i] == 'B') {
53                 x_s += 1;
54                 if(x_s > 4) flag = true;
55                 if(!flag) swap(Map[x_s - 1][y_s], Map[x_s][y_s]);
56             } else if(op[i] == 'A') {
57                 x_s -= 1;
58                 if(x_s < 0) flag = true;
59                 if(!flag) swap(Map[x_s + 1][y_s], Map[x_s][y_s]);
60             } else if(op[i] == 'R') {
61                 y_s += 1;
62                 if(y_s > 4) flag = true;
63                 if(!flag) swap(Map[x_s][y_s - 1], Map[x_s][y_s]);
64             } else if(op[i] == 'L') {
65                 y_s -= 1;
66                 if(y_s < 0) flag = true;
67                 if(!flag) swap(Map[x_s][y_s + 1], Map[x_s][y_s]);
68             }
69             if(flag) break;
70         }
71 
72         //cout << "Puzzle #:"
73         printf("Puzzle #%d:
", ++cnt);
74         if(flag) {
75             cout << "This puzzle has no final configuration." << endl;
76         } else {
77             for(int i = 0; i < 5; i++) {
78                 for(int j = 0; j < 5; j++) {
79                     if(!j) cout << Map[i][j];
80                     else cout << " " << Map[i][j];
81                 }
82                 cout << endl;
83             }
84         }
85     }
86     return 0;
87 }
WA代码

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 int main () {
  5     int cases = 0;
  6     bool line = false;
  7     char initial [5] [7];
  8 
  9     while ( gets (initial [0]) ) {
 10 
 11         if ( strcmp (initial [0], "Z") == 0 )
 12             return 0;
 13 
 14         gets (initial [1]);
 15         gets (initial [2]);
 16         gets (initial [3]);
 17         gets (initial [4]);
 18 
 19         int blank_x;
 20         int blank_y;
 21 
 22         for ( int i = 0; i < 5; i++ ) {
 23             for ( int j = 0; j < 5; j++ ) {
 24                 if ( initial [i] [j] == ' ' ) {
 25                     blank_x = i;
 26                     blank_y = j;
 27                     i = j = 5;
 28                 }
 29             }
 30         }
 31 
 32         char command [1000];
 33         bool valid = true;
 34         bool exit_koro = false;
 35 
 36         while ( !exit_koro && gets (command)) {
 37 
 38             for ( int i = 0; command [i] != 0; i++ ) {
 39 
 40                 if ( command [i] == '0' || !valid ) {
 41                     exit_koro = true;
 42                     break;
 43                 }
 44 
 45                 switch (command [i]) {
 46                 case 'A' :
 47                     if ( blank_x == 0 )
 48                         valid = false;
 49                     else {
 50                         initial [blank_x] [blank_y] = initial [blank_x - 1] [blank_y];
 51                         initial [blank_x - 1] [blank_y] = ' ';
 52                         blank_x--;
 53                     }
 54                     break;
 55 
 56                 case 'B' :
 57                     if ( blank_x == 4 )
 58                         valid = false;
 59                     else {
 60                         initial [blank_x] [blank_y] = initial [blank_x + 1] [blank_y];
 61                         initial [blank_x + 1] [blank_y] = ' ';
 62                         blank_x++;
 63                     }
 64                     break;
 65 
 66                 case 'R' :
 67                     if ( blank_y == 4 )
 68                         valid = false;
 69                     else {
 70                         initial [blank_x] [blank_y] = initial [blank_x] [blank_y + 1];
 71                         initial [blank_x] [blank_y + 1] = ' ';
 72                         blank_y++;
 73                     }
 74                     break;
 75 
 76                 case 'L' :
 77                     if ( blank_y == 0 )
 78                         valid = false;
 79                     else {
 80                         initial [blank_x] [blank_y] = initial [blank_x] [blank_y - 1];
 81                         initial [blank_x] [blank_y - 1] = ' ';
 82                         blank_y--;
 83                     }
 84                     break;
 85                 }
 86             }
 87         }
 88 
 89         if ( line )
 90             printf ("
");
 91         line = true;
 92 
 93         printf ("Puzzle #%d:
", ++cases);
 94 
 95         if ( valid ) {
 96             for ( int i = 0; i < 5; i++ ) {
 97                 printf ("%c %c %c %c %c
", initial [i] [0], initial [i] [1],
 98                         initial [i] [2], initial [i] [3], initial [i] [4]);
 99             }
100         }
101 
102         else
103             printf ("This puzzle has no final configuration.
");
104 
105     }
106 
107     return 0;
108 }
AC代码



原文地址:https://www.cnblogs.com/Destiny-Gem/p/3796132.html