Skip to content

Commit

Permalink
Merge pull request #150 from AlgoLeadMe/37-Munbin-Lee
Browse files Browse the repository at this point in the history
37-Munbin-Lee
  • Loading branch information
MunbinLee authored Mar 7, 2024
2 parents 54c69c7 + 9fda614 commit 7e66693
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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;
}

0 comments on commit 7e66693

Please sign in to comment.