-
Notifications
You must be signed in to change notification settings - Fork 0
/
101.cpp
119 lines (100 loc) · 2.76 KB
/
101.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <stdlib.h>
using namespace std;
int a, b, Ax, Ay, Bx, By, len, block[25][25];
void search(void)
{
for (int i = 0; i < len; ++i)
for (int j = 0; j < len; ++j) {
if (block[i][j] == -1) break;
if (block[i][j] == a) {
Ax = i;
Ay = j;
}
if (block[i][j] == b) {
Bx = i;
By = j;
}
}
}
int main(int argc, char const *argv[])
{
char word[5];
int tmp, tmp_b;
cin >> len;
for (int i = 0; i < len; ++i) {
block[i][0] = i;
for (int j = 1; j < len; ++j)
block[i][j] = -1;
}
cin >> word;
while (word[0] != 'q') {
if (word[0] == 'm') {
cin >> a >> word >> b;
search();
if (Ax == Bx) {
cin >> word;
continue;
}
tmp = Ay + 1;
tmp_b = block[Ax][tmp];
while (tmp_b > -1) {
block[tmp_b][0] = tmp_b;
block[Ax][tmp++] = -1;
tmp_b = block[Ax][tmp];
}
if (word[1] == 'n') { // move a onto b
tmp = By + 1;
tmp_b = block[Bx][tmp];
while (tmp_b > -1) {
block[tmp_b][0] = tmp_b;
block[Bx][tmp++] = -1;
tmp_b = block[Bx][tmp];
}
By++;
}
else { // move a over b
while (block[Bx][By] > -1)
By++;
}
block[Ax][Ay] = -1;
block[Bx][By] = a;
}
else {
cin >> a >> word >> b;
search();
if (Ax == Bx) {
cin >> word;
continue;
}
if (word[1] == 'n') { // pile a onto b
tmp = By + 1;
tmp_b = block[Bx][tmp];
while (tmp_b > -1) {
block[tmp_b][0] = tmp_b;
block[Bx][tmp++] = -1;
tmp_b = block[Bx][tmp];
}
By++;
}
else { // pile a over b
while (block[Bx][By] > -1)
By++;
}
tmp_b = block[Ax][Ay];
while (tmp_b > -1) {
block[Bx][By++] = tmp_b;
block[Ax][Ay++] = -1;
tmp_b = block[Ax][Ay];
}
}
cin >> word;
}
for (int i = 0; i < len; ++i) {
cout << i << ":";
for (int j = 0; j < len; ++j)
if (block[i][j] > -1) cout << " " << block[i][j];
cout << endl;
}
return 0;
}