/home/toolbox/public_html/solutions/1/101/d.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdint.h>
7 #include <math.h>
8 #include <stdlib.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 #define MAX_LINE 256
16
17 #define MOVE 0
18 #define PILE 3
19 #define ONTO 1
20 #define OVER 2
21
22 /*
23 move onto 1 0 + 1
24 move over 2 0 + 2
25 pile onto 4 3 + 1
26 pile over 5 3 + 2
27 */
28
29 #define MAX_BLOCKS 30
30
31 /*
32 * Author: Isaac Traxler
33 * Date: 2014-10-30
34 * Purpose: fun
35 * Problem: 101 - The Blocks Problem
36 */
37
38 /*
39 * This template reads data until a terminating value is reached.
40 */
41
42 int size;
43 int cmd;
44 int a;
45 int b;
46 int blocks[MAX_BLOCKS][MAX_BLOCKS];
47 char commands[4][5] = { "move", "onto", "over", "pile" };
48
49
50 void dump()
51 {
52 /* FUNCTION dump */
53 } /* FUNCTION dump */
54
55 int getInput()
56 {
57 /* FUNCTION getInput */
58 int moreToDO;
59 char t1[10];
60 char t2[10];
61 int c1;
62 int c2;
63
64 scanf(" %s ", t1);
65 moreToDo = 'q' != t1[0];
66 if (moreToDo)
67 {
68 /* got a command */
69 scanf(" %d %s %d ", &a, t2, &b);
70 if ('m' == t1[0])
71 {
72 /* move */
73 c1 = MOVE;
74 } /* move */
75 else
76 {
77 /* pile */
78 if ('p' != t1[0])
79 {
80 a=0; /* die if command is not move or pile */
81 b = b/a;
82 }
83 c1 = PILE;
84 } /* pile */
85 if ('n' == t2[1])
86 {
87 /* move */
88 c2 = ONTO;
89 } /* move */
90 else
91 {
92 /* pile */
93 if ('v' != t2[2])
94 {
95 a=0; /* die if command is not move or pile */
96 b = b/a;
97 }
98 c2 = OVER;
99 } /* pile */
100 cmd = c1 + c2;
101 printf("%s %d %s %d -- %d\n", commands[c1], a, commands[c2], b, cmd);
102 } /* got a command */
103 return moreToDo;
104 } /* FUNCTION getInput */
105
106 void init()
107 {
108 /* FUNCTION init */
109 int i;
110 int j;
111
112 scanf(" %d ", &size);
113 for (i=0; i<size; i++)
114 {
115 /* for each i */
116 blocks[i][0] = 1;
117 blocks[i][1] = i;
118 for (j=2; j<size; j++)
119 {
120 /* for each j */
121 blocks[i][j] = 0;
122 } /* for each j */
123 } /* for each i */
124 } /* FUNCTION init */
125
126 int fnd(int f)
127 {
128 /* FUNCTION fnd */
129 int ret = -1;
130 int i;
131 int j;
132
133 while (-1 = ret)
134 {
135 /* while */
136 for (i=0; i<size; i++)
137 {
138 /* look in each column */
139 for (j=1; j<=blocks[i][0]; j++)
140 {
141 /* for each stack */
142 if (f == block[i][j])
143 {
144 /* found it */
145 ret = i;
146 } /* found it */
147 } /* for each stack */
148 } /* look in each column */
149 } /* while */
150 return ret;
151 } /* FUNCTION fnd */
152
153 void push(int pos, int val)
154 {
155 /* FUNCTION push */
156 /* This function takes the val(ue) passed and places it on top of
157 the indicated stack (pos) */
158 blocks[pos][0] = blocks[pos][0] + 1;
159 blocks[pos][blocks[pos][0]] = val;
160 } /* FUNCTION push */
161
162 void clear(int pos, int num)
163 {
164 /* FUNCTION clear */
165 /* This function will take all values in stack pos above num and
166 return them to the top of the stack they started in */
167 int i;
168
169 for (i=blocks[pos][0]; num != blocks[pos][i]; i--)
170 {
171 /* for */
172 push(blocks[pos][i], blocks[pos][i]);
173 blocks[pos][0] = blocks[pos][0] - 1;
174 } /* for */
175 } /* FUNCTION clear */
176
177 void remove(int pos, int val)
178 {
179 /* FUNCTION remove */
180 /* This function will pop the top of the stack (making sure value is right) */
181 if (val != blocks[pos][blocks[pos][0]])
182 {
183 return -1;
184 }
185 blocks[pos][0] = blocks[pos][0] - 1;
186 } /* FUNCTION remove */
187
188
189 void process()
190 {
191 /* FUNCTION process */
192 int pa;
193 int pb;
194
195 pa = fnd(a);
196 pb = fnd(b);
197 if ((a != b) && (pa != pb))
198 {
199 /* ignore commands where a==b or and b are in same stack */
200 switch (cmd)
201 {
202 /* switch */
203 case MOVE+OVER:
204 {
205 /* MOVER + OVER */
206 clear(pa, a);
207 remove(pa, a);
208 push(pb, a);
209 break;
210 } /* MOVER + OVER */
211 case MOVE+ONTO:
212 {
213 /* MOVER + ONTO */
214 clear(pa, a);
215 clear(pb, b);
216 remove(pa, a);
217 push(pb, a);
218 break;
219 } /* MOVER + ONTO */
220 case PILE+OVER:
221 {
222 /* PILE + OVER */
223 shft(a,b);
224 break;
225 } /* PILE + OVER */
226 case PILE+ONTO:
227 {
228 /* PILE + ONTO */
229 break;
230 } /* PILE + ONTO */
231 } /* switch */
232 } /* ignore commands where a==b or and b are in same stack */
233
234 } /* FUNCTION process */
235
236 int main ()
237 {
238 /* main */
239 int moreToDo;
240
241 init();
242 moreToDo = getInput();
243 while (moreToDo)
244 {
245 /* while */
246 process();
247 moreToDo = getInput();
248 } /* while */
249
250 return EXIT_SUCCESS;
251 } /* main */
252