#include #define MAXR 200 #define MAXC 400 char p[MAXR][MAXC]; int q[MAXR*MAXC][2]; int qstart, qend; int r, c; int bodu; void search_shift(int y, int x) { char c = p[y][x]; int i, j, offy, offx; qstart = 0; qend = 1; q[0][0] = y; q[0][1] = x; p[y][x] = 0; while (qend > qstart) { x = q[qstart][1]; y = q[qstart][0]; qstart++; if (p[y-1][x] == c) { p[y-1][x] = 0; q[qend][0] = y-1; q[qend][1] = x; qend++; } if (p[y+1][x] == c) { p[y+1][x] = 0; q[qend][0] = y+1; q[qend][1] = x; qend++; } if (p[y][x-1] == c) { p[y][x-1] = 0; q[qend][0] = y; q[qend][1] = x-1; qend++; } if (p[y][x+1] == c) { p[y][x+1] = 0; q[qend][0] = y; q[qend][1] = x+1; qend++; } } bodu += (qend-2)*(qend-2); offx = 0; for (i = 1; i <= c; i++) { offy = 0; for (j = 1; j <= r; j++) { if (p[j][i] == 0) { offy++; continue; } p[j-offy][i-offx] = p[j][i]; if (offx || offy) p[j][i] = 0; } if (!p[1][i]) offx++; } if (!p[1][1]) bodu += 1000; } int main(void) { int zad; int i, tahu, x, y, j; char buf[100]; gets(buf); sscanf(buf, "%d", &zad); while (zad--) { gets(buf); sscanf(buf, "%d %d", &c, &r); for (i = 0; i <= r+1; i++) for (j = 0; j <= c+1; j++) p[i][j] = 0; for (i = 0; i < r; i++) gets(p[r-i]+1); bodu = 0; gets(buf); sscanf(buf, "%d", &tahu); for (i = 0; i < tahu; i++) { gets(buf); sscanf(buf, "%d %d", &x, &y); if (x < 1 || y < 1 || x > c || y > r || !p[y][x] || (p[y-1][x] != p[y][x] && p[y+1][x] != p[y][x] && p[y][x-1] != p[y][x] && p[y][x+1] != p[y][x])) continue; search_shift(y, x); } printf("Game over!\nScore dosazene v teto hre je %d bodu.\nPrejete si hrat znovu?\nPrijemnou zabavu Vam preje firma ACMTENDO.\n", bodu); } return 0; }