๐ฉ๐ปโ๐ป๋ฌธ์ ๋งํฌ
[๋ฐฑ์ค 10026] ์ ๋ก์์ฝ (Python)
โ๏ธIdea Sketch
2021-08-03
1. ๋ฌธ์ ๊ฐ ์๊ตฌํ๋ ์ถ๋ ฅ๊ฐ์?
- ์ ๋ก์์ฝ์ด ์๋ ์ฌ๋์ R, G, B ์ธ ์ข ๋ฅ๋ก ๊ตฌ์ญ์ ๋๋๋ค.
- ์ ๋ก์์ฝ์ธ ์ฌ๋์ R=G, B ๋ ์ข ๋ฅ๋ก ๊ตฌ์ญ์ ๋๋๋ค.
- ๋ ์ฌ๋์ด ์ธ์งํ๋ ๊ตฌ์ญ์ ์๋ฅผ ๊ฐ๊ฐ ์ถ๋ ฅํด์ผ ํ๋ค.
2. main ๋ก์ง
- ์ ๋ ฅ๊ฐ์ ๋ฐ์ graph๋ฅผ ์์ฑํ๋ค.
- ๋ฐฉ๋ฌธ์ฌ๋ถ๋ฅผ ์ ์ฅํ๋ visited๋ฅผ ์์ฑํ๋ค.
- ๋ชจ๋ color๋ฅผ ๋ฐฉ๋ฌธํ๋ฏ๋ก ์ด์ค ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ๋ค.
for i in range(n):
for j in range(m):
# graph[i][j]
- ์ ๋ก์์ฝ์ด ์๋ ์ฌ๋
1) ์ฒ์ ๋ฐฉ๋ฌธํ ๊ฒฝ์ฐ, ์๋ก์ด ๊ตฌ์ญ์ ์ธ์งํ ๊ฒ์ด๋ค.
2) ๋ฐ๋ผ์ result += 1 ์ ์คํํ๋ค. - ์ ๋ก์์ฝ์ธ ์ฌ๋
0) color๊ฐ 'G'์ธ ๊ฒฝ์ฐ, 'R'๋ก ์ธ์งํ๋ค.
1) ์ฒ์ ๋ฐฉ๋ฌธํ ๊ฒฝ์ฐ, ์๋ก์ด ๊ตฌ์ญ์ ์ธ์งํ ๊ฒ์ด๋ค.
2) ๋ฐ๋ผ์ result += 1 ์ ์คํํ๋ค.
3. DFS ๋ก์ง
- ๊ฒฝ๊ณ๊ฐ์ ๋ถ์ํ๋ค.
- ๋ฐฉ๋ฌธ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
- ๋ฐฉ๋ฌธํ color๊ฐ, main์์ ์๋ก์ด ๊ตฌ์ญ์ ์ธ์งํ์ ๋ color์ ์ผ์นํ๋์ง ํ์ธํ๋ค.
2-(1) ์์ ์๋ก์ด ๊ตฌ์ญ์ ์ธ์งํ์ ๋ - ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ๋ฅผ ํ๋ค.
- ์ํ์ข์ฐ์ ์ธ์ ํ color๋ฅผ ๋ฐฉ๋ฌธํ๋ค.
โ๏ธ์์ค์ฝ๋
2021-08-03
112ms ํต๊ณผ
import sys
sys.setrecursionlimit(10**6)
def dfs(x, y, color):
if (0<=x<n) and (0<=y<n):
if visited[x][y] == 0:
if graph[x][y] == color:
visited[x][y] = 1
dfs(x-1, y, color)
dfs(x+1, y, color)
dfs(x, y-1, color)
dfs(x, y+1, color)
return True
return False
def dfs_color(x, y, color):
if (0<=x<n) and (0<=y<n):
if visited[x][y] == 0:
if (graph[x][y] == color) or (color == 'R' and graph[x][y] == 'G'):
visited[x][y] = 1
dfs_color(x-1, y, color)
dfs_color(x+1, y, color)
dfs_color(x, y-1, color)
dfs_color(x, y+1, color)
return True
return False
if __name__ == '__main__':
n = int(input())
graph = [list(map(str, sys.stdin.readline().rstrip())) for _ in range(n)]
# ์ ๋ก์์ฝ์ด ์๋ ์ฌ๋
visited = [[0 for _ in range(n)] for _ in range(n)]
result = 0
for i in range(n):
for j in range(n):
if dfs(i, j, graph[i][j]) == True:
result += 1
print(result, end=' ')
# ์ ๋ก์์ฝ์ธ ์ฌ๋
visited = [[0 for _ in range(n)] for _ in range(n)]
result = 0
for i in range(n):
for j in range(n):
if graph[i][j] == 'G':
graph[i][j] = 'R'
if dfs_color(i, j, graph[i][j]) == True:
result += 1
print(result)
main ๋ฆฌํฉํ ๋ง ํ 120ms ํต๊ณผ
- for๋ฌธ์ผ๋ก main์ ์ค๋ณต ์ฝ๋ ๋ฆฌํฉํ ๋ง
import sys
sys.setrecursionlimit(10**6)
def dfs(x, y, color):
if (0<=x<n) and (0<=y<n):
if visited[x][y] == 0:
if graph[x][y] == color:
visited[x][y] = 1
dfs(x-1, y, color)
dfs(x+1, y, color)
dfs(x, y-1, color)
dfs(x, y+1, color)
return True
return False
def dfs_color(x, y, color):
if (0<=x<n) and (0<=y<n):
if visited[x][y] == 0:
if (graph[x][y] == color) or (color == 'R' and graph[x][y] == 'G'):
visited[x][y] = 1
dfs_color(x-1, y, color)
dfs_color(x+1, y, color)
dfs_color(x, y-1, color)
dfs_color(x, y+1, color)
return True
return False
if __name__ == '__main__':
n = int(input())
graph = [list(map(str, sys.stdin.readline().rstrip())) for _ in range(n)]
for man in range(2):
visited = [[0 for _ in range(n)] for _ in range(n)]
result = 0
for i in range(n):
for j in range(n):
if man == 0: # ์ ๋ก์์ฝ์ด ์๋ ์ฌ๋
if dfs(i, j, graph[i][j]) == True:
result += 1
else: # ์ ๋ก์์ฝ์ธ ์ฌ๋
if graph[i][j] == 'G':
graph[i][j] = 'R'
if dfs_color(i, j, graph[i][j]) == True:
result += 1
print(result, end=' ')