ํŒŒ์ด์ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[๋ฐฑ์ค€ 10026] ์ ๋ก์ƒ‰์•ฝ (Python)

์€์ง„ 2021. 8. 4. 12:51

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[๋ฐฑ์ค€ 10026] ์ ๋ก์ƒ‰์•ฝ (Python)
๋ฐฑ์ค€


โœ๏ธIdea Sketch

2021-08-03

1. ๋ฌธ์ œ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ์ถœ๋ ฅ๊ฐ’์€?

  1. ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ์‚ฌ๋žŒ์€ R, G, B ์„ธ ์ข…๋ฅ˜๋กœ ๊ตฌ์—ญ์„ ๋‚˜๋ˆˆ๋‹ค.
  2. ์ ๋ก์ƒ‰์•ฝ์ธ ์‚ฌ๋žŒ์€ R=G, B ๋‘ ์ข…๋ฅ˜๋กœ ๊ตฌ์—ญ์„ ๋‚˜๋ˆˆ๋‹ค.
  3. ๋‘ ์‚ฌ๋žŒ์ด ์ธ์ง€ํ•˜๋Š” ๊ตฌ์—ญ์˜ ์ˆ˜๋ฅผ ๊ฐ๊ฐ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

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=' ')