|
| 1 | +""" |
| 2 | +https://www.acmicpc.net/problem/10026 |
| 3 | +
|
| 4 | +
|
| 5 | +- NXN (<= 1000) , R,G,B |
| 6 | +- 상하좌우로 겹쳐진 "구역" = 1개 |
| 7 | +- 적록 색약 R = G , B |
| 8 | +
|
| 9 | +
|
| 10 | +=(1) 정상 |
| 11 | +R + G + B |
| 12 | +(2) 색약 |
| 13 | +max(R,G) + B |
| 14 | +# 유형 : DFS |
| 15 | +
|
| 16 | +# flow |
| 17 | +
|
| 18 | +
|
| 19 | +1.(0,0) 부터 DFS 수행 + 방문(visited) 등록 : 1 X N*N |
| 20 | +2. 방문 리스트 : iT False not in visited , 까지 반복 |
| 21 | +""" |
| 22 | + |
| 23 | + |
| 24 | +import sys |
| 25 | +# 1. 입력 받기 |
| 26 | +n = int(input()) |
| 27 | +field = [ [] for _ in range(n) ] |
| 28 | +colors_g = [[] , [] , []] |
| 29 | +for i in range(n): |
| 30 | + tmp = sys.stdin.readline().split() |
| 31 | + # print(f"tmp {tmp}") |
| 32 | + for t in range(len(tmp[0])) : |
| 33 | + field[i].append(tmp[0][t]) |
| 34 | + if tmp[0][t] == "R" : |
| 35 | + colors_g[0].append([i,t]) |
| 36 | + elif tmp[0][t] == "G": |
| 37 | + colors_g[1].append([i,t]) |
| 38 | + else : |
| 39 | + colors_g[2].append([i,t]) |
| 40 | +# print(f"colors-G {colors_g}") |
| 41 | + |
| 42 | +# 2. DFS |
| 43 | +dx = [0,0,-1,1] |
| 44 | +dy = [-1,1,0,0,] |
| 45 | +def dfs(start_y,start_x, visited, flag , colors ): |
| 46 | + r , g, b= colors |
| 47 | + if field[start_y][start_x] == "R": |
| 48 | + r+=1 |
| 49 | + elif field[start_y][start_x] == "G": |
| 50 | + g+=1 |
| 51 | + else : |
| 52 | + b+=1 |
| 53 | + queue = list() |
| 54 | + queue.append([start_y,start_x]) |
| 55 | + visited.append([start_y,start_x]) |
| 56 | + while queue : |
| 57 | + cur_y , cur_x= queue.pop() |
| 58 | + for i in range(4): |
| 59 | + next_y ,next_x = cur_y + dy[i] , cur_x + dx[i] |
| 60 | + if next_y>= n or next_y < 0 or next_x>= n or next_x < 0: |
| 61 | + print("####") |
| 62 | + continue |
| 63 | + |
| 64 | + if flag : #적록색약 경우 |
| 65 | + if field[cur_y][cur_x] in ["R","G"] and field[next_y][next_x] in ["R","G"] and [next_y,next_x] not in visited : |
| 66 | + queue.append([next_y,next_x]) |
| 67 | + visited.append([next_y,next_x]) |
| 68 | + continue |
| 69 | + if field[cur_y][cur_x] == field[next_y][next_x] and [next_y,next_x] not in visited : #색상이 같은 경우만 진행 |
| 70 | + queue.append([next_y, next_x]) |
| 71 | + visited.append([next_y, next_x]) |
| 72 | + print(f"visited : {visited}") |
| 73 | + return [r,g,b] ,visited |
| 74 | + |
| 75 | + |
| 76 | +# 1. 정상 |
| 77 | +# visited= [ [False for _ in range(n)] for k in range(n)] |
| 78 | + |
| 79 | +colors = [ 0, 0, 0] |
| 80 | +for c in range(3) : |
| 81 | + category_colors = colors_g[c] |
| 82 | + visited = [] |
| 83 | + while len(visited) <= len(category_colors): # R |
| 84 | + for y,x in category_colors : |
| 85 | + if [y,x] not in visited : |
| 86 | + colors , visited = dfs(y, x, visited , False , colors) |
| 87 | + print(f"i {colors}") |
| 88 | + |
| 89 | +print(sum(colors)) |
| 90 | +# visited = [] |
| 91 | +# colors = [ 0, 0, 0] |
| 92 | +# for c in range(3) : |
| 93 | +# category_colors = colors_g[c] |
| 94 | +# while len(visited) <= len(category_colors ): # R |
| 95 | +# for y,x in category_colors : |
| 96 | +# if [y,x] not in visited : |
| 97 | +# colors , visited = dfs(y, x, visited , True,colors) |
| 98 | +# print(sum(colors)) |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
0 commit comments