From 9fda61430e9236c5fdc15766de13b8647cc3de0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EB=AC=B8=EB=B9=88?= <3412mb@gmail.com> Date: Tue, 5 Mar 2024 12:40:20 +0900 Subject: [PATCH] =?UTF-8?q?2024-03-05=20=EC=84=9D=EC=9C=A0=20=EC=8B=9C?= =?UTF-8?q?=EC=B6=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Munbin-Lee/README.md | 1 + ...\354\234\240 \354\213\234\354\266\224.cpp" | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 "Munbin-Lee/\353\260\261\355\212\270\353\236\230\355\202\271/37-\354\204\235\354\234\240 \354\213\234\354\266\224.cpp" diff --git a/Munbin-Lee/README.md b/Munbin-Lee/README.md index 79bc19ee..13e29ef2 100644 --- a/Munbin-Lee/README.md +++ b/Munbin-Lee/README.md @@ -37,4 +37,5 @@ | 34차시 | 2024.02.06 | 구현 | 피자 굽기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/133 | | 35차시 | 2024.02.18 | 백트래킹 | 단어 마방진 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 | | 36차시 | 2024.02.21 | 문자열 | 회문은 회문아니야!! | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 | +| 37차시 | 2024.03.05 | 백트래킹 | 석유 시추 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 | --- diff --git "a/Munbin-Lee/\353\260\261\355\212\270\353\236\230\355\202\271/37-\354\204\235\354\234\240 \354\213\234\354\266\224.cpp" "b/Munbin-Lee/\353\260\261\355\212\270\353\236\230\355\202\271/37-\354\204\235\354\234\240 \354\213\234\354\266\224.cpp" new file mode 100644 index 00000000..b2acdd8a --- /dev/null +++ "b/Munbin-Lee/\353\260\261\355\212\270\353\236\230\355\202\271/37-\354\204\235\354\234\240 \354\213\234\354\266\224.cpp" @@ -0,0 +1,78 @@ +#include +#include +#include +#include + +using namespace std; + +int solution(vector> 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> chunks(r, vector (c, -1)); + vector oils; + + auto bfs = [&](int y, int x, int chunk) { + queue> 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 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; +}