[ 2023-07-24 ๐Ÿชฝ TIL ]

Burkeyยท2023๋…„ 7์›” 24์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
128/157

๋ฐฑ์ค€ 10026, 4963๋ฒˆ ํŒŒ์ด์ฌ

10026๋ฒˆ

๋ฌธ์ œ


์ฝ”๋“œ

import sys
from collections import deque

input = sys.stdin.readline

n = int(input())
color = []
result_1 = []
for _ in range(n):
  color.append(list(input().strip()))

visted_1 = [[False] * n for _ in range(n)] 
# ์ƒ‰์•ฝ ์—†๋Š” ์‚ฌ๋žŒ์˜ ๋ฐฉ๋ฌธ๊ธฐ๋ก
visted_2 = [[False] * n for _ in range(n)]
# ์ƒ‰์•ฝ ์žˆ๋Š” ์‚ฌ๋žŒ์˜ ๋ฐฉ๋ฌธ๊ธฐ๋ก
move = ((0,1), (0, -1), (1, 0), (-1, 0)) # (์ขŒ, ์šฐ, ํ•˜, ์ƒ)
cnt_1, cnt_2 = 0, 0

for i in range(n):
  for j in range(n):
    if (not visted_1[i][j]):
      cnt_1 += 1
      c = color[i][j]
      q = deque([(i,j)])
      visted_1[i][j] = True
      
      while q: # bfs
        ci, cj = q.popleft()
        
        for mi, mj in move:
          xi,xj = (ci + mi), (cj + mj)
          if (0<=xi<n) and (0<=xj<n) and (not visted_1[xi][xj]) and color[xi][xj] == c: 
          # n x m ๋ฒ”์œ„ ์•ˆ ์ƒํ•˜์ขŒ์šฐ ๋‚ด์— ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ ์ค‘์— 
          # ๊ฐ™์€ ์ƒ‰์„ ๊ฐ€์ง„ ๋ถ€๋ถ„์ด ์žˆ๋Š”์ง€ ์ฒดํฌ
            q.append((xi,xj))
            visted_1[xi][xj] = True
            
umm = ('R', 'G') # ์ƒ‰์•ฝ์ธ์—๊ฒŒ๋Š” R๊ณผ G๊ฐ€ ๋™์ผ ๊ฒฝ๋กœ๋กœ ์ฒดํฌ๋œ๋‹ค. 

for i in range(n):
  for j in range(n):
    if (not visted_2[i][j]):
      cnt_2 += 1
      c = color[i][j]
      q = deque([(i,j)])
      visted_2[i][j] = True
      
      while q: # bfs
        ci, cj = q.popleft()
        
        for mi, mj in move:
          xi,xj = (ci + mi), (cj + mj)
          if (0<=xi<n) and (0<=xj<n) and (not visted_2[xi][xj]) :
            if (c in umm): # R ๋˜๋Š” G์ธ ๊ฒฝ์šฐ
            # ๋‹ค์Œ ๊ฒฝ๋กœ์— R or G๊ฐ€ ์˜ค๋ฉด ์ด๋™ ๊ฐ€๋Šฅํ•˜๋‹ค. 
              if (color[xi][xj] in umm): 
                q.append((xi,xj))
                visted_2[xi][xj] = True
            elif color[xi][xj] == c: # B ์ผ๋•Œ 
              q.append((xi,xj))
              visted_2[xi][xj] = True

print(cnt_1, cnt_2)

4963๋ฒˆ

๋ฌธ์ œ


์ฝ”๋“œ

import sys
from collections import deque

input = sys.stdin.readline

n, m = map(int, input().split())
result = []
move = ((0,1), (1,0), (-1, 0), (0, -1), (1,1), (1, -1), (-1, -1), (-1, 1)) 
# ์ƒํ•˜์ขŒ์šฐ ๋ง๊ณ  ๋Œ€๊ฐ์„ ์œผ๋กœ๋„ ์ด๋™๊ฐ€๋Šฅ

while n > 0 and m > 0: # n, m์ด ๋‘˜ ๋‹ค 0์ด ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋ฉด ๋๋‚œ๋‹ค.
  feild = []
  visited = [[0] * n for _ in range(m)]
  count = 0
  
  for _ in range(m):
    feild.append(list(map(int, input().strip().split())))

  for i in range(m):
    for j in range(n):
      if visited[i][j] == 0 and feild[i][j] == 1:
        count += 1
        
        q = deque([(i, j)])
        visited[i][j] = 1
        
        while q : # bfs
          ci, cj = q.popleft()
          
          for mi, mj in move :
            xi, xj = (mi + ci), (mj + cj)
            if 0<=xi<m and 0<=xj<n and visited[xi][xj] == 0 and feild[xi][xj] == 1:
            # ์ด๋™๊ฐ€๋Šฅ ๋ฒ”์œ„ ๋‚ด์— ๋ฐฉ๋ฌธํ•œ์  ์—†์œผ๋ฉด์„œ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ
            # ์ด๋™๊ฐ€๋Šฅ ์„ฌ์œผ๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.
              q.append((xi, xj))
              visited[xi][xj] = 1
              
  result.append(count)
  n, m = map(int, input().split())
  
for val in result:
  print(val)

๋‘ ๋ฌธ์ œ ๋ชจ๋‘ bfs๋กœ ํ•ด๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.
bfs๊ฐ€ ์•„๋ฌด๋ž˜๋„ ๊ตฌํ˜„ํ•˜๊ธฐ ๋” ์‰ฌ์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€