[TIL #24] 24์ผ์ฐจ ์˜ค๋Š˜์˜ ๊ณต๋ถ€ (2023.03.26 ์ผ)

2023. 3. 26. 23:01ใ†Today I Learned

TIL 2023.03.26 ๐Ÿฃ

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ๋ฐฑ์ค€ 6064 ์นด์ž‰ ๋‹ฌ๋ ฅ : ๊ฒจ์šฐ ์ดํ•ดํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ธ”๋กœ๊ทธ๋กœ ์ •๋ฆฌํ–ˆ๋‹ค. ๊ธ€์ด ์ž˜ ์ฝํžˆ๋„๋ก ์“ฐ๋Š” ๊ฑด ์ •๋ง ์–ด๋ ต๋‹ค๋Š” ๊ฒƒ์„ ์ƒˆ์‚ผ ๋˜ ๋Š๊ผˆ๋‹ค. ํœด ๐Ÿ˜ฎ‍๐Ÿ’จ
  • ๋ฐฑ์ค€ 1780 ์ข…์ด์˜ ๊ฐฏ์ˆ˜
    • ์‹คํŒจ ์†Œ์Šค
      # ์ข…์ด์˜ ๊ฐฏ์ˆ˜
      import sys
      n = int(sys.stdin.readline().rstrip())
      paper = []
      for _ in range(n):
          paper.append(list(sys.stdin.readline().rstrip().split()))
      cnt = {'-1':0, '0':0, '1':0}
      
      def cutPaper(x, y, n):
          if n == 1:
              cnt[paper[x][y]] += 1
              return
          
          # ๋ชจ๋‘ ๊ฐ™์€ ์ˆ˜์ธ์ง€ ํ™•์ธ
          paperNum = paper[x][y]
          for i in range(x, x + n):
              for j in range(y, y + n):
                  if paper[i][j] != paperNum:
                      paperNum = paper[i][j]
                      break
          # 9๊ฐœ๋กœ ์ž๋ฅผ์ง€ ์—ฌ๋ถ€ ํ™•์ธ
          if paperNum == paper[x][y]: 
              cnt[paperNum] += 1
              return
          
          # 9๊ฐœ๋กœ ์ž๋ฅด๊ธฐ : paperNum ๊ฐ’์ด ๋ฐ”๊ผˆ๋‹ค๋ฉด ์ข…์ด๊ฐ€ ๊ฐ™์€ ์ˆ˜๊ฐ€ ์•„๋‹˜
          paperSize = n // 3
          for i in range(0, 3):
              for j in range(0, 3):
                  cutPaper(x + (i * paperSize), y + (j * paperSize), paperSize)
      
      cutPaper(0, 0, n)
      # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
      print(cnt['-1'])
      print(cnt['0'])
      print(cnt['1'])
    • ๋‹ค๋ฅธ ์ฝ”๋“œ ์ฐธ๊ณ ํ•˜์ง€ ์•Š๊ณ  ํ›„๋ฃจ๋ฃฉ ์ง  ์ฝ”๋“œ๋ผ ๋˜๊ฒŒ ๋ฟŒ๋“ฏํ•ดํ•˜๋ฉด์„œ ์ œ์ถœํ–ˆ๋Š”๋ฐ ๋ฐ”๋กœ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค! ๐Ÿ˜…
    • ์ด์ค‘ for๋ฌธ์œผ๋กœ paper ๋ฒ”์œ„ ๋‚ด์— ๋‹ค๋ฅธ ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ paperNum์„ paper[i][j] ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค. ์ด์ค‘ for๋ฌธ์ด ๋๋‚œ ๋’ค paperNum ๊ฐ’์ด ์ดˆ๊ธฐ๊ฐ’์ด์˜€๋˜ paper[x][y]์™€ ๋‹ค๋ฅด๋‹ค๋ฉด ๋ฒ”์œ„ ๋‚ด์— ๋‹ค๋ฅธ ์ˆซ์ž๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ 9๊ฐœ๋กœ ์ข…์ด๋ฅผ ์ž๋ฅด๋„๋ก ํ•˜์˜€๋‹ค. ๐Ÿ‘‰ ์ด๊ฒŒ ๋ฐ”๋กœ ์‹คํŒจ์š”์ธ!
    • ์ด์ค‘ for๋ฌธ์„ break ํ•  ๋•Œ, ๋‚ด๋ถ€์— ์žˆ๋Š” j๊ฐ’ for๋ฌธ๋งŒ break ๋˜๊ณ  ์™ธ๋ถ€์˜ for๋ฌธ์€ break ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋”ฐ๋ผ์„œ paperNum์˜ ๊ฐ’์„ paper[x][y]์™€ ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ๊ฒŒ ๋œ๋‹ค๋ฉด, ๋ฒ”์œ„๋‚ด์˜ ๋‹ค๋ฅธ ๊ฐ’์„ ๊ฐ์ง€ํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค.
    • ๋”ฐ๋ผ์„œ cutFlag๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ด์ค‘ for๋ฌธ ์ „์ฒด๊ฐ€ break๋˜๋„๋ก ์ˆ˜์ •ํ–ˆ๋‹ค.
    • ์ตœ์ข… ์†Œ์Šค
      # ์ข…์ด์˜ ๊ฐฏ์ˆ˜
      import sys
      n = int(sys.stdin.readline().rstrip())
      paper = []
      for _ in range(n):
          paper.append(list(sys.stdin.readline().rstrip().split()))
      cnt = {'-1':0, '0':0, '1':0}
      
      def cutPaper(x, y, n):
          if n == 1:
              cnt[paper[x][y]] += 1
              return
          
          # ๋ชจ๋‘ ๊ฐ™์€ ์ˆ˜์ธ์ง€ ํ™•์ธ
          paperNum = paper[x][y]
          cutFlag = False
          for i in range(x, x + n):
              for j in range(y, y + n):
                  if paper[i][j] != paperNum:
                      cutFlag = True  # ๋‹ค๋ฅธ ์ˆ˜๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ข…์ด ์ž๋ฅด๊ธฐ
                      break
              if cutFlag:
                  break
      
          if not cutFlag: 
              cnt[paperNum] += 1  # ํ•ด๋‹น ์ˆซ์ž์˜ ์ข…์ด ์ˆ˜ ์ฆ๊ฐ€
              return
          
          # 9๊ฐœ๋กœ ์ž๋ฅด๊ธฐ : cutFlag๊ฐ€ ๋ฐ”๊ผˆ๋‹ค๋ฉด ์ข…์ด๊ฐ€ ๊ฐ™์€ ์ˆ˜๊ฐ€ ์•„๋‹˜
          paperSize = n // 3
          for i in range(0, 3):
              for j in range(0, 3):
                  cutPaper(x + (i * paperSize), y + (j * paperSize), paperSize)
      
      cutPaper(0, 0, n)
      # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
      print(cnt['-1'])
      print(cnt['0'])
      print(cnt['1'])
  • ๋ฐฑ์ค€ 1992 ์ฟผ๋“œ ํŠธ๋ฆฌ : 1780๋ฒˆ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ
    • [์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์œ„, ์™ผ์ชฝ ์•„๋ž˜, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜] 4๊ฐœ์˜ ์˜์—ญ์œผ๋กœ ๋‚˜๋‰˜๋ฉฐ, 0 ๋˜๋Š” 1๋กœ ์••์ถ•๋  ์ˆ˜ ์žˆ๋‹ค.
    • ๊ด„ํ˜ธ () ์ถœ๋ ฅ์œผ๋กœ ๊ตฌ๋ถ„์ด ํ•„์š”ํ•˜๋‹ค. ๐Ÿ‘‰ ์žฌ๊ท€ ํ˜ธ์ถœ ์ „์— ๊ด„ํ˜ธ๋กœ ๋‚ด์šฉ์„ ๊ฐ์‹ธ๋„๋ก ์ถœ๋ ฅํ•ด์คฌ๋‹ค.
    • # ์ฟผ๋“œ ํŠธ๋ฆฌ
      import sys
      n = int(sys.stdin.readline().rstrip())
      video = []
      for _ in range(n):
          video.append(list(sys.stdin.readline().rstrip()))
      
      def divideVideo(x, y, n):
          if n == 1:
              print(video[x][y], end='')
              return
          
          # ๋ชจ๋‘ ๊ฐ™์€ ์ˆ˜์ธ์ง€ ํ™•์ธ
          divideFlag = False
          for i in range(x, x + n):
              for j in range(y, y + n):
                  if video[i][j] != video[x][y]:
                      divideFlag = True  # ๋‹ค๋ฅธ ์ˆ˜ ์ฒดํฌ
                      break
              if divideFlag:
                  break
      
          if not divideFlag: 
              print(video[x][y], end='')  # video[x][y]๋กœ ์••์ถ•
              return
          
          # 4๊ฐœ ์˜์—ญ : ์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์œ„, ์™ผ์ชฝ ์•„๋ž˜, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜
          size = n // 2
          print("(", end='')
          divideVideo(x, y, size)
          divideVideo(x, y + size, size)
          divideVideo(x + size, y, size)
          divideVideo(x + size, y + size, size)
          print(")", end='')
      
      divideVideo(0, 0, n)
  • ๋ฐฑ์ค€ 11403 : ํ”Œ๋ฃจ์ด๋“œ ์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ™œ์šฉ ๋ฌธ์ œ
  • ๋ฐฑ์ค€ 7569 ํ† ๋งˆํ†  : 7576๋ฒˆ ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ, ๋˜‘๊ฐ™์€ ํ† ๋งˆํ†  ๋ฌธ์ œ์ธ๋ฐ ์ƒ์ž์˜ ๋†’์ด(h)๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉฐ, ์•ž ๋’ค์˜ ์ƒ์ž์—๋„ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.
    • ๋”ฐ๋ผ์„œ ์•ž๊ณผ ๋’ค์˜ ์ƒ์ž์—๋„ ์˜ํ–ฅ์„ ์ฃผ๊ธฐ ์œ„ํ•ด ๋ฐฉํ–ฅ ๋‘๊ฐœ (-n, 0), (n, 0) ์„ ์ถ”๊ฐ€ํ–ˆ๊ณ , ์ƒํ•˜์ขŒ์šฐ ์ฒดํฌํ•  ๋•Œ ๊ฐ™์€ ์ธต์ธ์ง€ ํ™•์ธํ•˜๋„๋ก ์ถ”๊ฐ€ํ–ˆ๋‹ค.
    • # ํ† ๋งˆํ† 2 
      import sys
      from collections import deque
      def bfs(n, h, pos):
          dirs = [(-1, 0, True), (1, 0, True), (0, -1, True), (0, 1, True), (-n, 0, False), (n, 0, False)] # ์œ„, ์•„๋ž˜, ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ,
          queue = deque(pos)
          answer = 0
          while queue :
              x, y, date = queue.popleft()
              for dx, dy, sameBox in dirs:
                  nx = x + dx
                  ny = y + dy
                  if nx < 0 or nx >= (n * h) or ny < 0 or ny >= m:
                      continue
                  start = (x // n) * n   # ์ธต ์‹œ์ž‘ ๋ฒ”์œ„
                  if sameBox and (nx < start or nx >= start + n):   # ์ƒํ•˜์ขŒ์šฐ๊ฐ€ ๊ฐ™์€ ์ธต์ด ์•„๋‹Œ ๊ฒฝ์šฐ
                      continue
                  if tomato[nx][ny] == -1:
                      continue
                  if tomato[nx][ny] == 0:
                      tomato[nx][ny] = date + 1
                      queue.append((nx, ny, tomato[nx][ny]))
      
              answer = max(date, answer)
      
          for t in tomato:
              if 0 in t:
                  return -1
              
          return date
      
      m, n, h= map(int, sys.stdin.readline().rstrip().split())
      tomato = []
      for _ in range(n * h):
          tomato.append(list(map(int, sys.stdin.readline().rstrip().split())))
      pos = []    # ์ต์€ ํ† ๋งˆํ†  ์ขŒํ‘œ
      for i in range(n * h):
          for j in range(m):
              if tomato[i][j] == 1:
                  pos.append((i, j, 0))
      print(bfs(n, h, pos))

ํด๋ฆฐ์ฝ”๋“œ

  • ํด๋ฆฐ์ฝ”๋“œ ์ด์ œ๋Š” ํŒŒ์ด์ฌ์ด๋‹ค 8์žฅ_ํŒŒ์ด์ฌ์—์„œ ๋น ์ง€๊ธฐ ์‰ฌ์šด ํ•จ์ •๋“ค

์ถ”๊ฐ€์ ์œผ๋กœ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ

๊ธฐํƒ€ ๐Ÿค”

์˜ค๋Š˜์€ ์˜ค๋žœ๋งŒ์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ค‘์ ์œผ๋กœ ๊ณต๋ถ€ํ–ˆ๋‹ค. ์žฌ๋ฐŒ๋‹ค. 3์›” ์•ˆ์— ๋ฐฑ์ค€ ๊ณจ๋“œ 3์„ ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ๋‘๊ทผ!