1018๋ฒ: ์ฒด์คํ ๋ค์ ์น ํ๊ธฐ
์ง๋ฏผ์ด๋ ์์ ์ ์ ํ์์ MN๊ฐ์ ๋จ์ ์ ์ฌ๊ฐํ์ผ๋ก ๋๋์ด์ ธ ์๋ MรN ํฌ๊ธฐ์ ๋ณด๋๋ฅผ ์ฐพ์๋ค. ์ด๋ค ์ ์ฌ๊ฐํ์ ๊ฒ์์์ผ๋ก ์น ํด์ ธ ์๊ณ , ๋๋จธ์ง๋ ํฐ์์ผ๋ก ์น ํด์ ธ ์๋ค. ์ง๋ฏผ์ด๋ ์ด ๋ณด๋๋ฅผ ์๋ผ์ 8ร8 ํฌ๊ธฐ์ ์ฒด์คํ์ผ๋ก ๋ง๋ค๋ ค๊ณ ํ๋ค.
์ฒด์คํ์ ๊ฒ์์๊ณผ ํฐ์์ด ๋ฒ๊ฐ์์ ์น ํด์ ธ ์์ด์ผ ํ๋ค. ๊ตฌ์ฒด์ ์ผ๋ก, ๊ฐ ์นธ์ด ๊ฒ์์๊ณผ ํฐ์ ์ค ํ๋๋ก ์์น ๋์ด ์๊ณ , ๋ณ์ ๊ณต์ ํ๋ ๋ ๊ฐ์ ์ฌ๊ฐํ์ ๋ค๋ฅธ ์์ผ๋ก ์น ํด์ ธ ์์ด์ผ ํ๋ค. ๋ฐ๋ผ์ ์ด ์ ์๋ฅผ ๋ฐ๋ฅด๋ฉด ์ฒด์คํ์ ์์น ํ๋ ๊ฒฝ์ฐ๋ ๋ ๊ฐ์ง๋ฟ์ด๋ค. ํ๋๋ ๋งจ ์ผ์ชฝ ์ ์นธ์ด ํฐ์์ธ ๊ฒฝ์ฐ, ํ๋๋ ๊ฒ์์์ธ ๊ฒฝ์ฐ์ด๋ค.
๋ณด๋๊ฐ ์ฒด์คํ์ฒ๋ผ ์น ํด์ ธ ์๋ค๋ ๋ณด์ฅ์ด ์์ด์, ์ง๋ฏผ์ด๋ 8ร8 ํฌ๊ธฐ์ ์ฒด์คํ์ผ๋ก ์๋ผ๋ธ ํ์ ๋ช ๊ฐ์ ์ ์ฌ๊ฐํ์ ๋ค์ ์น ํด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ๋ค. ๋น์ฐํ 8*8 ํฌ๊ธฐ๋ ์๋ฌด๋ฐ์๋ ๊ณจ๋ผ๋ ๋๋ค. ์ง๋ฏผ์ด๊ฐ ๋ค์ ์น ํด์ผ ํ๋ ์ ์ฌ๊ฐํ์ ์ต์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ฒซ์งธ ์ค์ N๊ณผ M์ด ์ฃผ์ด์ง๋ค. N๊ณผ M์ 8๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 50๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ฐ์์ด๋ค. ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์๋ ๋ณด๋์ ๊ฐ ํ์ ์ํ๊ฐ ์ฃผ์ด์ง๋ค. B๋ ๊ฒ์์์ด๋ฉฐ, W๋ ํฐ์์ด๋ค.
์ฒซ์งธ ์ค์ ์ง๋ฏผ์ด๊ฐ ๋ค์ ์น ํด์ผ ํ๋ ์ ์ฌ๊ฐํ ๊ฐ์์ ์ต์๊ฐ์ ์ถ๋ ฅํ๋ค.
8 8
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
1
color=["B","W"]
board_1 = [[color[::y][x%2] for x in range(8)] for y in [1,-1,1,-1,1,-1,1,-1]]
board_2 = [[color[::y][x%2] for x in range(8)] for y in [1,-1,1,-1,1,-1,1,-1][::-1]]
board_1,board_2
([['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],
['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],
['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],
['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],
['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],
['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],
['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],
['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B']],
[['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],
['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],
['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],
['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],
['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],
['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'],
['W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'],
['B', 'W', 'B', 'W', 'B', 'W', 'B', 'W']])
#๋ณด๋ 2๋ ํ์์์-๊ฒฐ๊ณผ ๋ฐ์ ์ผ๋ก ํด๊ฒฐ
#๋์ ํ๋ก๊ทธ๋๋ฐ?
#์ถ๊ฐ ์ค๋ง ๋น๊ตํ๊ณ ๊ธฐ์กด์ ๋ต ๋ฐ์ ํด์ ๋ํจ.
1๊ฐ์ง ํ์ ์ ๊ธฐ์ค์ผ๋ก ๋ชจ๋ ๋ต ๋ฐฐ์ด์ ํ๋ฆฌ๋ฉด 0,๋ง์ผ๋ฉด 1์ธ ๋ฐฐ์ด๋ก ๋ง๋ค๊ณ ,
8*8๋ก ์๋ผ์ ๋ง์ ์, 64-๋ง์ ์ ์ค ์์ ์๋ฅผ ์ทจํจ.
import sys
h, w = (int(x) - 7 for x in input().split())
raw = sys.stdin.readlines()
board = [list(l.rstrip()) for l in raw]
result = []
base = ["B","W"]
for y,line in enumerate(board):
newline=[]
for x,cell in enumerate(line):
if cell == base[(x+y)%2]:
newline.append(1)
else:
newline.append(0)
result.append(newline)
diff = 64
for y in range(h):
for x in range(w):
corr = 0
for l in result[y:y+8]:
corr += sum(l[x:x+8])
if diff> min(corr,64-corr):
diff = min(corr,64-corr)
print(diff)
์ค๋ณต ์ฐ์ฐ์ด ๋ณด์ด๋๋ฐ 8*8์ ๋งค๋ฒ ๋ํด์ฃผ๊ณ ์๋ค.
๊ทธ๋ฌ๋ฏ๋ก x๋ฐฉํฅ์ผ๋ก 1 ์ด๋ํ๋ฉด ์ฒซ ์ด๋งํผ ๋นผ๊ณ ์ ์ด์ ํฉ๋งํผ ๋ํ๊ณ ,
y ๋ฐฉํฅ์ผ๋ก 1 ์ด๋ํ๋ฉด ์ด์ ๊ฐ์์ ์ฒซ ํ๋งํผ ๋นผ๊ณ ์๋ก์ด ํ์ ๊ณ์ฐํด์ ๋ํ๋ ๋ฐฉ๋ฒ์ด ํ์ํ๋ค.
cumulative_sums = []
for x in range(w):
cumulative_col = []
cumulative_sum = 0
for y in range(h):
cumulative_sum += result[y][x]
cumulative_col.append(cumulative_sum)
cumulative_sums.append(cumulative_col)
diff = 64
for y in range(h - 7):
for x in range(w - 7):
corr = 0
for dx in range(8):
# ์์ชฝ ํ ๊ฐ ๋นผ๊ณ ์๋์ชฝ ํ ๊ฐ ๋ํ๊ธฐ
corr += cumulative_sums[x + dx][y + 7]
if y > 0:
corr -= cumulative_sums[x + dx][y - 1]
if diff > min(corr, 64 - corr):
diff = min(corr, 64 - corr)
# ๊ฒฐ๊ณผ ์ถ๋ ฅ
diff