Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

The Spot Game

141.c

/* 141 - The Spot Game */
#include <stdio.h>
#include <string.h>

typedef char board[52][52];

int n;
board boards[105];

int winner, move;

int
pat1(int a, int b) {
	int i,j;
	for (i=0; i<n; i++) {
		for (j=0; j<n; j++) {
			if (boards[a][i][j] != boards[b][i][j]) {
				return 0;
			}
		}
	}
	return 1;
}
int
pat2(int a, int b) {
	int i,j;
	for (i=0; i<n; i++) {
		for (j=0; j<n; j++) {
			if (boards[a][i][j] != boards[b][n-j-1][i]) {
				return 0;
			}
		}
	}
	return 1;
}
int
pat3(int a, int b) {
	int i,j;
	for (i=0; i<n; i++) {
		for (j=0; j<n; j++) {
			if (boards[a][i][j] != boards[b][j][n-i-1]) {
				return 0;
			}
		}
	}
	return 1;
}
int
pat4(int a, int b) {
	int i,j;
	for (i=0; i<n; i++) {
		for (j=0; j<n; j++) {
			if (boards[a][i][j] != boards[b][n-i-1][n-j-1]) {
				return 0;
			}
		}
	}
	return 1;
}

int
pattern(int a, int b) {
	if (pat1(a,b) || pat2(a,b) || pat3(a,b) || pat4(a,b)) {
		return 1;
	}
	return 0;
}

void
play(int i, int a, int b) {
	int j;

	if (winner) {
		return;
	}
	memcpy(&boards[i], &boards[i-1], sizeof(board));
	boards[i][a-1][b-1] ^= 1;

	for (j=0; j<i; j++) {
		if (pattern(i,j)) {
			winner=1;
			move=i;
		}
	}
}

int
main(void) {
	while (1) {
		int i;

		if (scanf("%d", &n) != 1 || n==0) {
			break;
		}
		memset(boards, 0, sizeof(boards));
		winner=0;
		for (i=0; i<2*n; i++) {
			int a,b;
			char c;
			scanf("%d %d %c", &a, &b, &c);
			play(i+1,a,b);
		}
		if (winner) {
			printf("Player %d wins on move %d\n", move%2+1, move);
		} else {
			printf("Draw\n");
		}
	}
	return 0;
}