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

No Rectangles

135.c

/* 135 - No Rectangles */
#include <stdio.h>
#include <string.h>

#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

int k,n;
char mat[1000][1000];

int
test(void) {
	int a,b,c;

	for (a=0; a<n-1; a++) {
		for (b=0; b<n-1; b++) {
			if (!mat[a][b]) continue;
			for (c=1; c<n-MAX(a,b); c++) {
				if (mat[a+c][b] && mat[a][b+c] && mat[a+c][b+c]) {
					return 0;
				}
			}
		}
	}
	return 1;
}

void
calc(void) {
	int i,j;
	memset(mat, 0, sizeof(mat));

	for (i=0; i<k; i++) {
		mat[0][i] = mat[i][0] = 1;
	}
	for (i=1; i<k; i++) {
		for (j=1; j<k; j++) {
			mat[i][(k-1)*i+j] = mat[(k-1)*i+j][i] = 1;
		}
	}
	for (i=0; i<k-1; i++) {
		for (j=0; j<k-1; j++) {
			int a,b;
			int c = i*j % (k-1);
			for (a=0; a<k-1; a++) {
				for (b=0; b<k-1; b++) {
					if ((k-1+a-b)%(k-1) == c) {
						mat[k+(k-1)*i+a][k+(k-1)*j+b] = 1;
					}
				}
			}
		}
	}
}

void
print(void) {
	int i,j;
#if DEBUG
	for (i=0; i<n; i++) {
		for (j=0; j<n; j++) {
			if (mat[i][j]) {
				printf("X");
			} else {
				printf(".");
			}
		}
		printf("\n");
	}
#endif
	for (i=0; i<n; i++) {
		int first=1;
		for (j=0; j<n; j++) {
			if (mat[i][j]) {
				if (first) {
					first=0;
				} else {
					printf(" ");
				}
				printf("%d", j+1);
			}
		}
		printf("\n");
	}
}

int
main(void) {
	int first=1;
	while(scanf("%d", &k)==1) {
		if (first) {
			first=0;
		} else {
			printf("\n");
		}
		n = k*k-k+1;
		calc();
#if DEBUG
		if (test()) {
			printf("Ok\n");
		} else {
			printf("ERROR!\n");
			continue;
		}
#endif
		print();
	}
	return 0;
}