int white = 0; auto dfs = [&] (auto self, int x, int y) { if (x < 0 || x > n + 1 || y < 0 || y > m + 1 || vis[x][y]) { return; } if (mp[x][y] == 1) { // 黑格 vis[x][y] = 1; return; } // 白 if (x >= 1 && x <= n && y >= 1 && y <= m) { white += 1; } vis[x][y] = 1; for (int i = 0; i < 4; ++i) { self(self, x + dx[i], y + dy[i]); } }; dfs(dfs, 0, 0); if (black + white == m * n) returnfalse; elsereturntrue; } voideachT(){ n = read(), m = read(), k = read(); for (int i = 0; i < k; ++i) { a[i].first = read(), a[i].second = read(); } int l = 1, r = k; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) r = mid - 1; else l = mid + 1; } string res(r, '1'); string res2(k - r, '0'); cout << res << res2 << '\n'; }
voiddfsh(int x, int y, int d, int step){ if (vis[x][y][d] >= 2) return; ans[x][y][d] = step; int nd; if (mp[x][y] == '/') { nd = d ^ 3; } elseif (mp[x][y] == '\\') { nd = d ^ 2; } elseif (mp[x][y] == '-') { if (d <= 1) nd = d ^ 1; else nd = d; } elseif (mp[x][y] == '|') { if (d > 1) nd = d ^ 1; else nd = d; } vis[x][y][d]++; dfsh(x + dx[nd], y + dy[nd], nd, step); }
intdfs(int stx, int sty, int std, int x, int y, int d, int step = 0){ if (mp[x][y] == '*') { return step; } if (ans[x][y][d] && step == 0) { return step + ans[x][y][d]; } if (x == stx && y == sty && std == d && step != 0) { // 进环 return step; }
int nd; if (mp[x][y] == '/') { nd = d ^ 3; if (visnow[x][y] != q) visnow[x][y] = q, step++; } elseif (mp[x][y] == '\\') { nd = d ^ 2; if (visnow[x][y] != q) visnow[x][y] = q, step++; } elseif (mp[x][y] == '-') { if (d <= 1) { nd = d ^ 1; if (visnow[x][y] != q) visnow[x][y] = q, step++; } else { nd = d; } } elseif (mp[x][y] == '|') { if (d > 1) { nd = d ^ 1; if (visnow[x][y] != q) visnow[x][y] = q, step++; } else { nd = d; } } returndfs(stx, sty, std, x + dx[nd], y + dy[nd], nd, step); }
intmain(){ int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> mp[i][j]; } } for (int i = 0; i <= m + 1; i++) mp[0][i] = mp[n + 1][i] = '*'; for (int i = 0; i <= n + 1; i++) mp[i][0] = mp[i][m + 1] = '*'; cin >> q; while (q) { int x, y; string d; cin >> x >> y >> d; if (d == "above") { x--; ans[x][y][0] = dfs(x, y, 0, x, y, 0); cout << ans[x][y][0] << endl; } elseif (d == "below") { x++; ans[x][y][1] = dfs(x, y, 1, x, y, 1); cout << ans[x][y][1] << endl; } elseif (d == "left") { y--; ans[x][y][2] = dfs(x, y, 2, x, y, 2); cout << ans[x][y][2] << endl; } else { y++; ans[x][y][3] = dfs(x, y, 3, x, y, 3); cout << ans[x][y][3] << endl; } q--; } return0; }
#include<bits/stdc++.h> usingnamespace std; using ll = longlong; #define int ll const ll INF = 0x3f3f3f3f;
inline ll read(){ ll x = 0, f = 0; char c = getchar(); while (c > 57 || c < 48) f |= c == 45, c = getchar(); while (c <= 57 && c >= 48) x = (x << 3) + (x << 1) + c - 48, c = getchar(); return f ? -x : x; }
char a[1010][1010]; int vis[1010][1010]; int n, m; int xx[10] = { 1, -1, 0, 0, 1, -1, 1, -1 }; int yy[10] = { 0, 0, 1, -1, 1, -1, -1, 1 }; vector<pair<int, int>>v;
voiddfs0(int x, int y){ for (int i = 0; i < 8; i++) { int newx = x + xx[i]; int newy = y + yy[i]; if (0 <= newx && newx <= n + 1 && 0 <= newy && newy <= m + 1 && vis[newx][newy] == 0) { if (a[newx][newy] == '.') { vis[newx][newy] = -1; dfs0(newx, newy); } } } }
voiddfs(int cnt, int x, int y){ for (int i = 0; i < 4; i++) { int newx = x + xx[i]; int newy = y + yy[i]; if (0 <= newx && newx <= n + 1 && 0 <= newy && newy <= m + 1 && vis[newx][newy] != cnt && vis[newx][newy] != -1) { vis[newx][newy] = cnt; v.push_back({ newx, newy }); if (a[newx][newy] == '#') { dfs(cnt, newx, newy); } } } }
voiddfs2(int cnt, int x, int y){ for (int i = 0; i < 4; i++) { int newx = x + xx[i]; int newy = y + yy[i]; if (0 <= newx && newx <= n + 1 && 0 <= newy && newy <= m + 1 && vis[newx][newy] == cnt) { vis[newx][newy] = INF; if (a[newx][newy] == '.') { dfs2(cnt, newx, newy); } } } }
voideachT(){ n = read(), m = read(); int ans1 = 0, ans8 = 0, ans0 = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; } } for (int i = 0; i <= n + 1; i++) { a[i][0] = a[i][m + 1] = '.'; } for (int j = 0; j <= m + 1; j++) { a[0][j] = a[n + 1][j] = '.'; } dfs0(0, 0);
for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (!vis[i][j] && a[i][j] == '#') { v.clear(); dfs(i * n + j, i, j); int num = 0; for (auto [x, y] : v) { if (a[x][y] == '.' && vis[x][y] != INF) { // cerr << x <<" "<< y << "\n"; dfs2(vis[x][y], x, y); num++; } } // cerr << num << '\n'; if (num == 0) ans1++; elseif (num == 1) ans0++; elseif (num == 2) ans8++; } } } // for (int i = 0; i <= n + 1; i++) // { // for (int j = 0; j <= m + 1; j++) // { // cerr << vis[i][j] << " "; // } // cerr << "\n"; // }
int ans = inf; auto dfs = [&](auto&& self, int id, int ab, int cd) { // ab表示AB的异或和 即A的异或和^B的异或和 if (id == n) { // 枚举完毕 int maxb = -inf, maxd = -inf; // 记录最大值 for (int i = 0; i < 1 << m; i++) { int a = rk[i], b = ab ^ a; // 枚举得到A的异或和a B的异或和b if (AB[n][a] && w[a] >= w[b]) { // a这个异或和是能表示的 且不妨设w[a]>=w[b] maxb = max(maxb, w[b]); ans = min(ans, w[a] - min(w[b], maxd)); }
int c = rk[i], d = cd ^ c; if (CD[n][c] && w[c] >= w[d]) { maxd = max(maxd, w[d]); ans = min(ans, w[c] - min(w[d], maxb)); } } return; }
// 枚举将a[id]放在AB中 for (int i = 0; i < 1 << m; i++) { AB[id + 1][i] = AB[id][i] | AB[id][i ^ a[id]]; // 可选arr[id] 能表示出的异或和有i^a[id] CD[id + 1][i] = CD[id][i]; } self(self, id + 1, ab ^ a[id], cd);
// 枚举将a[id]放在CD中 for (int i = 0; i < 1 << m; i++) { AB[id + 1][i] = AB[id][i]; CD[id + 1][i] = CD[id][i] | CD[id][i ^ a[id]]; } self(self, id + 1, ab, cd ^ a[id]); }; dfs(dfs, 1, a[0], 0);