From 936628b3ac1bb86836ca5ac75b4a32b01ae1b8ce Mon Sep 17 00:00:00 2001 From: Donghoon Lee Date: Mon, 22 Jul 2024 19:29:39 +0900 Subject: [PATCH 1/2] Create 32031-dfs.cpp --- week12/32031/donghoony/32031-dfs.cpp | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 week12/32031/donghoony/32031-dfs.cpp diff --git a/week12/32031/donghoony/32031-dfs.cpp b/week12/32031/donghoony/32031-dfs.cpp new file mode 100644 index 0000000..aa41960 --- /dev/null +++ b/week12/32031/donghoony/32031-dfs.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +typedef long long ll; +constexpr int INF = 0x3f3f3f3f; + +ll N, M, v[323232], NM4, ans; +char arr[202][202]; +vector g[323232]; + +void dfs(int cur) { + v[cur] = 1; + for (auto nxt: g[cur]) { + if (v[nxt]) continue; + dfs(nxt); + } +} + +void merge(int a, int b) { + g[a].push_back(b); + g[b].push_back(a); +} + +void merge_block(int y, int x, char type) { + int pi = 4 * (y - 1) * M + 4 * (x - 1) + 1; + if (type == 'O') { + for (int i = 0; i < 4; i++) merge(pi + i, pi + i + NM4); + } else if (type == 'I') { + merge(pi, pi + 2), merge(pi + 1, pi + 3); + merge(pi + NM4, pi + 2 + NM4), merge(pi + 1 + NM4, pi + 3 + NM4); + } else { + merge(pi, pi + 1), merge(pi + 2, pi + 3); + merge(pi + NM4, pi + 1 + NM4), merge(pi + 2 + NM4, pi + 3 + NM4); + } +} + +int main() { + cin >> N >> M; + NM4 = 4 * N * M; + for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) cin >> arr[i][j]; + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= M; j++) { + merge_block(i, j, arr[i][j]); + // right + int pi = 4 * (i - 1) * M + 4 * (j - 1) + 1; + if (j != M) { + merge(pi + 1, pi + 4); + merge(pi + 3, pi + 6); + merge(pi + 1 + NM4, pi + 4 + NM4); + merge(pi + 3 + NM4, pi + 6 + NM4); + } + // down + if (i != N) { + merge(pi + 2, pi + 2 + 4 * M - 2); + merge(pi + 3, pi + 3 + 4 * M - 2); + merge(pi + 2 + NM4, pi + 2 + 4 * M - 2 + NM4); + merge(pi + 3 + NM4, pi + 3 + 4 * M - 2 + NM4); + } + } + } + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= M; j++) { + int pi = 4 * (i - 1) * M + 4 * (j - 1) + 1; + for (int k = 0; k < 4; k++) { + if (!v[pi + k]) dfs(pi + k), ans++; + if (!v[pi + k + NM4]) dfs(pi + k + NM4), ans++; + } + } + } + cout << ans << '\n'; + return 0; +} From 9e41e77f6171f0782df013d088b32237728453d6 Mon Sep 17 00:00:00 2001 From: Donghoon Lee Date: Mon, 22 Jul 2024 19:30:23 +0900 Subject: [PATCH 2/2] Create 32031-uf.cpp --- week12/32031/donghoony/32031-uf.cpp | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 week12/32031/donghoony/32031-uf.cpp diff --git a/week12/32031/donghoony/32031-uf.cpp b/week12/32031/donghoony/32031-uf.cpp new file mode 100644 index 0000000..af2efe5 --- /dev/null +++ b/week12/32031/donghoony/32031-uf.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; +typedef long long ll; +constexpr int INF = 0x3f3f3f3f; + +ll N, M, p[323232], NM4, ans; +char g[202][202]; + +int find(int x) { + if (p[x] < 0) return x; + return p[x] = find(p[x]); +} + +void merge(int a, int b) { + a = find(a), b = find(b); + if (a == b) return; + p[b] = a; +} + +void merge_block(int y, int x, char type) { + int pi = 4 * (y - 1) * M + 4 * (x - 1) + 1; + if (type == 'O') { + for (int i = 0; i < 4; i++) merge(pi + i, pi + i + NM4); + } else if (type == 'I') { + merge(pi, pi + 2), merge(pi + 1, pi + 3); + merge(pi + NM4, pi + 2 + NM4), merge(pi + 1 + NM4, pi + 3 + NM4); + } else { + merge(pi, pi + 1), merge(pi + 2, pi + 3); + merge(pi + NM4, pi + 1 + NM4), merge(pi + 2 + NM4, pi + 3 + NM4); + } +} + +int main() { + memset(p, -1, sizeof p); + cin >> N >> M; + NM4 = 4 * N * M; + for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) cin >> g[i][j]; + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= M; j++) { + merge_block(i, j, g[i][j]); + // right + int pi = 4 * (i - 1) * M + 4 * (j - 1) + 1; + if (j != M) { + merge(pi + 1, pi + 4); + merge(pi + 3, pi + 6); + merge(pi + 1 + NM4, pi + 4 + NM4); + merge(pi + 3 + NM4, pi + 6 + NM4); + } + // down + if (i != N) { + merge(pi + 2, pi + 2 + 4 * M - 2); + merge(pi + 3, pi + 3 + 4 * M - 2); + merge(pi + 2 + NM4, pi + 2 + 4 * M - 2 + NM4); + merge(pi + 3 + NM4, pi + 3 + 4 * M - 2 + NM4); + } + } + } + for (int i = 1; i <= N; i++) { + for (int j = 1; j <= M; j++) { + int pi = 4 * (i - 1) * M + 4 * (j - 1) + 1; + for (int k = 0; k < 4; k++) { + ans += (p[pi + k] < 0) + (p[pi + k + NM4] < 0); + } + } + } + cout << ans << '\n'; + return 0; +}