Source

romensio_c / src / main.c

Full commit
  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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
 * main.c
 *
 *  Created on: Apr 30, 2012
 *      Author: stanner
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct {
	int i;
	int j;
	int value;
}Node;

//function prototypes
bool oldNeighbor(Node I[], Node n);
bool hasNeighbor(Node P[], Node n);
bool skip(Node fail[], Node n, int f_count);
void print(int **A, int n, int moves);
void removePosition(int **A, int n, int i, int j);
void solve(int **A, int n);
void getPath(int **A, Node Q[], int *q_count, int n, Node I[], int i_count);
bool inQ(Node Q[], Node n);
void sortPath(Node Q[], int q_count);
void clearFail(Node fail[], int f_count);

int main (int argc, char *argv[]) {

	FILE *fp;
	int n;
	int **A;



	if (argc == 2 ) {
		fp = fopen(argv[1], "r");
		if (fp == NULL) {
			printf("Can't open %s\n", argv[1]);
			return 1;
		}

		//read in n
		fscanf(fp, "%d", &n);
		//printf("%d", n);

		//create the game board
		A = (int**) malloc (sizeof(int) * n);
		int i;
		for (i = 0; i < n; i++) {
			A[i] = (int *) malloc (sizeof(int) * n);
		}
		int j;
		int in;
		//Populate the game board from the input file
		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {
				fscanf(fp, "%d", &in);

				A[i][j] = in;
			}
		}

		//print(*&A, n);

		//removePosition(*&A, n, 3, 3);

		solve(*&A, n);




		//Game over
		return 0;
	}
	else {
		printf("please include only the input file when executing romensio_c");
		return 1;
	}
}

void print (int **A, int n, int moves) {
	//Print out the matrix
	int i;
	int j;
	int left = 0;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {

			if (A[i][j] == 0) {
				printf("  ");
			}
			else {
				printf("%d ", A[i][j]);
				left++;
			}

		}
		printf("\n");
	}

	printf("Numbers left: %d\n", left);
	printf("Number of moves: %d\n", moves);
}

void removePosition(int **A, int n, int i, int j) {

	int p;
	for (p = i; p > 0; p--) {
		A[p][j] = A[p - 1][j];
	}
	A[p][j] = 0;

}

void solve(int **A, int n) {
	Node L;
	Node Q[10];
	Node I[100];
	Node *fail = (Node *) malloc (sizeof(Node) * n * n);
	int f_count = 0;
	int moves = 0;

	int i;
	int j;

	Node clear;
	clear.i = 0;
	clear.j = 0;
	clear.value = 0;


	int a;
	for (a = 0; a < n*n; a++) {
		L.i = 0;
		L.j = 0;
		L.value = A[0][0];

		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {
				Node t;
				t.i = i;
				t.j = j;
				t.value = A[i][j];
				if (A[i][j] > L.value && !skip(fail, t, f_count)) {
					L.i = i;
					L.j = j;
					L.value = A[i][j];
				}
			}
		}
		if (L.value == 0) {
			break;
		}

		for (i = 0; i < 10; i++) {
			Q[i] = clear;
		}
		for (i = 0; i < 100; i++) {
			I[i] = clear;
		}
		int q_count = 0;
		int i_count = 0;
		Q[0] = L;
		q_count++;
		getPath(*&A, Q, &q_count, n, I, i_count);
		//So now Q Should either have a path or be null
		sortPath(Q, q_count);
		if (q_count > 1) {
			for (i = 0; i < q_count; i++) {
				removePosition(*&A, n, Q[i].i, Q[i].j);
				clearFail(fail, f_count);
			}
			moves++;
		}
		else {

			fail[f_count] = L;
			f_count++;
		}
		//print(*&A, n, moves);
			//Now We need to find a new L and start the recusive search again

	}

	print(*&A, n, moves);

}

void sortPath(Node Q[], int q_count) {
	int i;
	Node tmp;
	for (i = 0; i < q_count; i++) {
		int j;
		for (j = i; j < q_count; j++) {
			if(Q[i].i >= Q[j].i) {
				tmp = Q[i];
				Q[i] = Q[j];
				Q[j] = tmp;
			}
		}

	}
}
void clearFail(Node fail[], int f_count) {

	int i;
	Node clear;
	clear.value = 0;
	clear.i = 0;
	clear.j = 0;

	for (i = 0; i < f_count; i++) {
		fail[i] = clear;
	}
}

