Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

37-Munbin-Lee #150

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Munbin-Lee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@
| 34μ°¨μ‹œ | 2024.02.06 | κ΅¬ν˜„ | <a href="https://www.acmicpc.net/problem/1756">ν”Όμž κ΅½κΈ°</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/133 |
| 35μ°¨μ‹œ | 2024.02.18 | λ°±νŠΈλž˜ν‚Ή | <a href="https://www.acmicpc.net/problem/24891">단어 λ§ˆλ°©μ§„</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 |
| 36μ°¨μ‹œ | 2024.02.21 | λ¬Έμžμ—΄ | <a href="https://www.acmicpc.net/problem/15927">νšŒλ¬Έμ€ νšŒλ¬Έμ•„λ‹ˆμ•Ό!!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 |
| 37μ°¨μ‹œ | 2024.03.05 | λ°±νŠΈλž˜ν‚Ή | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/250136">μ„μœ  μ‹œμΆ”</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 |
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>

using namespace std;

int solution(vector<vector<int>> land) {
int r = land.size();
int c = land[0].size();

int dy[4] {-1, 0, 1, 0};
int dx[4] {0, -1, 0, 1};

vector<vector<int>> chunks(r, vector<int> (c, -1));
vector<int> oils;

auto bfs = [&](int y, int x, int chunk) {
queue<pair<int, int>> q;
q.emplace(y, x);

int oil = 0;

while (!q.empty()) {
auto [cy, cx] = q.front();
q.pop();

oil++;
chunks[cy][cx] = chunk;

for (int dir = 0; dir < 4; dir++) {
int ny = cy + dy[dir];
int nx = cx + dx[dir];

if (ny == -1 || ny == r || nx == -1 || nx == c) continue;
if (land[ny][nx] == 0) continue;

land[ny][nx] = 0;
q.emplace(ny, nx);
}
}

oils.emplace_back(oil);
};

for (int y = 0; y < r; y++) {
for (int x = 0; x < c; x++) {
if (land[y][x] == 0) continue;

land[y][x] = 0;
bfs(y, x, oils.size());
}
}

int answer = -1;

for (int x = 0; x < c; x++) {
unordered_set<int> set;

for (int y = 0; y < r; y++) {
int chunk = chunks[y][x];

if (chunk == -1) continue;

set.emplace(chunk);
}

int oil = 0;

for (int chunk : set) {
oil += oils[chunk];
}

answer = max(answer, oil);
}

return answer;
}