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

Hi-Q

379.c

/* 379 - Hi-Q */
#include <stdio.h>
#include <string.h>

int pegs[50];

int
up(int a) {
	if (a<=3 || a==7 || a==8 || a==12 || a==13) return 0;
	if (a<=6) return a-3;
	if (a<=11) return a-5;
	if (a<=27) return a-7;
	if (a<=30) return a-5;
	return a-3;
}

int
down(int a) {
	if (!a || a>=31 || a==21 || a==22 || a==26 || a==27) return 0;
	if (a>=28) return a+3;
	if (a>=23) return a+5;
	if (a>=7) return a+7;
	if (a>=4) return a+5;
	return a+3;
}

int
left(int a) {
	if (a<=1 || a==4 || a==7 || a==14 || a==21 || a==28 || a==31) return 0;
	return a-1;
}

int
right(int a) {
	if (!a || a==3 || a==6 || a==13 || a==20 || a==27 || a==30 || a==33) return 0;
	return a+1;
}

int
try(void) {
	int i;

	for (i=33; i>=1; i--) {
		if (pegs[i]) continue;
		if (pegs[down(i)] && pegs[down(down(i))]) {
			pegs[down(i)] = pegs[down(down(i))] = 0;
			pegs[i] = 1;
			return 1;
		}
		if (pegs[right(i)] && pegs[right(right(i))]) {
			pegs[right(i)] = pegs[right(right(i))] = 0;
			pegs[i] = 1;
			return 1;
		}
		if (pegs[left(i)] && pegs[left(left(i))]) {
			pegs[left(i)] = pegs[left(left(i))] = 0;
			pegs[i] = 1;
			return 1;
		}
		if (pegs[up(i)] && pegs[up(up(i))]) {
			pegs[up(i)] = pegs[up(up(i))] = 0;
			pegs[i] = 1;
			return 1;
		}
	}
	return 0;
}

void
calc(void) {
	int i;
	int tot=0;

	while(try());
	for (i=1; i<=33; i++) {
		if (pegs[i]) tot+=i;
	}
	printf("%d\n", tot);
}

int
main(void) {
	int i,n;

	printf("HI Q OUTPUT\n");
	scanf("%d", &n);
	for (i=0; i<n; i++) {
		int a;
		memset(pegs, 0, sizeof(pegs));
		while (1) {
			scanf("%d", &a);
			if (a==0) {
				break;
			} else {
				pegs[a] = 1;
			}
		}
		calc();
	}
	printf("END OF OUTPUT\n");
	return 0;
}