/home/toolbox/public_html/solutions/4/439/a.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 #include <ctype.h>
10
11 #define TRUE (1 == 1)
12 #define FALSE (1 != 1)
13
14 #define DEBUG if (FALSE)
15
16 #define MAX_LINE 257
17
18 /*
19 * Author: Isaac Traxler
20 * Date: 2021-09-03
21 * Purpose: fun
22 * Problem: 439
23 */
24
25 /*
26 * This template reads lines of data at a time until end of file.
27 */
28
29 #define UNKNOWN -88
30
31 char line[MAX_LINE];
32 char xx[3];
33 char yy[3];
34 int xx1;
35 int xx2;
36 int yy1;
37 int yy2;
38 int board[10][10];
39 int dx[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
40 int dy[8] = { 2, 2, 1, 1, -1, -1, -2, -2};
41
42 void init()
43 {
44 /* FUNCTION init */
45 int i;
46 int j;
47
48 for (i=1; 9>i; i++)
49 {
50 /* for i */
51 for (j=1; 9>j; j++)
52 {
53 /* for j */
54 board[i][j] = UNKNOWN;
55 } /* for j */
56 } /* for i */
57 } /* FUNCTION init */
58
59 void dump()
60 {
61 /* FUNCTION dump */
62 int i;
63 int j;
64
65 for (i=1; 9>i; i++)
66 {
67 /* for i */
68 for (j=1; 9>j; j++)
69 {
70 /* for j */
71 printf(" %3d", board[i][j]);
72 } /* for j */
73 printf("\n");
74 } /* for i */
75 } /* FUNCTION dump */
76
77 int getInput()
78 {
79 /* FUNCTION getInput */
80 int dataReadFlag;
81
82 fgets(line, MAX_LINE, stdin);
83 if (feof(stdin))
84 dataReadFlag = FALSE;
85 else
86 {
87 /* something to read */
88 dataReadFlag = TRUE;
89 line[strlen(line)-1] = 0;
90 xx[0] = line[0];
91 xx[1] = line[1];
92 xx[2] = 0;
93 yy[0] = line[3];
94 yy[1] = line[4];
95 yy[2] = 0;
96 } /* something to read */
97 return (dataReadFlag);
98 } /* FUNCTION getInput */
99
100 int xlateLetter(char z)
101 {
102 /* FUNCTION xlateLetter */
103 int toReturn;
104
105 switch (z)
106 {
107 /* switch */
108 case 'a':
109 toReturn = 1;
110 break;
111 case 'b':
112 toReturn = 2;
113 break;
114 case 'c':
115 toReturn = 3;
116 break;
117 case 'd':
118 toReturn = 4;
119 break;
120 case 'e':
121 toReturn = 5;
122 break;
123 case 'f':
124 toReturn = 6;
125 break;
126 case 'g':
127 toReturn = 7;
128 break;
129 case 'h':
130 toReturn = 8;
131 break;
132 } /* switch */
133 return (toReturn);
134 } /* FUNCTION xlateLetter */
135
136 void xlate()
137 {
138 /* FUNCTION xlate */
139 xx1 = xlateLetter(xx[0]);
140 xx2 = xx[1] - '0';
141 yy1 = xlateLetter(yy[0]);
142 yy2 = yy[1] - '0';
143 } /* FUNCTION xlate */
144
145 int setMove(int x, int y, int cnt)
146 {
147 /* FUNCTION setMove */
148 int changed = FALSE;
149 int i;
150 int tx;
151 int ty;
152
153 DEBUG printf("in set Move with %d,%d checking moves for %d\n", x, y, cnt);
154 for (i=0; 8>i; i++)
155 {
156 /* step through all 8 possible moves */
157 tx = x + dx[i];
158 ty = y + dy[i];
159 DEBUG printf("Trying %d,%d\n", tx, ty);
160 if ((0 < tx) && (9 > tx) && (0 < ty) && (9 > ty))
161 {
162 /* valid square to be moved to */
163 DEBUG printf("board[%d][%d] is currently %d\n", tx, ty, board[tx][ty]);
164 if (UNKNOWN == board[tx][ty])
165 {
166 /* found an empty square */
167 changed = TRUE;
168 board[tx][ty] = cnt;
169 DEBUG printf("SETTING: board[%d][%d] set to %d\n", tx, ty, cnt);
170 } /* found an empty square */
171 } /* valid square to be moved to */
172 } /* step through all 8 possible moves */
173 return changed;
174 } /* FUNCTION setMove */
175
176 void fill(int cnt)
177 {
178 /* FUNCTION fill */
179 int changed = FALSE;
180 int tmp;
181 int i;
182 int j;
183
184 DEBUG printf("fill level %d\n", cnt);
185 DEBUG dump();
186 for (i=1; 9>i; i++)
187 {
188 /* for i */
189 for (j=1; 9>j; j++)
190 {
191 /* for j */
192 if (cnt == board[i][j])
193 {
194 /* found the level I am at */
195 DEBUG printf("will try %d,%d to %d\n", i, j, cnt + 1);
196 tmp = setMove(i, j, cnt + 1);
197 changed = changed || tmp;
198 } /* found the level I am at */
199 } /* for j */
200 } /* for i */
201 if ((changed) && (UNKNOWN == board[yy1][yy2]))
202 {
203 /* changed something -- try until no changes */
204 fill(cnt + 1);
205 } /* changed something -- try until no changes */
206 } /* FUNCTION fill */
207
208 void process()
209 {
210 /* FUNCTION process */
211 /* To get from e2 to e4 takes 2 knight moves. */
212 init();
213 xlate();
214 board[xx1][xx2] = 0;
215 DEBUG printf("board[%d][%d] set to 0 to start things off\n", xx1, xx2);
216 fill(0);
217 printf("To get from %s to %s takes %d knight moves.\n", xx, yy, board[yy1][yy2]);
218 } /* FUNCTION process */
219
220 int main()
221 {
222 /* main */
223 int moreToDo;
224
225 init();
226 moreToDo = getInput();
227 while (moreToDo)
228 {
229 /* while */
230 process();
231 moreToDo = getInput();
232 } /* while */
233
234 return EXIT_SUCCESS;
235 } /* main */
236