A - Flag of Berland
思路:
Code:
#include <bits/stdc++.h> using namespace std; const int N = 105; int n, m; vector<string> s(N), ss(N); bool check(int n, int m) { if (n % 3) return false; int divide = n / 3; if (s[divide] == s[0] || s[divide * 2] == s[0] || s[divide] == s[divide * 2]) return false; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] != s[i / divide * divide][0]) return false; } } return true; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ss[j].push_back(s[i][j]); } } if (check(n, m)) { cout << "YES\n"; return 0; } swap(n, m); s.clear(); // 清空字符串数组 s.resize(n); // 重新设置大小 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { s[i].push_back(ss[i][j]); // 使用push_back函数向vector中添加元素 } } if (check(n, m)) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; }
B - Darker and Darker
Code:
/* 遍历所有的#统计最大即可简单bfs就行 */ #include <bits/stdc++.h> using namespace std; typedef pair<int, int> PII; const int N = 1e3 + 5; char arr[N][N]; int n, m, ans; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; bool isVaild(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m && arr[x][y] == '.'; } queue<array<int, 3>> q; void bfs() { while (!q.empty()) { auto [x, y, cnt] = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { int dx = x + dir[i][0]; int dy = y + dir[i][1]; if (isVaild(dx, dy)) { arr[dx][dy] = '#'; q.push({dx, dy, cnt + 1}); ans = max(ans, cnt + 1); } } } cout << ans << '\n'; return ; } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cout << fixed << setprecision(12); cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j<= m; j++) { cin >> arr[i][j]; if (arr[i][j] == '#') { q.push({i, j, 0}); } } } bfs(); return 0; }
标签:arr,return,divide,int,Vjudge,小组,++,false,模拟 From: https://www.cnblogs.com/youhualiuh/p/18061075