[BOJ][๐ŸŸก4][๋ฐฑ์ค€#15683] ๊ฐ์‹œ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #15683


๋ฌธ์ œ

์Šคํƒ€ํŠธ๋งํฌ์˜ ์‚ฌ๋ฌด์‹ค์€ 1ร—1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋Š” Nร—M ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ๋ฌด์‹ค์—๋Š” ์ด K๊ฐœ์˜ CCTV๊ฐ€ ์„ค์น˜๋˜์–ด์ ธ ์žˆ๋Š”๋ฐ, CCTV๋Š”ย 5๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. ๊ฐ CCTV๊ฐ€ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1๋ฒˆ 2๋ฒˆ 3๋ฒˆ 4๋ฒˆ 5๋ฒˆ

1๋ฒˆ CCTV๋Š” ํ•œ ์ชฝ ๋ฐฉํ–ฅ๋งŒ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. 2๋ฒˆ๊ณผ 3๋ฒˆ์€ ๋‘ ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, 2๋ฒˆ์€ ๊ฐ์‹œํ•˜๋Š” ๋ฐฉํ–ฅ์ด ์„œ๋กœ ๋ฐ˜๋Œ€๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•˜๊ณ , 3๋ฒˆ์€ ์ง๊ฐ ๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•œ๋‹ค. 4๋ฒˆ์€ ์„ธ ๋ฐฉํ–ฅ, 5๋ฒˆ์€ ๋„ค ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. CCTV๋Š” ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์— ์žˆ๋Š” ์นธ ์ „์ฒด๋ฅผ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ๋ฌด์‹ค์—๋Š” ๋ฒฝ์ด ์žˆ๋Š”๋ฐ, CCTV๋Š” ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†๋‹ค. CCTV๊ฐ€ ๊ฐ์‹œํ•  ์ˆ˜ ์—†๋Š” ์˜์—ญ์€ ์‚ฌ๊ฐ์ง€๋Œ€๋ผ๊ณ  ํ•œ๋‹ค. CCTV๋Š” ํšŒ์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํšŒ์ „์€ ํ•ญ์ƒ 90๋„ ๋ฐฉํ–ฅ์œผ๋กœ ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฐ์‹œํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ ๋˜๋Š” ์„ธ๋กœ ๋ฐฉํ–ฅ์ด์–ด์•ผ ํ•œ๋‹ค.

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 6 0 0 0 0 0 0 0 ์ง€๋„์—์„œ 0์€ ๋นˆ ์นธ, 6์€ ๋ฒฝ, 1~5๋Š” CCTV์˜ ๋ฒˆํ˜ธ์ด๋‹ค. ์œ„์˜ ์˜ˆ์‹œ์—์„œ 1๋ฒˆ์˜ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์„ โ€˜#โ€™๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 # 6 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0

# 1 0 6 0

0 0 0 0 0 0

0 0 # 0 0 0 0 0 # 0 0 0 0 0 1 0 6 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 6 0 0 0 # 0 0 0

โ†’ โ† โ†‘ โ†“

CCTV๋Š” ๋ฒฝ์„ ํ†ต๊ณผํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, 1๋ฒˆ์ด โ†’ ๋ฐฉํ–ฅ์„ ๊ฐ์‹œํ•˜๊ณ  ์žˆ์„ ๋•Œ๋Š” 6์˜ ์˜ค๋ฅธ์ชฝ์— ์žˆ๋Š” ์นธ์„ ๊ฐ์‹œํ•  ์ˆ˜ ์—†๋‹ค.

0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 6 0 0 6 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 5 ์œ„์˜ ์˜ˆ์‹œ์—์„œ ๊ฐ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์„ ์•Œ์•„๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

0 0 0 0 0 #

2 # # #

0 0 0 0 6 # 0 6 # # 2 # 0 0 0 0 0 #

# # # # 5

0 0 0 0 0 #

2 # # #

0 0 0 0 6 # 0 6 0 0 2 # 0 0 0 0 # #

# # # # 5

0 # 0 0 0 # 0 2 0 0 0 # 0 # 0 0 6 # 0 6 # # 2 # 0 0 0 0 0 #

# # # # 5

0 # 0 0 0 # 0 2 0 0 0 # 0 # 0 0 6 # 0 6 0 0 2 # 0 0 0 0 # #

# # # # 5

์™ผ์ชฝ ์ƒ๋‹จ 2: โ†”, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2:ย โ†” ์™ผ์ชฝ ์ƒ๋‹จ 2: โ†”, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2:ย โ†• ์™ผ์ชฝ ์ƒ๋‹จ 2: โ†•, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2:ย โ†” ์™ผ์ชฝ ์ƒ๋‹จ 2: โ†•, ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ 2:ย โ†•

CCTV๋Š” CCTV๋ฅผ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž.

0 0 2 0 3 0 6 0 0 0 0 0 6 6 0 0 0 0 0 0

์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์— 2์˜ ๋ฐฉํ–ฅ์ด โ†• 3์˜ ๋ฐฉํ–ฅ์ดย โ†์™€ย โ†“์ธ ๊ฒฝ์šฐ ๊ฐ์‹œ๋ฐ›๋Š” ์˜์—ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

# 2 # 3

0 6 # 0 # 0 0 6 6 # 0 0 0 0 #

์‚ฌ๋ฌด์‹ค์˜ ํฌ๊ธฐ์™€ ์ƒํƒœ, ๊ทธ๋ฆฌ๊ณ  CCTV์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, CCTV์˜ ๋ฐฉํ–ฅ์„ ์ ์ ˆํžˆ ์ •ํ•ด์„œ, ์‚ฌ๊ฐ ์ง€๋Œ€์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์‚ฌ๋ฌด์‹ค์˜ ์„ธ๋กœ ํฌ๊ธฐ N๊ณผ ๊ฐ€๋กœ ํฌ๊ธฐ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N, M โ‰ค 8) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์‚ฌ๋ฌด์‹ค ๊ฐ ์นธ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ์นธ, 6์€ ๋ฒฝ, 1~5๋Š” CCTV๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , ๋ฌธ์ œ์—์„œ ์„ค๋ช…ํ•œ CCTV์˜ ์ข…๋ฅ˜์ด๋‹ค.ย  CCTV์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋Š” 8๊ฐœ๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์‚ฌ๊ฐ ์ง€๋Œ€์˜ ์ตœ์†Œ ํฌ๊ธฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

