diff --git a/suhyun113/README.md b/suhyun113/README.md index a359954..ffc876a 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -11,4 +11,6 @@ | 7차시 | 2024.05.09 | 트리 | [원숭이 매달기](https://www.acmicpc.net/problem/2716) | [#31](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/31) | | 8차시 | 2024.05.14 | 수학 | [어린 왕자](https://www.acmicpc.net/problem/1004) | [#32](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/32) | | 9차시 | 2024.05.28 | 다익스트라 | [최소비용 구하기](https://www.acmicpc.net/problem/1916) | [#33](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/33) | -| 10차시 | 2024.07.13 | 플로이드-워셜 | [플로이드](https://www.acmicpc.net/problem/11404) | [#36](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/36) | \ No newline at end of file +| 10차시 | 2024.07.13 | 플로이드-워셜 | [플로이드](https://www.acmicpc.net/problem/11404) | [#36](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/36) | +| 11차시 | 2024.07.16 | BFS | [연구소](https://www.acmicpc.net/problem/14502) | [#39](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/39) | +| 12차시 | 2024.07.24 | 이분 탐색 | [나무 자르기](https://www.acmicpc.net/problem/2805) | [#42](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/42) | \ No newline at end of file diff --git "a/suhyun113/\352\265\254\355\230\204/11-suhyun113.cpp" "b/suhyun113/\352\265\254\355\230\204/11-suhyun113.cpp" new file mode 100644 index 0000000..e4c2e43 --- /dev/null +++ "b/suhyun113/\352\265\254\355\230\204/11-suhyun113.cpp" @@ -0,0 +1,106 @@ +//14502 : 연구소 + +#include +#include +#include +#include +using namespace std; + +// 메크로 설정 +#define BLANK 0 +#define WALL 1 +#define VIRUS 2 + +int N, M; // 지도의 세로 크기, 가로 크기 +vector> initial_map; // 초기 지도 +vector> blank_map; // 빈 칸 +int directions[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // 방향 + +// 가능한 모든 벽 세우기 조합 구하기 +vector>> building_walls() { + vector>> combinations; + int size = blank_map.size(); // 빈 칸 크기 + for (int i = 0; i < size; i++) { + for (int j = i + 1; j < size; j++) { + for (int k = j + 1; k < size; k++) { + // 빈 칸에 벽 3개 세우기 + combinations.push_back({blank_map[i], blank_map[j], blank_map[k]}); + } + } + } + return combinations; +} + +// 너비 우선 탐색(바이러스인 칸 큐에 저장 후, +// 바이러스 칸 주변 칸이 바이러스 아닌 빈 칸이라면, +// 주변 칸을 바이러스로 바꾸기) +void bfs(vector>& map_copy) { + queue> q; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map_copy[i][j] == VIRUS) { + q.push({i, j}); + } + } + } + // 바이러스인 칸이 아니고 빈 칸이라면, + while (!q.empty()) { + int x = q.front().first; + int y = q.front().second; + q.pop(); + for (int d = 0; d < 4; d++) { // 방향 탐색 + int nx = x + directions[d][0]; + int ny = y + directions[d][1]; + if (nx >= 0 && nx < N && ny >= 0 && ny < M && map_copy[nx][ny] == 0) { + map_copy[nx][ny] = VIRUS; // 빈 칸을 바이러스로 바꾸기 + q.push({nx, ny}); + } + } + } +} + +// 안전 영역 크기 계산 함수 +int calculate_safe_area(vector>& map_copy) { + int safe_area = 0; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (map_copy[i][j] == BLANK) { // 빈 칸의 개수 + safe_area++; + } + } + } + return safe_area; +} + +int main() { + cin >> N >> M; + initial_map.resize(N, vector(M)); + + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + cin >> initial_map[i][j]; + if (initial_map[i][j] == BLANK) { + blank_map.push_back({ i, j }); // 빈 칸 지도 생성 + } + } + } + + vector>> wall_combinations = building_walls(); // 빈 칸에 벽 세우기 + int max_safe_area = 0; // 안전 영역의 최대 크기 + + for (const auto& walls : wall_combinations) { // 벽 3개를 세운 여러 조합 반복 + // 초기 맵 복사해서 그 맵에 벽 3개 세우는 여러 조합 중 한 경우 적용 + vector> map_copy = initial_map; + for (const auto& wall : walls) { // 한 경우에서 세운 벽 3개의 각각 위치에 벽 세우기 + map_copy[wall.first][wall.second] = WALL; // 벽 세우기 + } + + bfs(map_copy); // 바이러스 주변 칸 빈 칸이라면 바이러스 번짐 + + int safe_area = calculate_safe_area(map_copy); // 안전영역 크기 계산 + max_safe_area = max(max_safe_area, safe_area); + } + + cout << max_safe_area << endl; + return 0; +} \ No newline at end of file diff --git "a/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" new file mode 100644 index 0000000..848d4ec --- /dev/null +++ "b/suhyun113/\354\235\264\353\266\204 \355\203\220\354\203\211/12-suhyun113.cpp" @@ -0,0 +1,51 @@ +// 2805 : 나무 자르기 + +#include +#include +using namespace std; + +// 절단기에 지정한 나무의 높이 H로 나무를 자름 +// H보다 크면 자르기(작으면, 값이 0 또는 음수로 나옴) +bool can_cut(vector& trees, int H, int M) { + long long total = 0; // 자른 나무들의 총 합 + for (int tree : trees) { + if (tree > H) { + total += (tree - H); + } + } + return total >= M; // 총 합이 M 보다는 커야 함 +} + +int binary_search(vector& trees, int M) { + int lo = 0; + int hi = 1000000000; + + while (lo + 1 < hi) { + int mid = (lo + hi) / 2; // 중앙값 저장 + + if (can_cut(trees, mid, M)) { // 절단기 높이 변경 + lo = mid; // 현재의 절단기 높이를 가능한 최소 높이로 저장(중앙값 더 커짐) + } + else { + hi = mid; // 현재 절단기 높이 안되면, 더 낮은 높이로 확인 + } + } + + return lo; // canCut을 만족해야 하므로 +} + +int main() { + int N, M; + cin >> N >> M; + + // N개의 나무들의 길이 목록 만들기 + vector trees(N); + for (int i = 0; i < N; i++) { + cin >> trees[i]; + } + + int result = binary_search(trees, M); + cout << result << endl; + + return 0; +} \ No newline at end of file