intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n, m, r, c; cin >> n >> m >> r >> c; vector<string> grid(n); for (auto& v : grid) { cin >> v; } int B = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { B += (grid[i][j] == 'B'); } } if (!B) { cout << "-1\n"; continue; } if (grid[r - 1][c - 1] == 'B') { cout << "0\n"; continue; } B = 0; for (int i = 0; i < m; i++) { B += (grid[r - 1][i] == 'B'); } for (int i = 0; i < n; i++) { B += (grid[i][c - 1] == 'B'); } if (B != 0) { cout << "1\n"; continue; } cout << "2\n"; } return0; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n, m; cin >> n >> m; vector<vector<int>> dp(n, vector<int>(m, INT_MAX)); queue<pair<int, int>> q; dp[0][0] = dp[0][m - 1] = dp[n - 1][0] = dp[n - 1][m - 1] = 0; q.emplace(0, 0); q.emplace(0, m - 1); q.emplace(n - 1, 0); q.emplace(n - 1, m - 1); while (!q.empty()) { auto& [x, y] = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int nx = x + fx[i][0]; int ny = y + fx[i][1]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && dp[nx][ny] > dp[x][y] + 1) { dp[nx][ny] = dp[x][y] + 1; q.emplace(nx, ny); } } } vector<int> ans; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans.push_back(dp[i][j]); } } sort(ans.begin(), ans.end(), greater<>()); for (int i = 0; i < n * m; i++) { cout << n + m - ans[i] - 2 << " \n"[i == n * m - 1]; } } return0; }
voiddfs(int u, int fa, int x){ for (int i = head[u]; i; i = edges[i].next) { int v = edges[i].v; if (v == fa) continue; ans[{u, v}] = ans[{v, u}] = x; dfs(v, u, x ^ 1); } }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { ans.clear(); int n; cin >> n; for (int i = 1; i <= n; i++) { head[i] = d[i] = 0; } k = 0; vector<pair<int, int>> vec(n - 1); for (auto& [u, v] : vec) { cin >> u >> v; d[u]++; d[v]++; add(u, v); } bool flag = true; for (int i = 1; i <= n; i++) { if (d[i] >= 3) { flag = false; break; } } if (!flag) { cout << "-1\n"; continue; } int root = 0; for (int i = 1; i <= n; i++) { if (d[i] == 1) { root = i; break; } } dfs(root, 0, 0); for (auto& [u, v] : vec) { cout << (ans[{u, v}] ? 2 : 3) << " "; } cout << "\n"; } return0; }