4 6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 0 6 0
0 0 0 0 0 0


์ถœ๋ ฅ

20


์˜ˆ์ œ 2

์ž…๋ ฅ

6 6
0 0 0 0 0 0
0 2 0 0 0 0
0 0 0 0 6 0
0 6 0 0 2 0
0 0 0 0 0 0
0 0 0 0 0 5


์ถœ๋ ฅ

15


์˜ˆ์ œ 3

์ž…๋ ฅ

6 6
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1


์ถœ๋ ฅ

6


์˜ˆ์ œ 4

์ž…๋ ฅ

6 6
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 5 0 0
0 0 5 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1


์ถœ๋ ฅ

2


์˜ˆ์ œ 5

์ž…๋ ฅ

1 7
0 1 2 3 4 5 6


์ถœ๋ ฅ

0


์˜ˆ์ œ 6

์ž…๋ ฅ

3 7
4 0 0 0 0 0 0
0 0 0 2 0 0 0
0 0 0 0 0 0 4


์ถœ๋ ฅ

0


My Sol

import sys
input = sys.stdin.readline

def dfs(i, N):
    global didj_dict
    if i == N:
        findStart()
        return

    ci, cj, ck = CCTVs[i]
    for didj_lst in didj_dict[ck]:
        cctvSet.append((ci, cj, didj_lst))
        dfs(i+1, N)
        cctvSet.pop()


