0% found this document useful (0 votes)
11 views2 pages

Ready Go Part 2 Chibuoyim Ogbonna Madlogic Source Code

Uploaded by

coderade9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Ready Go Part 2 Chibuoyim Ogbonna Madlogic Source Code

Uploaded by

coderade9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <bits/stdc++.

h>
using namespace std;

#define int long long // maybe remove

void solve() {
int n, m;
cin >> n >> m;
vector<string> grid(n);
for (int i = 0; i < n; i++) {
cin >> grid[i];
}
const int dx[] = { -1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
auto in = [&](int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m);
};
vector<vector<bool>> vis(n, vector<bool>(m));
vector<pair<int, int>> cc;
auto dfs = [&](auto && self, int x, int y) -> void {
vis[x][y] = true;
cc.emplace_back(x, y);
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (in(nx, ny) && !vis[nx][ny] && grid[nx][ny] == 'W') {
self(self, nx, ny);
}
}
};
int res = 0;
vector<vector<int>> g(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'W' && !vis[i][j]) {
cc.clear();
dfs(dfs, i, j);
int cnt = 0;
int xx = -1;
int yy = -1;
set<pair<int, int>> st;
for (const auto& [x, y] : cc) {
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (in(nx, ny) && grid[nx][ny] == '.') {
xx = nx;
yy = ny;
st.emplace(nx, ny);
}
}
}
if ((int) st.size() == 0) {
res += (int) cc.size();
} else if ((int) st.size() == 1) {
g[xx][yy] += (int) cc.size();
}
}
}
}
int ans = res;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans = max(ans, res + g[i][j]);
}
}
cout << ans << '\n';
}

int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
freopen("ready_go_part_2_input.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int nT;
cin >> nT;
for (int t = 1; t <= nT; t++) {
cout << "Case #" << t << ": ";
solve();
}
return 0;
}

You might also like