๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 1018 - ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

by YWTechIT 2021. 5. 24.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 1018 - ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

๋ฐฑ์ค€ 1018 - ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ


โšก๏ธ ๋‚˜์˜ ํ’€์ด

์ด ๋ฌธ์ œ์—์„œ ๊ณ ๋ฏผํ•ด์•ผ ํ•  ๋ถ€๋ถ„์€ ํฌ๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋‰œ๋‹ค.

  1. ์ขŒ์ธก ์ƒ๋‹จ์ด B / W๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ฒด์ŠคํŒ์„ ๋งŒ๋“ ๋‹ค. (์ด๋•Œ, ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉด์„œ ์ƒ‰์น ๋˜์–ด์žˆ์–ด์•ผ ํ•จ.)
  2. N * M ํฌ๊ธฐ์˜ ๋ณด๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. (์ด๋•Œ, N์€ ์„ธ๋กœ, M์€ ๊ฐ€๋กœ)
  3. N * M ํฌ๊ธฐ์˜ ๋ณด๋“œ๋ฅผ 8 * 8ํ˜•ํƒœ๋กœ ์ž๋ฅด๊ณ , 1๋ฒˆ์—์„œ ์ž‘์„ฑํ•œ ๋‘ ๊ฐœ์˜ ์ฒด์ŠคํŒ์˜ ์ขŒํ‘œ๋ฅผ ์„œ๋กœ ๋น„๊ตํ•œ๋‹ค.
  4. ์ขŒํ‘œ์˜ ๊ฐ’์ด ์„œ๋กœ ๋‹ค๋ฅด๋ฉด cnt๋ฅผ ์ฆ๊ฐ€ํ•˜๋Š”๋ฐ, ๋” ์ž‘์€ ๊ฐ’์„ return ํ•œ๋‹ค.

 

1๋ฒˆ ๋ฐฉ๋ฒ•์„ ์ขŒํ‘œ๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ์ผ์ •ํ•œ ๊ทœ์น™์œผ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  ๋‚ด ๋ฐฉ์‹๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ์‹œ๊ฐ„์„ ๋‹ค ๊นŒ๋จน๊ณ  ๊ตฌํ˜„์„ ํ•˜์ง€๋„ ๋ชปํ–ˆ๋‹ค. ๐Ÿ˜ญ ๐Ÿ˜ญ W๊ฐ€ ๋จผ์ € ์‹œ์ž‘ํ•˜๋Š” ์ฒด์ŠคํŒ์„ ๋งŒ๋“ค ๋•Œ ์ขŒํ‘œ๋ฅผ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์ด ์žˆ๋‹ค.

  1. W๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ์ขŒํ‘œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  2. W์˜ ์ขŒํ‘œ i+j๋Š” ๋ชจ๋‘ ์ง์ˆ˜์ด๋‹ค.
  3. B๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ์ขŒํ‘œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  4. B์˜ ์ขŒํ‘œ i+j๋Š” ๋ชจ๋‘ ํ™€์ˆ˜์ด๋‹ค.

 

 

์ดํ›„ n*m ๋ฐฐ์—ด์—์„œ 8*8 ์ขŒํ‘œ๋ฅผ ๋ชจ๋‘ ํ™•์ธํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด๊ฒƒ๋„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ž˜ ๋ชฐ๋ž๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ ๋œ๋‹ค๋ฉด ์ €๋ฒˆ์— ์ž‘์„ฑํ•œ ๊ธ€์„ ๋ณด์ž.

  1. n-7, m-7์˜ ๋ฒ”์œ„๋กœ ์ด์ค‘ ๋ฐ˜๋ชฉ๋ฌธ์„ ์„ ์–ธํ•œ๋‹ค. n๊ณผ m์ด ๋ชจ๋‘ 8์ด๋ฉด, n-7 = 1, m-7 = 1์ด ๋˜๋Š”๋ฐ ๋ฐ˜๋ณต๋ฌธ์˜ ๋ฒ”์œ„๋Š” 1-1๊นŒ์ง€ ์„ธ๊ธฐ๋•Œ๋ฌธ์— 0๊นŒ์ง€๋งŒ ๋ฒ”์œ„๋กœ ์žกํžŒ๋‹ค.
  2. 1๋ฒˆ์—์„œ ์„ ์–ธํ•œ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์ด 0์œผ๋กœ ๊ณ ์ •๋˜๋Š”๋™์•ˆ 8*8์˜ ๋ฒ”์œ„๋กœ ํ•œ๋ฒˆ ๋” ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์„ ์–ธํ•œ๋‹ค.
  3. chessํŒ๊ณผ ๋น„๊ตํ•œ๋‹ค.

 

๊ฐ’์ด 2๊ฐœ ์ด์ƒ์ธ๋ฐ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐฑ์‹ ํ•  ๋•Œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜์ž.

  1. cnt = min(cnt, temp1)
  2. cnt = min(cnt, temp2)
# 4์ค‘ ๋ฐ˜๋ณต๋ฌธ
n, m = map(int, input().split())
chess_W = [[0] * m for _ in range(n)]
chess_B = [[0] * m for _ in range(n)]
arr = [list(input()) for _ in range(n)]
cnt = float('inf')

for i in range(n):
    for j in range(m):
        if not (i+j) % 2:
            chess_W[i][j] = 'W'
            chess_B[i][j] = 'B'
        else:
            chess_W[i][j] = 'B'
            chess_B[i][j] = 'W'

for i in range(n-7):
    for j in range(m-7):
        temp_W, temp_B = 0, 0
        for x in range(8):
            for y in range(8):
                if arr[i+x][j+y] != chess_W[i+x][j+y]:
                    temp_W += 1
                if arr[i+x][j+y] != chess_B[i+x][j+y]:
                    temp_B += 1
        cnt = min(cnt, temp_W)
        cnt = min(cnt, temp_B)
print(cnt)
# 2์ค‘ ๋ฐ˜๋ณต๋ฌธ + ํ•จ์ˆ˜
n, m = map(int, input().split())
chess_W = [[0] * m for _ in range(n)]
chess_B = [[0] * m for _ in range(n)]
arr = [list(input()) for _ in range(n)]
cnt = float('inf')

def check(a, b):
    global cnt
    temp_W, temp_B = 0, 0
    for x in range(8):
        for y in range(8):
            if arr[a + x][b + y] != chess_W[a + x][b + y]:
                temp_W += 1
            if arr[a + x][b + y] != chess_B[a + x][b + y]:
                temp_B += 1
    cnt = min(cnt, temp_W)
    cnt = min(cnt, temp_B)
    return cnt

for i in range(n):
    for j in range(m):
        if not (i+j) % 2:
            chess_W[i][j] = 'W'
            chess_B[i][j] = 'B'
        else:
            chess_W[i][j] = 'B'
            chess_B[i][j] = 'W'

for i in range(n-7):
    for j in range(m-7):
        check(i, j)

print(cnt)
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€