defcheck(x, y, r): if x < 0or x >= M or y < 0or y >= N: returnFalse for i inrange(-r, r + 1): for j inrange(-r, r + 1): if x + i < 0or x + i >= M or y + j < 0or y + j >= N: returnFalse if mp[x + i][y + j] != mp[x][y]: returnFalse returnTrue
for _ inrange(T): M, N, Q = map(int, input().split()) mp = [list(input()) for _ inrange(M)] print(M, N, Q) for _ inrange(Q): qx, qy = map(int, input().split()) left, right = 0, min(M, N) while left <= right: mid = (left + right) // 2 if check(qx, qy, mid): left = mid + 1 else: right = mid - 1 print(2 * right + 1)
boolcheck(vector<string> &mp, int x, int y, int r){ int M = mp.size(), N = mp[0].size(); if (x < 0 || x >= M || y < 0 || y >= N) returnfalse; for (int i = -r; i <= r; i++) { for (int j = -r; j <= r; j++) { if (x + i < 0 || x + i >= M || y + j < 0 || y + j >= N) returnfalse; if (mp[x + i][y + j] != mp[x][y]) returnfalse; } } returntrue; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t, m, n, q, x, y, l, r; cin >> t; while (t--) { cin >> m >> n >> q; vector<string> mp(m); for (int i = 0; i < m; i++) { cin >> mp[i]; } cout << m << " " << n << " " << q << endl; while (q--) { cin >> x >> y; l = 0, r = min(m, n); while (l <= r) { int mid = (l + r) / 2; if (check(mp, x, y, mid)) { l = mid + 1; } else { r = mid - 1; } } cout << 2 * r + 1 << endl; } } return0; }
方法二:只擴散邊緣的格子
可以發現,雖然暴力法的寫法比較直觀,但是其中會有很多重複的檢查。
在檢查時,只需要檢查半徑 恰好 為 r 的邊緣格子是否符合條件即可。因此可以將暴力法中 r2 的兩層迴圈,改成兩個 r×2 的兩層迴圈。
defcheck(x, y, r): if x < 0or x >= M or y < 0or y >= N: returnFalse for i inrange(-r, r + 1): for j in [-r, r]: if x + i < 0or x + i >= M or y + j < 0or y + j >= N: returnFalse if mp[x + i][y + j] != mp[x][y]: returnFalse for j inrange(-r, r + 1): for i in [-r, r]: if x + i < 0or x + i >= M or y + j < 0or y + j >= N: returnFalse if mp[x + i][y + j] != mp[x][y]: returnFalse returnTrue
for _ inrange(T): M, N, Q = map(int, input().split()) mp = [list(input()) for _ inrange(M)] print(M, N, Q) for _ inrange(Q): x, y = map(int, input().split()) r = 1 while check(x, y, r): r += 1 print(2 * r - 1)
boolcheck(int x, int y, int r){ int M = mp.size(), N = mp[0].size(); if (x < 0 || x >= M || y < 0 || y >= N) returnfalse; for (int i = -r; i <= r; i++) { for (int j = -r; j <= r; j += 2 * r) { if (x + i < 0 || x + i >= M || y + j < 0 || y + j >= N) returnfalse; if (mp[x + i][y + j] != mp[x][y]) returnfalse; } } for (int j = -r; j <= r; j++) { for (int i = -r; i <= r; i += 2 * r) { if (x + i < 0 || x + i >= M || y + j < 0 || y + j >= N) returnfalse; if (mp[x + i][y + j] != mp[x][y]) returnfalse; } } returntrue; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t, m, n, q, x, y, r; cin >> t; while (t--) { cin >> m >> n >> q; mp.resize(m); for (int i = 0; i < m; i++) cin >> mp[i]; cout << m << " " << n << " " << q << endl; while (q--) { cin >> x >> y; r = 1; while (check(x, y, r)) r++; cout << 2 * r - 1 << endl; } } return0; }
寫在最後
Cover photo is remixed from @Tiffany, thanks for their work!