๐ฉ๐ปโ๐ป๋ฌธ์ ๋งํฌ
[๋ฐฑ์ค 16234] ์ธ๊ตฌ ์ด๋ (Python)
โ๏ธIdea Sketch
2021-08-09
1. ์ด๋ฒ ๋ฌธ์ ๋ 80% ์์ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํด ์ ๋ฅผ ๋จน์ ๋ฌธ์ ๋ค.
- Python3์๋ ์ฝ 1000ํ์ ์ฌ๊ท ์ ํ์ด ์๋ค.
- PyPy3๋ ์ฌ๊ท ์ ํ ํ์๊ฐ ๋ ์ปค์ ํต๊ณผํ ์ ์์๋ค.
import sys
sys.setrecursionlimit(10**6)
2. BFS์ DFS
- DFS, BFS ๋ ์ค ์ด๋ ๊ฒ์ ํด๋ ์๊ด์์ง๋ง
- BFS๋ก ๊ตฌํํ ์ฌ๋ก๊ฐ ๋ง๋ค.
- ์ด๋ฒ์ DFS๋ก ๊ตฌํํด๋ดค๋๋ฐ ์ฌ๊ท ์ ํ์ผ๋ก ์ ๋ฅผ ๋ง์ด ๋จน์๋ค.
- ๋ค์ ์ด ๋ฌธ์ ๋ฅผ ๋ณธ๋ค๋ฉด BFS๋ก ๊ตฌํํด๋ณผ ๊ฒ.
3. ๋ก์ง ์ดํดํ๊ธฐ
- ๋ก์ง ์ดํด์ ๋ง์ ๋์์ด ๋ ๋ง์ด๊ตฌ๋ฏธ๋ ๋ธ๋ก๊ทธ
dfs(x, y)
: ์ํ์ข์ฐ ์ธ์ ํ ๋๋ผ์ ์ธ๊ตฌ ์ฐจ์ด๋ฅผ ํ์ธํ๋ค.flag
: ์ธ์ ํ ๋๋ผ๊ฐ ์๋ ๊ฒฝ์ฐTrue
๋ฅผ ๋ฐํํ๋ค.main
: ์ฐํฉํ ๋๋ผ์ ์ขํgroup
๊ณผ ์ด ์ธ๊ตฌ ์sum
์ ์ ์ํ๋ค.exit
1) ์ธ์ ํ ๋๋ผ๊ฐ ์๋ ๊ฒฝ์ฐFalse
, graph์ ์ธ๊ตฌ ์ด๋์ ๋ฐ์ํ๋ค.
2) ์ธ์ ํ ๋๋ผ๊ฐ ์๊ณ graph ํ์์ ๋๋ธ ๊ฒฝ์ฐTrue
, while๋ฌธ์ ์ข ๋ฃํ๋ค.
โ๏ธ์์ค์ฝ๋
2021-08-09
- Python3๋ก ์ ์ถํ ๊ฒฝ์ฐ 80%์์ ์๊ฐ ์ด๊ณผ
- PyPy3๋ก ์ ์ถํ ๊ฒฝ์ฐ 900ms ํต๊ณผ
import sys
def dfs(x, y):
global sum
flag = False
if (0<=x<n) and (0<=y<n):
visit[x][y] = 1
if (0<=x-1<n) and (0<=y<n):
if visit[x-1][y] == 0:
if l <= abs(graph[x][y] - graph[x-1][y]) <= r:
group.append([x-1, y])
sum += graph[x-1][y]
flag = True
dfs(x-1, y)
if (0<=x+1<n) and (0<=y<n):
if visit[x+1][y] == 0:
if l <= abs(graph[x][y] - graph[x+1][y]) <= r:
group.append([x+1, y])
sum += graph[x+1][y]
flag = True
dfs(x+1, y)
if (0<=x<n) and (0<=y-1<n):
if visit[x][y-1] == 0:
if l <= abs(graph[x][y] - graph[x][y-1]) <= r:
group.append([x, y-1])
sum += graph[x][y-1]
flag = True
dfs(x, y-1)
if (0<=x<n) and (0<=y+1<n):
if visit[x][y+1] == 0:
if l <= abs(graph[x][y] - graph[x][y+1]) <= r:
group.append([x, y+1])
sum += graph[x][y+1]
flag = True
dfs(x, y+1)
return (group, sum, flag)
if __name__ == '__main__':
n, l, r = map(int, input().split())
graph = [list(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]
result = 0
while True:
visit = [[0 for _ in range(n)] for _ in range(n)]
result += 1
exit = True
for i in range(n):
for j in range(n):
if visit[i][j] == 0:
group = [[i, j]]
sum = graph[i][j]
group, sum, flag = dfs(i, j)
if flag == True:
exit = False
for x, y in group:
graph[x][y] = int(sum/len(group))
if exit == True:
print(result-1)
break
โ๏ธ๋ช ๋ต
BFS ๋ก์ง
import sys
from collections import deque
input = sys.stdin.readline
dy = [1, -1, 0, 0]
dx = [0, 0, 1, -1]
def bfs(y, x):
global visisted, m
queue = deque()
queue.append((y, x))
sum_val = m[y][x]
visisted[y][x] = 1
pos = [(y, x)]
while queue:
y, x = queue.popleft()
for my, mx in zip(dy, dx):
ny = y + my
nx = x + mx
if 0 <= ny < N and 0 <= nx < N and not visisted[ny][nx]:
if L <= abs(m[ny][nx] - m[y][x]) <= R:
visisted[ny][nx] = 1
sum_val += m[ny][nx]
queue.append((ny, nx))
pos.append((ny, nx))
if len(pos) > 1: # exist group
cnt = len(pos)
mean = sum_val // cnt
for i in range(cnt):
y, x = pos[i]
m[y][x] = mean
q.append((y, x))
return 0
else:
return 1
def check(y, x):
for my, mx in zip(dy, dx):
ny = y + my
nx = x + mx
if 0 <= ny < N and 0 <= nx < N:
if L <= abs(m[ny][nx] - m[y][x]) <= R:
return 0
return 1
N, L, R = map(int, input().split())
m = [[] for _ in range(N)]
q = deque()
for i in range(N):
m[i] = list(map(int, input().split()))
for j in range(N):
q.append((i, j))
cnt = 0
flag = False
while not flag:
visisted = [[0] * N for _ in range(N)]
flag = True
size = len(q)
for _ in range(size):
y, x = q.popleft()
if visisted[y][x] or check(y, x):
continue
if not bfs(y, x):
flag = False
if not flag:
cnt += 1
print(cnt)