/* Problem 5--Advanced Tic-Tac-Toe This was just a matter of counting the horizontal, vertical, and diagonal solutions. */ #include #include #define MIN(a,b) ((a)<(b)?(a):(b)) FILE *in, *out; int main (int argc, char **argv); int count (int r, int c, int k); int main (int argc, char **argv) { int r, c, k; in = fopen ("prob5.in","r"); out = fopen ("prob5.out","w"); while (fscanf (in,"%d",&r),r>0) { /* Process input. */ fscanf (in,"%d %d",&c,&k); fprintf (out,"%d\n",count (r,c,k)); } fclose (in); fclose (out); return EXIT_SUCCESS; } /* returns total number of tic-tac-toe solutions for the various values of r, c, and k. */ int count (int r, int c, int k) { int total=0; if (k==1) return r*c; /* Special case; each square is a solution. */ if (k <= r) total+= c*(r-k+1); /* Vertical solutions */ if (k <= c) total+= r*(c-k+1); /* Horizontal solutions */ if (k <= r && k <= c) /* Diagonal solutions */ total += 2*(1+abs(r-c)+MIN(r,c)-k)*(MIN(r,c)-k+1); return total; }