#include #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 cr = p[y][x]; int i, j, offy, offx; int cx1, cx2; qstart = 0; qend = 1; q[0][0] = y; q[0][1] = x; p[y][x] = 0; cx1 = x; cx2 = x; while (qend > qstart) { x = q[qstart][1]; y = q[qstart][0]; qstart++; if (cx1 > x) cx1 = x; if (cx2 < x) cx2 = x; if (p[y-1][x] == cr) { p[y-1][x] = 0; q[qend][0] = y-1; q[qend][1] = x; qend++; } if (p[y+1][x] == cr) { p[y+1][x] = 0; q[qend][0] = y+1; q[qend][1] = x; qend++; } if (p[y][x-1] == cr) { p[y][x-1] = 0; q[qend][0] = y; q[qend][1] = x-1; qend++; } if (p[y][x+1] == cr) { p[y][x+1] = 0; q[qend][0] = y; q[qend][1] = x+1; qend++; } } bodu += (qend-2)*(qend-2); offx = 0; for (i = cx1; i <= cx2; 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]) offx++; } for (i = 1; i <= r; i++) { memmove(p[i]+cx2-offx+1, p[i]+cx2+1, c-cx2+1); memset(p[i]+c-offx+1, 0, offx); } } 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); } if (!p[1][1]) bodu += 1000; 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; }