def findStart():
    global cctvSet, mat, min_ns
    global I, J

    # mat2 ์ œ์ž‘
    mat2 = []
    for lst in mat:
        mat2.append(lst[::])

    # ๋ชจ๋“  CCTV์˜ ๊ฐ์‹œ์ง€์—ญ ์ฒดํฌ
    for cctvInfo in cctvSet:
        ci, cj, didj_lst = cctvInfo
        for di, dj in didj_lst:
            k = 1
            while 0<=ci+di*k<I and 0<=cj+dj*k<J:
                # ํƒ์ƒ‰ ์ขŒํ‘œ์˜ ๊ฐ’์ด 0์ด๋ฉด # ๋„ฃ๊ธฐ
                if not mat2[ci+di*k][cj+dj*k]:
                    mat2[ci+di*k][cj+dj*k] = '#'
                # ํƒ์ƒ‰ ์ขŒํ‘œ์˜ ๊ฐ’์ด 6์ด๋ฉด ๋ฐ˜๋ณต ํƒˆ์ถœ
                elif mat2[ci+di*k][cj+dj*k]==6:
                    break

                k += 1
    # mat2์˜ 0 ์ฒดํฌ
    sum_v = 0
    for i in range(I):
        for j in range(J):
            if not mat2[i][j]:
                sum_v += 1

    # sum_v = 0์ด๋ฉด ์ข…๋ฃŒ
    if not sum_v:
        print(0)
        quit()

    # ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹ 
    if min_ns > sum_v:
        min_ns = sum_v



# ์ž…๋ ฅ ์ฒ˜๋ฆฌ
I, J = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(I)]
didj_dict = {
    1: [[(-1,0)],
        [(0,-1)],
        [(1,0)],
        [(0,1)]],
    2: [[(-1,0),(1,0)],
        [(0,-1),(0,1)]],
    3: [[(0,-1),(-1,0)],
        [(-1,0),(0,1)],
        [(0,1),(1,0)],
        [(1,0),(0,-1)]],
    4: [[(-1,0),(0,1),(1,0)],
        [(0,-1),(0,1),(1,0)],
        [(0,-1),(-1,0),(1,0)],
        [(0,-1),(-1,0),(0,1)]],
    5: [[(0,-1),(-1,0),(0,1),(1,0)]]}

# CCTV ์ขŒํ‘œ, ์ข…๋ฅ˜ ์ €์žฅ
cctvN = 0
CCTVs = []
for i in range(I):
    for j in range(J):
        if 0 < mat[i][j] < 6:
            CCTVs.append((i, j, mat[i][j]))
            cctvN += 1

# main ์‹คํ–‰
cctvSet = []
min_ns = I*J
dfs(0, cctvN)

# ๊ฒฐ๊ณผ ์ถœ๋ ฅ
print(min_ns)

๋ชจ๋“  ์ƒํ™ฉ์— ๋Œ€ํ•ด์„œ ์™„์ „ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. cctv์˜ ํƒ€์ž…๋ณ„ ๋ฐ”๋ผ๋ณด๋Š” ๊ฐ๋„์˜ delta ๋‹จ์œ„๋ฅผ dictionary๋กœ ์ง€์ •ํ•ด ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜์˜€๋‹ค. CCTV์˜ ์ขŒํ‘œ์™€ ํ•จ๊ป˜ ํƒ€์ž…๊นŒ์ง€ ํ•จ๊ป˜ ์ €์žฅํ•ด dfs๋กœ ๋Œ๋ ธ์œผ๋ฉฐ, CCTV์˜ ํƒ€์ž… ๋•Œ๋ฌธ์— ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋งŽ์ด ๋‚˜์™€ ๋ณต์žกํ•œ ๊ฒƒ๋งŒ ์ œ์™ธํ•˜๊ณ ๋Š” ํ๋ฆ„๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. CCTV ํƒ€์ž…์„ ๊ฐ๊ฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ ํ•˜๊ณ , dfs ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ for๋ฌธ์œผ๋กœ stack์„ ๋นผ๊ณ  ๋„ฃ๋Š” ๊ณผ์ •์„ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ๊ด€๊ฑด์ด์—ˆ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