Skip to content

Commit 74d2ed8

Browse files
authored
Merge pull request #242 from AlgorithmStudy-Allumbus/mj/presentation
7월 5주차 발제/과제 자료 업로드
2 parents 4dbe128 + 2b88606 commit 74d2ed8

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
BOJ #2468. 안전 영역 (실버2)
3+
https://www.acmicpc.net/problem/2468
4+
유형: Graph, DFS, BFS
5+
"""
6+
7+
"""
8+
DFS 풀이
9+
"""
10+
import sys
11+
sys.setrecursionlimit(100000)
12+
13+
input = sys.stdin.readline
14+
15+
16+
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)]
17+
def dfs(x, y):
18+
visited[x][y] = True
19+
for dx, dy in directions:
20+
nx, ny = x + dx, y + dy
21+
if 0 <= nx < N and 0 <= ny < N and \
22+
not visited[nx][ny] and map[nx][ny] > h:
23+
dfs(nx, ny)
24+
25+
26+
N = int(input())
27+
map = [list(map(int, input().split())) for _ in range(N)]
28+
max_height = 0
29+
30+
# 맵 내의 최대값 구하기
31+
for m in map:
32+
max_height = max(max_height, max(m))
33+
34+
answer = 0
35+
36+
for h in range(0, max_height + 1): # 물이 잠기지 않는 상황을 고려하여 0부터 시작한다.
37+
visited = [[False for _ in range(N)] for _ in range(N)]
38+
count = 0
39+
40+
for i in range(N):
41+
for j in range(N):
42+
if not visited[i][j] and map[i][j] > h:
43+
dfs(i, j)
44+
count += 1
45+
46+
answer = max(answer, count)
47+
48+
print(answer)
49+
50+
"""
51+
BFS 풀이
52+
"""
53+
from collections import deque
54+
55+
n = int(input())
56+
graph = []
57+
maxNum = 0
58+
59+
for i in range(n):
60+
graph.append(list(map(int, input().split())))
61+
for j in range(n):
62+
if graph[i][j] > maxNum:
63+
maxNum = graph[i][j]
64+
65+
dx = [0, 0, 1, -1]
66+
dy = [1, -1, 0, 0]
67+
68+
69+
def bfs(a, b, value, visited):
70+
q = deque()
71+
q.append((a, b))
72+
visited[a][b] = 1
73+
74+
while q:
75+
x, y = q.popleft()
76+
77+
for i in range(4):
78+
nx = x + dx[i]
79+
ny = y + dy[i]
80+
if 0 <= nx < n and 0 <= ny < n:
81+
if graph[nx][ny] > value and visited[nx][ny] == 0:
82+
visited[nx][ny] = 1
83+
q.append((nx, ny))
84+
85+
86+
result = 0
87+
for i in range(maxNum):
88+
visited = [[0] * n for i in range(n)]
89+
cnt = 0
90+
91+
for j in range(n):
92+
for k in range(n):
93+
if graph[j][k] > i and visited[j][k] == 0:
94+
bfs(j, k, i, visited)
95+
cnt += 1
96+
97+
if result < cnt:
98+
result = cnt
99+
100+
print(result)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## 🚀7월 5주차 (7/29) 스터디 발제 주제: Graph (DFS/BFS)
2+
> 발제자: 김민정 (@Mingguriguri)
3+
4+
> [!NOTE]
5+
> 주제: Graph (DFS/BFS)
6+
7+
### 🗂️ 스터디 자료
8+
- PDF: [바로가기
9+
](Study_PGS_81302.pdf)
10+
11+
12+
### 📖 문제
13+
- [프로그래머스 #81302. 거리두기 확인하기](https://www.acmicpc.net/problem/1759): Graph (DFS/BFS) / Level 2
14+
- 정답 코드: [Study_PGS_81302_거리두기확인하기.py](Study_PGS_81302_거리두기확인하기.py)
15+
16+
### 💻 과제
17+
- [백준 #2468. 안전구역](https://www.acmicpc.net/problem/2468): Graph (DFS/BFS) / 실버1
18+
- 정답 코드: [Assignment_BOJ_2468_안전구역.py](Assignment_BOJ_2468_안전구역.py)
1000 KB
Binary file not shown.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""
2+
PGS #81032. 거리두기 확인하기 (Level 2)
3+
https://school.programmers.co.kr/learn/courses/30/lessons/81302
4+
유형: Brute Force, Graph, BFS
5+
"""
6+
7+
"""
8+
풀이1: 완전 탐색
9+
"""
10+
11+
12+
def solution(places):
13+
def is_safe(place):
14+
for i in range(5):
15+
for j in range(5):
16+
if place[i][j] != 'P':
17+
continue
18+
19+
for dx, dy in dirs:
20+
ni, nj = i + dx, j + dy
21+
if not (0 <= ni < 5 and 0 <= nj < 5):
22+
continue
23+
if place[ni][nj] != 'P':
24+
continue
25+
26+
dist = abs(dx) + abs(dy)
27+
if dist == 1:
28+
return 0 # 거리 1에서 바로 P면 위반
29+
elif dist == 2:
30+
# 파티션 여부 확인
31+
if dx == 0: # 수평
32+
if place[i][j + dy // 2] != 'X':
33+
return 0
34+
elif dy == 0: # 수직
35+
if place[i + dx // 2][j] != 'X':
36+
return 0
37+
else: # 대각선
38+
if place[i][nj] != 'X' or place[ni][j] != 'X':
39+
return 0
40+
return 1
41+
42+
dirs = [
43+
(-1, 0), (1, 0), (0, -1), (0, 1), # 거리 1
44+
(-2, 0), (2, 0), (0, -2), (0, 2), # 일직선 거리 2
45+
(-1, -1), (-1, 1), (1, -1), (1, 1) # 대각선 거리 2
46+
]
47+
48+
answer = []
49+
for place in places:
50+
answer.append(is_safe(place))
51+
52+
return answer
53+
54+
55+
"""
56+
풀이2: BFS
57+
"""
58+
from collections import deque
59+
60+
61+
def bfs(p):
62+
start = []
63+
64+
for i in range(5): # 시작점이 되는 P 좌표 구하기
65+
for j in range(5):
66+
if p[i][j] == 'P':
67+
start.append([i, j])
68+
69+
for s in start:
70+
queue = deque([s]) # 큐에 초기값
71+
visited = [[0] * 5 for i in range(5)] # 방문 처리 리스트
72+
distance = [[0] * 5 for i in range(5)] # 경로 길이 리스트
73+
visited[s[0]][s[1]] = 1
74+
75+
while queue:
76+
y, x = queue.popleft()
77+
78+
dx = [-1, 1, 0, 0] # 좌우
79+
dy = [0, 0, -1, 1] # 상하
80+
81+
for i in range(4):
82+
nx = x + dx[i]
83+
ny = y + dy[i]
84+
85+
if 0 <= nx < 5 and 0 <= ny < 5 and visited[ny][nx] == 0:
86+
87+
if p[ny][nx] == 'O':
88+
queue.append([ny, nx])
89+
visited[ny][nx] = 1
90+
distance[ny][nx] = distance[y][x] + 1
91+
92+
if p[ny][nx] == 'P' and distance[y][x] <= 1:
93+
return 0
94+
return 1
95+
96+
97+
def solution(places):
98+
answer = []
99+
100+
for i in places:
101+
answer.append(bfs(i))
102+
103+
return answer

0 commit comments

Comments
 (0)