1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| #include <iostream> #include <cstdio> #include <cstring> #include <cctype> #include <algorithm>
#include <vector> #include <string> #include <stack> #include <queue>
#include <map> #include <cmath>
#define For(a,x,y) for (int a = x; a <= y; ++a) #define Forw(a,x,y) for (int a = x; a < y; ++a) #define Bak(a,y,x) for (int a = y; a >= x; --a)
namespace FastIO { inline int getint() { int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -1; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; } inline void __basic_putint(int x) { if (x < 0) { x = -x; putchar('-'); } if (x >= 10) __basic_putint(x / 10); putchar(x % 10 + '0'); } inline void putint(int x, char external) { __basic_putint(x); putchar(external); } }
namespace Solution { const int dx[] = {0, 1, -1, 0, 0}; const int dy[] = {0, 0, 0, -1, 1}; const int MAXN = 1000 + 10; char mp[MAXN][MAXN]; bool vis[MAXN][MAXN]; int n, m, nowans; int xans[MAXN * MAXN], yans[MAXN * MAXN]; int ans[MAXN][MAXN]; void Search(int x, int y) { ++nowans; xans[nowans] = x; yans[nowans] = y; for (int i = 1; i <= 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (!(nx <= 0 || nx > n || ny <= 0 || ny > n) && !vis[nx][ny] && mp[nx][ny] != mp[x][y]) { vis[nx][ny] = true; Search(nx, ny); } } } }
signed main() { #define HANDWER_FILE #ifndef HANDWER_FILE freopen("testdata.in", "r", stdin); freopen("testdata.out", "w", stdout); #endif using namespace Solution; using namespace FastIO; scanf("%d %d", &n, &m); For (i, 1, n) { scanf("%s", mp[i] + 1); } For (i, 1, n) { For (j, 1, n) { if (!vis[i][j]) { vis[i][j] = true; nowans = 0; Search(i, j); for (int no = 1; no <= nowans; ++no) { ans[xans[no]][yans[no]] = nowans; } } } } For (i, 1, m) { int x, y; scanf("%d %d", &x, &y); printf("%d\n", ans[x][y]); } return 0; }
|