UVa 227 - Puzzle

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

直接下去模擬即可。

Code

UVa 227
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<cstdio>
#define isOk(x,y) (x&&y&&x<=5&&y<=5)

inline void swap(char& obj, char& tar)
{
char temp = obj;
obj = tar;
tar = temp;
}
int main()
{
char puzzle[7][7], c;
int Case = 0;
while ((c = getchar()) != 'Z')
{
int i, j, raw, col;
puzzle[1][1] = c;
if (c == ' ')
col = raw = 1;
for (j = 2; j <= 5; j++)
if ((puzzle[1][j] = getchar()) == ' ')
raw = 1, col = j;
getchar();
for (i = 2; i <= 5; i++)
{
for (j = 1; j <= 5; j++)
if ((puzzle[i][j] = getchar()) == ' ')
raw = i, col = j;
getchar();
}

bool flag = true;
while ((c = getchar()) != '0')
{
if (!flag || c == '\n')
continue;

if (c == 'A')//上面移下來
{
if (isOk(raw - 1, col))
{
swap(puzzle[raw][col], puzzle[raw - 1][col]);
raw = raw - 1;
}
else
flag = false;
}
else if (c == 'B')//下面移上去
{
if (isOk(raw + 1, col))
{
swap(puzzle[raw][col], puzzle[raw + 1][col]);
raw = raw + 1;
}
else
flag = false;
}
else if (c == 'R')//右邊往左移
{
if (isOk(raw, col + 1))
{
swap(puzzle[raw][col], puzzle[raw][col + 1]);
col = col + 1;
}
else
flag = false;
}
else if (c == 'L')//左邊往右移
{
if (isOk(raw, col - 1))
{
swap(puzzle[raw][col], puzzle[raw][col - 1]);
col = col - 1;
}
else
flag = false;
}
}
getchar();

if (Case)
putchar('\n');

printf("Puzzle #%d:\n", ++Case);
if (!flag)
puts("This puzzle has no final configuration.");
else
{
for (i = 1; i <= 5; i++)
{
for (j = 1; j < 5; j++)
{
putchar(puzzle[i][j]);
putchar(' ');
}
putchar(puzzle[i][5]);
putchar('\n');
}
}
}

return 0;
}