Skip to content

Commit

Permalink
Merge pull request #91 from donghoony/patch-1
Browse files Browse the repository at this point in the history
week12_aru0504
  • Loading branch information
jminkkk authored Jul 22, 2024
2 parents 4d06c28 + 9e41e77 commit 20cb719
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
72 changes: 72 additions & 0 deletions week12/32031/donghoony/32031-dfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int INF = 0x3f3f3f3f;

ll N, M, v[323232], NM4, ans;
char arr[202][202];
vector<int> 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;
}
69 changes: 69 additions & 0 deletions week12/32031/donghoony/32031-uf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <bits/stdc++.h>

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;
}

0 comments on commit 20cb719

Please sign in to comment.