diff --git a/suhyun113/README.md b/suhyun113/README.md index 163899f..59a2688 100644 --- a/suhyun113/README.md +++ b/suhyun113/README.md @@ -10,4 +10,6 @@ | 6차시 | 2024.04.14 | 스택 | [컨트롤 제트](https://school.programmers.co.kr/learn/courses/30/lessons/120853) | [#21](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/21) | | 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) | \ No newline at end of file +| 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) | +| 11차시 | 2024.07.16 | BFS | [연구소](https://www.acmicpc.net/problem/14502) | [#39](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/39) | \ 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/\355\224\214\353\241\234\354\235\264\353\223\234-\354\233\214\354\205\234/10-suhyun113.py" "b/suhyun113/\355\224\214\353\241\234\354\235\264\353\223\234-\354\233\214\354\205\234/10-suhyun113.py" new file mode 100644 index 0000000..d856392 --- /dev/null +++ "b/suhyun113/\355\224\214\353\241\234\354\235\264\353\223\234-\354\233\214\354\205\234/10-suhyun113.py" @@ -0,0 +1,41 @@ +# 11404 : 플로이드 +import sys + +input = sys.stdin.readline +INF = float('inf') # 최대값 정의 + +# 노드의 개수(n)과 간선의 개수(m) 입력 +n = int(input()) # 도시의 개수 n +m = int(input()) # 버스의 개수 m + +# 2차원 리스트 (그래프 표현) 만들고, 무한대로 초기화(플로이드-워셜 = 이차원 배열) +graph = [[INF] * (n + 1) for _ in range(n + 1)] + +# i에서 j로 갈 수 없는 경우, +# 자기 자신에서 자기 자신으로 가는 비용은 0으로 초기화(대각선에 해당하는 부분)) +for i in range(1, n + 1): + for j in range(1, n + 1): + if i == j: + graph[i][j] = 0 + +# 각 간선에 대한 정보를 입력받아, 그 값으로 초기화 +for _ in range(m): + # A -> B로 가는 비용을 C라고 설정 + i, j, cost = map(int, input().split()) + if graph[i][j] > cost: # 시작 도시와 도착 도시 연결하는 노선 하나가 x + graph[i][j] = cost + +# 점화식에 따라 플로이드 워셜 알고리즘을 수행(3중 for문) +for k in range(1, n + 1): + for i in range(1, n + 1): + for j in range(1, n + 1): + graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]) + +# 수행된 결과를 출력 +for i in range(1, n + 1): + for j in range(1, n + 1): + if graph[i][j] == INF: + print(0, end=' ') + else: + print(graph[i][j], end=' ') + print() \ No newline at end of file