Volver a la lista de problemas
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; }