-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP227.cpp
79 lines (78 loc) · 1.56 KB
/
P227.cpp
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
int main() {
PI empty;
char board[5][5];
string line;
for(int cas = 1; true; ++cas) {
getline(cin, line);
if(line[0] == 'Z')
return 0;
FORY(5) {
FORX(5) {
board[y][x] = line[x];
if(line[x] == ' ')
empty = PI(x,y);
}
getline(cin, line);
}
//cerr << empty.XX << "," << empty.YY << endl;
bool ok = true;
while(true) {
FORUI(line.size() && ok) {
char move = line[i];
if(move == '0')
break;
switch(move) {
case 'A':
if(empty.YY == 0)
ok = false;
else {
swap(board[empty.YY][empty.XX], board[empty.YY-1][empty.XX]);
--empty.YY;
}
break;
case 'L':
if(empty.XX == 0)
ok = false;
else {
swap(board[empty.YY][empty.XX], board[empty.YY][empty.XX-1]);
--empty.XX;
}
break;
case 'B':
if(empty.YY == 4)
ok = false;
else {
swap(board[empty.YY][empty.XX], board[empty.YY+1][empty.XX]);
++empty.YY;
}
break;
default:
if(empty.XX == 4)
ok = false;
else {
swap(board[empty.YY][empty.XX], board[empty.YY][empty.XX+1]);
++empty.XX;
}
break;
} // switch
} // for line && ok
if(line[line.size()-1] == '0')
break;
//cerr << "'" << line[line.size()-1] << "'" << endl;
getline(cin, line);
} // while true
if(cas != 1)
cout << endl;
cout << "Puzzle #" << cas << ":" << endl;
if(!ok)
cout << "This puzzle has no final configuration." << endl;
else {
FORY(5) {
cout << board[y][0];
FORX(4)
cout << " " << board[y][x+1];
cout << endl;
}
}
}
}