void getPath(int **A,Node Q[], int *q_count, int n, Node I[], int i_count) {


	//base case if q is empty
	if (*q_count == 0) {
		return;
	}

	int n_count = 0;

	Node P[100];
	int i;
	for (i = 0; i < 100; i++) {
		P[i].i = 0;
		P[i].j = 0;
		P[i].value = 0;
	}

	for (i = 0; i < *q_count; i++) {


		//NW
		if (Q[i].i - 1 >= 0 && Q[i].j - 1 >= 0 ) {

			Node NW;
			NW.i = Q[i].i - 1;
			NW.j = Q[i].j - 1;
			NW.value = A[NW.i][NW.j];
			if (!hasNeighbor(P, NW) && !oldNeighbor(I, NW) && NW.value && !inQ(Q, NW)) {
				P[n_count] = NW;
				n_count++;
			}
		}

		//N
		if (Q[i].i - 1 >= 0) {

			Node N;
			N.i = Q[i].i - 1;
			N.j = Q[i].j;
			N.value = A[N.i][N.j];
			if (!hasNeighbor(P, N) && !oldNeighbor(I, N) && N.value && !inQ(Q, N)) {
				P[n_count] = N;
				n_count++;
			}
		}

		//S
		if (Q[i].i + 1 < n) {
			Node S;
			S.i = Q[i].i + 1;
			S.j = Q[i].j;
			S.value = A[S.i][S.j];
			if (!hasNeighbor(P, S) && !oldNeighbor(I, S) && S.value && !inQ(Q, S)) {
				P[n_count] = S;
				n_count++;
			}
		}

		//NE
		if (Q[i].i - 1 >= 0 && Q[i].j + 1 < n) {

			Node NE;
			NE.i = Q[i].i - 1;
			NE.j = Q[i].j + 1;
			NE.value = A[NE.i][NE.j];
			if (!hasNeighbor(P, NE) && !oldNeighbor(I, NE) && NE.value && !inQ(Q, NE)) {
				P[n_count] = NE;
				n_count++;
			}
		}

		//W
		if (Q[i].j - 1 >= 0) {

			Node W;
			W.i = Q[i].i;
			W.j = Q[i].j - 1;
			W.value = A[W.i][W.j];
			if (!hasNeighbor(P, W) && !oldNeighbor(I, W) && W.value && !inQ(Q, W)) {
				P[n_count] = W;
				n_count++;
			}
		}
		//E
		if (Q[i].j + 1 < n) {

			Node E;
			E.i = Q[i].i;
			E.j = Q[i].j + 1;
			E.value = A[E.i][E.j];
			if (!hasNeighbor(P, E) && !oldNeighbor(I, E) && E.value && !inQ(Q, E)) {
				P[n_count] = E;
				n_count++;
			}
		}

		//SW
		if (Q[i].i + 1 < n && Q[i].j - 1 >= 0) {

			Node SW;
			SW.i = Q[i].i + 1;
			SW.j = Q[i].j - 1;
			SW.value = A[SW.i][SW.j];
			if (!hasNeighbor(P, SW) && !oldNeighbor(I, SW) && SW.value && !inQ(Q, SW)) {
				P[n_count] = SW;
				n_count++;
			}
		}

		//SE
		if (Q[i].i + 1 < n && Q[i].j + 1 < n) {

			Node SE;
			SE.i = Q[i].i + 1;
			SE.j = Q[i].j + 1;
			SE.value = A[SE.i][SE.j];
			if (!hasNeighbor(P, SE) && !oldNeighbor(I, SE) && SE.value && !inQ(Q, SE)) {
				P[n_count] = SE;
				n_count++;
			}
		}
	}
	//Now P is populated with all the Neighbors of the Nodes in Q

	Node T;
	T.value = 10;

	//Pick the smallest T
	for (i = 1; i < n_count; i++) {
		if (T.value > P[i].value && P[i].value != 0) {
			T = P[i];
		}
	}

	if (T.value == 10) {
		return;
	}

	//get the weight of Q
	int q_weight = 0;
	for (i = 0; i < *q_count; i++ ) {
		q_weight = q_weight + Q[i].value;

	}

	//Check the new weight
	int t_weight = q_weight + T.value;
	if (t_weight < 10) {
		Q[*q_count] = T;
		*q_count = *q_count + 1;
		return getPath(A, Q, q_count, n, I, i_count);
	}
	else if (t_weight > 10) {
		*q_count = *q_count - 1;
		Node last = Q[*q_count];
		I[i_count] = last;
		i_count++;
		Q[*q_count].i = 0;
		Q[*q_count].j = 0;
		Q[*q_count].value = 0;

		return getPath(A, Q, q_count, n, I, i_count);

	}
	else {
		Q[*q_count] = T;
		*q_count = *q_count + 1;
		return;
	}


}


//Check the set of neighbors for existing ones
bool hasNeighbor(Node P[], Node n) {
	int i;
	for (i = 0; i < 100; i++) {
		Node c = P[i];
		if (c.i == n.i && c.j == n.j && c.value == n.value) {
			return true;
		}
	}
	return false;
}

bool inQ(Node Q[], Node n) {
	int i;
	for (i = 0; i < 10; i++) {
		Node c = Q[i];
		if (c.i == n.i && c.j == n.j && c.value == n.value) {
			return true;
		}
	}
	return false;
}

bool skip(Node fail[], Node n, int f_count) {
	int i;
	if (!f_count) {
		return false;
	}
	for (i = 0; i < f_count; i++) {
		if (fail[i].value == n.value && fail[i].i == n.i && fail[i].j == n.j) {
			return true;
		}
	}
	return false;
}

//search the sed of old neighbors for exisiting ones.
bool oldNeighbor(Node I[], Node n) {
	int i;
	for (i = 0; i < 100; i++) {
		Node c = I[i];
		if (c.i == n.i && c.j == n.j && c.value == n.value) {
			return true;
		}
	}
	return false;
}