#include /* * Input: * M N * M by N grid in row-major order * (I,J) indicating cell removal selection * (0,0) end of selections * Repeat from M N until either M or N is zero * * Output: * Game Won -- if that's the case, or * Remaining grid -- if the game wasn't won */ #define MAXM 10 #define MAXN 40 #define G(r,c) grid[m-r][c-1] /* map game coords to C subscripts */ char grid[MAXM][MAXN]; char regr[MAXM*MAXN]; char regc[MAXM*MAXN]; int m, n; /* size of grid */ int nreg; /* Read the grid in row-major order */ void read_grid() { int i, j; int k; for (i=1;i<=m;i++) for (j=1;j<=n;j++) { scanf("%d",&k); G(i,j) = k; } } void display() { int i, j; for (i=0;i=0;i--) /* then bottom to top in each column */ if (grid[i][j] != -1) grid[k--][j] = grid[i][j]; while (k >= 0) grid[k--][j] = -1; } /* Now fill in the empty columns */ k = 0; /* k is next column to be filled */ for (j=0;j MAXM || n < 1 || n > MAXN) { printf("Bad parameters.\n"); exit(1); } read_grid(); ncells = m * n; /* The play begins... */ printf("Grid %d.\n",++gridno); for(;;) { scanf("%d%d",&r,&c); if (r == 0 && c == 0) break; /* end of this grid's data */ if (r < 1 || r > m || c < 1 || c > n) continue; nn = find_region(r,c); if (nn < 2) continue; remove_region(); ncells -= nn; } if (ncells == 0) printf(" Game Won\n\n"); else display(); } }