Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11-suhyun113 #39

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion suhyun113/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
| 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) |
106 changes: 106 additions & 0 deletions suhyun113/๊ตฌํ˜„/11-suhyun113.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//14502 : ์—ฐ๊ตฌ์†Œ

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

// ๋ฉ”ํฌ๋กœ ์„ค์ •
#define BLANK 0
#define WALL 1
#define VIRUS 2

int N, M; // ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ, ๊ฐ€๋กœ ํฌ๊ธฐ
vector<vector<int>> initial_map; // ์ดˆ๊ธฐ ์ง€๋„
vector<pair<int, int>> blank_map; // ๋นˆ ์นธ
int directions[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; // ๋ฐฉํ–ฅ

// ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ฒฝ ์„ธ์šฐ๊ธฐ ์กฐํ•ฉ ๊ตฌํ•˜๊ธฐ
vector<vector<pair<int, int>>> building_walls() {
vector<vector<pair<int, int>>> 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<vector<int>>& map_copy) {
queue<pair<int, int>> 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<vector<int>>& 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<int>(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<vector<pair<int, int>>> wall_combinations = building_walls(); // ๋นˆ ์นธ์— ๋ฒฝ ์„ธ์šฐ๊ธฐ
int max_safe_area = 0; // ์•ˆ์ „ ์˜์—ญ์˜ ์ตœ๋Œ€ ํฌ๊ธฐ

for (const auto& walls : wall_combinations) { // ๋ฒฝ 3๊ฐœ๋ฅผ ์„ธ์šด ์—ฌ๋Ÿฌ ์กฐํ•ฉ ๋ฐ˜๋ณต
// ์ดˆ๊ธฐ ๋งต ๋ณต์‚ฌํ•ด์„œ ๊ทธ ๋งต์— ๋ฒฝ 3๊ฐœ ์„ธ์šฐ๋Š” ์—ฌ๋Ÿฌ ์กฐํ•ฉ ์ค‘ ํ•œ ๊ฒฝ์šฐ ์ ์šฉ
vector<vector<int>> 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;
}
Original file line number Diff line number Diff line change
@@ -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()