Skip to content

Commit

Permalink
2024-03-05 석유 시추
Browse files Browse the repository at this point in the history
  • Loading branch information
MunbinLee committed Mar 5, 2024
1 parent 54c69c7 commit 9fda614
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 |
---
78 changes: 78 additions & 0 deletions Munbin-Lee/백트래킹/37-석유 시추.cpp
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 9fda614

Please sign in to comment.