-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101.cpp
129 lines (114 loc) · 2.68 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
120
121
122
123
124
125
126
127
128
129
/*
The Input:
The input begins with an integer n on a line by itself
representing the number of blocks in the block world.
You may assume that 0 < n < 25.
The number of blocks is followed by a sequence of block
commands, one command per line. Your program should process
all commands until the quit command is encountered.
You may assume that all commands will be of the form
specified above. There will be no syntactically incorrect
commands.
*/
#include <iostream>
#include <string.h>
using namespace std;
static int blk[25][25];
static int loc[25];
static int top[25];
static void reset_all(int n)
{
memset((void *)blk, -1, sizeof(int)*n*25);
for (int i=0; i<n; i++) {
blk[i][0]=i;
loc[i]=i;
top[i]=0;
}
}
static void show_result(int n)
{
for (int i=0; i<n; i++) {
cout << i << ":";
for (int j=0; blk[i][j]!=-1; j++) cout << " " << blk[i][j];
cout << endl;
}
}
static void reset_block(int a)
{
for (int j=loc[a], i=top[j]; blk[j][i]!=a; i--, top[j]--) {
blk[blk[j][i]][0]=blk[j][i];
blk[j][i]=-1;
loc[blk[j][i]]=blk[j][i];
top[blk[j][i]]=0;
}
}
static void pop_block(int a)
{
blk[loc[a]][top[loc[a]]]=-1;
top[loc[a]]--;
}
static void push_block(int a, int b)
{
top[loc[b]]++;
blk[loc[b]][top[loc[b]]]=a;
loc[a]=loc[b];
}
static void pile_block(int a, int b)
{
int i, j;
if (loc[a] == loc[b]) return;
for (i=top[loc[a]], j=1; blk[loc[a]][i]!=a; i--, j++) loc[blk[loc[a]][i]]=loc[b];
memcpy((void *)&blk[loc[b]][top[b]+1], (void *)&blk[loc[a]][i], j*sizeof(int));
memset((void *)&(blk[loc[a]][i]), -1, j*sizeof(int));
top[loc[a]]-=j;
top[loc[b]]+=j;
loc[a]=b;
}
void move_onto(int a, int b)
{
reset_block(a);
reset_block(b);
blk[loc[a]][0]=-1;
blk[loc[b]][1]=a;
loc[a]=b;
top[loc[b]]++;
}
void move_over(int a, int b)
{
reset_block(a);
pop_block(a);
push_block(a, b);
}
void pile_onto(int a, int b)
{
reset_block(b);
pile_block(a, b);
}
void pile_over(int a, int b)
{
pile_block(a, b);
}
int main(void)
{
int n, a, b;
string act, opt;
while (cin >> n) {
reset_all(n);
while (cin >> act) {
if (act == "quit") {
show_result(n);
break;
}
cin >> a >> opt >> b;
if (act == "move") {
if (opt == "onto") move_onto(a, b);
if (opt == "over") move_over(a, b);
}
if (act == "pile") {
if (opt == "onto") pile_onto(a, b);
if (opt == "over") pile_over(a, b);
}
}
};
return 0;
}