์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step11] 1018 : ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 18. 18:39

www.acmicpc.net/problem/1018

 

1018๋ฒˆ: ์ฒด์ŠคํŒ ๋‹ค์‹œ ์น ํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— N๊ณผ M์ด ์ฃผ์–ด์ง„๋‹ค. N๊ณผ M์€ 8๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ณด๋“œ์˜ ๊ฐ ํ–‰์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. B๋Š” ๊ฒ€์€์ƒ‰์ด๋ฉฐ, W๋Š” ํฐ์ƒ‰์ด๋‹ค.

www.acmicpc.net


 

์ฒด์ŠคํŒ์€ ๋งจ ์™ผ์ชฝ ์œ„ ์นธ์ด ์œ„์ชฝ์ด ๊ฒ€์ •์ƒ‰์ธ ๊ฒฝ์šฐ, ํฐ์ƒ‰์ธ ๊ฒฝ์šฐ ๋‘๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

์ฒด์ŠคํŒ์˜ ํฌ๊ธฐ๊ฐ€ 8*8๋ณด๋‹ค ํด ๊ฒฝ์šฐ ์ฒด์ŠคํŒ์„ ์ž˜๋ผ 8*8 ํฌ๊ธฐ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ž๋ฅธ ํ›„ ์ฒด์ŠคํŒ์ฒ˜๋Ÿผ ์ƒ‰์„ ์น ํ•  ๋•Œ ๊ฐ€์žฅ ์ ๊ฒŒ ์ •์‚ฌ๊ฐํ˜•์„ ์น ํ•˜๋Š” ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

 

๊ฐ€์žฅ ์ ๊ฒŒ ์ •์‚ฌ๊ฐํ˜•์„ ์น ํ•˜๋ ค๋ฉด ์–ด๋Š ๋ถ€๋ถ„์„ ์ž˜๋ผ์•ผ ํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, (0, 0) ์œ„์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ž๋ฅผ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธํ•ด ๊ฐ€์žฅ ์ ์€ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

def re_bboard(mboard, x, y)

๋ณด๋“œ์™€ ๊ฒ€์‚ฌํ•  (x, y)์ขŒํ‘œ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์‹œ์ž‘์ด ๊ฒ€์ •์ƒ‰์ธ ๋ณด๋“œ์™€ ๋น„๊ตํ•˜์—ฌ ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•  ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

 

def re_wboard(mboard, x, y)

๋ณด๋“œ์™€ ๊ฒ€์‚ฌํ•  (x, y)์ขŒํ‘œ๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์‹œ์ž‘์ด ํฐ์ƒ‰์ธ ๋ณด๋“œ์™€ ๋น„๊ตํ•˜์—ฌ ๋‹ค์‹œ ์น ํ•ด์•ผ ํ•  ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

 

 

1. ์ž…๋ ฅ๋ฐ›์€ nํฌ๊ธฐ์˜ myboard ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋“œ์˜ ์ƒํƒœ๋ฅผ ์ €์žฅ

2. ์ด์ค‘๋ฃจํ”„๋ฅผ ํ†ตํ•ด ๋ณด๋“œ๋ฅผ ์ž๋ฅผ ์™ผ์ชฝ ์œ„ ๊ผญ์ง€์  ์ขŒํ‘œ๋ฅผ (0, 0) ๋ถ€ํ„ฐ re_bboard()์™€ re_wboard()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋ฐ˜ํ™˜๊ฐ’์„ mincnt ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ

3.mincnt์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅ

# 1018.py

def re_bboard(mboard, x, y):
    cnt = 0
    board = ['BWBWBWBW', 'WBWBWBWB'] * 4
    for i in range(8):
        for j in range(8):
            if mboard[x + i][y + j] != board[i][j]:
                cnt += 1
    return cnt


def re_wboard(mboard, x, y):
    cnt = 0
    board = ['WBWBWBWB', 'BWBWBWBW'] * 4
    for i in range(8):
        for j in range(8):
            if mboard[x + i][y + j] != board[i][j]:
                cnt += 1
    return cnt


n, m = map(int, input().split())
myboard = [input() for _ in range(n)]
mincnt = []

for i in range(n - 7):
    for j in range(m - 7):
        mincnt.append(re_wboard(myboard, i, j))
        mincnt.append(re_bboard(myboard, i, j))

print(min(mincnt))