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
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <vector> #include <queue>
#define DEBUG(x) std::cerr << #x << " = " << x << std::endl; #define forall(G,u) for (int i_ = 0, __for_siz__ = (int) G[u].size(); i_ < __for_siz__; ++i_) #define mp(x,y) std::make_pair((x), (y))
using std::cin; using std::cout; using std::endl;
inline int read() { int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
const int MAXN = 1000 + 10; const int dx[] = { 0, 0, 1, -1 }; const int dy[] = { 1, -1, 0, 0 };
int n, m, t; bool blks[MAXN][MAXN]; int dist[MAXN][MAXN]; bool vis[MAXN][MAXN];
bool noAdjSame(int x, int y) { for (int dr = 0; dr < 4; ++dr) { int nx = x + dx[dr], ny = y + dy[dr]; if (nx < 1 || ny < 1 || nx > n || ny > m) continue; if (blks[nx][ny] == blks[x][y]) return false; } return true; }
void bfs(){ std::queue<std::pair<int, int> > q; memset(dist, 0x3f, sizeof dist); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (!noAdjSame(i, j)) { dist[i][j] = 0; q.push(mp(i, j)); } } } while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); if (vis[x][y]) continue; vis[x][y] = true; for (int dr = 0; dr < 4; ++dr) { int nx = x + dx[dr], ny = y + dy[dr]; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; dist[nx][ny] = std::min(dist[nx][ny], dist[x][y] + 1); q.push(mp(nx, ny)); } } }
int main() { scanf("%d %d %d", &n, &m, &t); for (int i = 1; i <= n; ++i) { static char ss[MAXN]; scanf("%s", ss + 1); for (int j = 1; j <= m; ++j) { blks[i][j] = (ss[j] == '1'); } } bfs(); for (int i = 1; i <= t; ++i) { int x, y; long long int f; scanf("%d %d %lld", &x, &y, &f); if (!vis[x][y]) { printf("%d\n", (int) (blks[x][y] == 1)); } else { printf("%d\n", (int) ((blks[x][y] ^ (std::max(0ll,(f - dist[x][y])) & 1)))); } } return 0; }
|