UVa227

UVa227

一道我目前为止写过的最长代码的题,小小纪念一下,思路很简单,就是模拟,但是他的输出格式,我不是很明白,是复制网友代码然后发现的规律。代码很简单,应该可以看懂,orz

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
104
105
#include<iostream>
#include<stdio.h>
using namespace std;
char s[6][6];
struct blank
{
int row;//行
int col;//列
}blank;
int input()
{
int flag2 = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++)
{
s[i][j] = getchar();
if (s[i][j] == 'Z') {
flag2 = 1; break;
}
if (s[i][j] == ' ') //指向空白格
{
blank.col = j;
blank.row = i;
}
if (j == 5)getchar();
}
if (flag2 == 1)break;
}

return flag2;
}
int move()
{
char move;
int flag = 0;
while(cin>>move)
{
if (move == '0')break;
switch (move) {
case('A'): if (blank.row != 1)
{
s[blank.row][blank.col] = s[blank.row-1][blank.col];
s[--blank.row][blank.col] = ' ';
}
else flag = 1;
break;//上
case('B'): if (blank.row != 5)
{
s[blank.row][blank.col] = s[blank.row+1][blank.col];
s[++blank.row][blank.col] = ' ';
}
else flag = 1;
break;//下
case('R'): if (blank.col != 5)
{
s[blank.row][blank.col] = s[blank.row][blank.col+1];
s[blank.row][++blank.col] = ' ';
}
else flag = 1;
break;//右
case('L'): if (blank.col != 1)
{
s[blank.row][blank.col] = s[blank.row][blank.col-1];
s[blank.row][--blank.col] = ' ';
}
else flag = 1;
break;//左
}
}
return flag;
}
void output(int count)
{
if (count > 1)cout << endl;
printf("Puzzle #%d:\n", count);
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j == 5)cout << s[i][j] << endl;
else cout << s[i][j] << " ";
}
}
}
int main()
{
int count = 1;
for (;;) {
int t1,t2;

t1 = input();
if (t1)break;

t2=move();
if (t2==0)output(count++);
else {
if (count > 1)cout << endl;
printf("Puzzle #%d:\n", count++);
cout << "This puzzle has no final configuration." << endl;
}
getchar();
}

return 0;
}