728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ๐Ÿค”

NxN ํฌ๊ธฐ์˜ ๋ฏธ๋กœ์—์„œ ์ถœ๋ฐœ์ง€์—์„œ ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋„์ฐฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

์ฃผ์–ด์ง„ ๋ฏธ๋กœ ๋ฐ–์œผ๋กœ๋Š” ๋‚˜๊ฐˆ ์ˆ˜ ์—†๋‹ค.

๋‹ค์Œ์€ 5x5 ๋ฏธ๋กœ์˜ ์˜ˆ์ด๋‹ค.
 

13101

10101

10101

10101

10021

 

๋งˆ์ง€๋ง‰ ์ค„์˜ 2์—์„œ ์ถœ๋ฐœํ•ด์„œ 0์ธ ํ†ต๋กœ๋ฅผ ๋”ฐ๋ผ ์ด๋™ํ•˜๋ฉด ๋งจ ์œ—์ค„์˜ 3์— ๋„์ฐฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.

 
 

[์ž…๋ ฅ]
 

์ฒซ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.  1<=T<=50
 

๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๋ณ„๋กœ ๋ฏธ๋กœ์˜ ํฌ๊ธฐ N๊ณผ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๋ฏธ๋กœ์˜ ํ†ต๋กœ์™€ ๋ฒฝ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. 0์€ ํ†ต๋กœ, 1์€ ๋ฒฝ, 2๋Š” ์ถœ๋ฐœ, 3์€ ๋„์ฐฉ์ด๋‹ค. 5<=N<=100

 

[์ถœ๋ ฅ]
 

๊ฐ ์ค„๋งˆ๋‹ค "#T" (T๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ฒˆํ˜ธ)๋ฅผ ์ถœ๋ ฅํ•œ ๋’ค, ๊ณ„์‚ฐ๊ฒฐ๊ณผ๋ฅผ ์ •์ˆ˜๋กœ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ๋˜๋Š” ‘error’๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

๋‚ด ํ’€๐Ÿฆท

# 4875. ๋ฏธ๋กœ
# ์ถœ๋ฐœ์ง€์—์„œ ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
# ๋งˆ์ง€๋ง‰ ์ค„์˜ 2์—์„œ ์ถœ๋ฐœ, 0์ธ ํ†ต๋กœ๋ฅผ ๋”ฐ๋ผ ์ด๋™ํ•˜๋ฉด ๋งจ ์œ—์ค„์˜ 3์— ๋„์ฐฉํ•˜๋Š”์ง€
# ์‚ฌ๋ฐฉ ํƒ์ƒ‰
dx = [0, 1, 0, -1] # ๋ฒ”์œ„ ์ œ๋Œ€๋กœ ํ™•์ธ
dy = [-1, 0, 1, 0]
def miro(i, j):
    global result # ๊ฒฐ๊ณผ๊ฐ’ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•ด์ฃผ๊ธฐ
    visited[i][j] = True
    for k in range(4):
        di = i + dx[k]
        dj = j + dy[k] # ํ•˜๋‚˜ํ•˜๋‚˜ ์‹ ๊ฒฝ์จ์„œ ์ ๊ธฐ
        if 0 <= di < N and 0 <= dj < N and not visited[di][dj]:
            # ํƒ์ƒ‰์•ˆํ–ˆ๊ณ  0์ด๋ฉด ์žฌ๊ท€
            if arr[di][dj] == 0:
                miro(di, dj)
            # 3 ๋„์ฐฉํ•˜๋ฉด ์„ฑ๊ณต
            elif arr[di][dj] == 3:
                result = 1 # ๊ฒฐ๊ณผ๊ฐ’ ์„ฑ๊ณต์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
                break

T = int(input())
for tc in range(1, T + 1):
    N = int(input()) # ๋ฏธ๋กœ์˜ ํฌ๊ธฐ
    arr = [list(map(int, input().strip())) for _ in range(N)]
    # ๋ฐฉ๋ฌธ ๊ฒ€์‚ฌ
    visited = [[False for _ in range(N)] for _ in range(N)]
    result = 0 # ๊ฒฐ๊ณผ๊ฐ’

    for i in range(N-1, -1, -1):
        for j in range(N-1, -1, -1):
            if arr[i][j] == 2:
                miro(i, j) # ํ•จ์ˆ˜์— ์‹œ์ž‘์  ๋„ฃ์–ด์ฃผ๊ธฐ

    print(f'#{tc} {result}')

๋ฒฝ์ด ์—†๋Š”๊ณณ์„ ํ”ผํ•ด์„œ ๊ฐ€์•ผํ•œ๋‹ค๊ณ ๋ผ๊ณ ๋ผ๊ณ ๋ผํŒŒ๋• ํ˜ผ์ž ์ด๋Ÿฌ๊ณ  ์žˆ๋‹ค๊ฐ€..

๋ญ”๊ฐ€ ์˜ค๋Š˜ ์‹œํ—˜์—์„œ ์‚ฌ์šฉํ•œ(?) ๋ธํƒ€ + dfs์—์„œ ๋ฐฐ์šด ๊ฐœ๋…๋“ค + ์žฌ๊ท€ ๋‹ค ์“ฐ๋ฉด ํ•ด๊ฒฐํ•  ๋“ฏ(?)์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค์•„ ๐Ÿค”

 


์ค‘๊ฐ„์— ์‹ค์ˆ˜ํ•œ ๋ถ€๋ถ„๋“ค ๐Ÿ˜ต‍๐Ÿ’ซ

- ๋ธํƒ€ ๋ฒ”์œ„ (์ทจ์ค‘ ์ฝ”๋”ฉํ•ด์„œ ๊ทธ๋Ÿฐ๊ฐ€ ์ž˜๋ชป ์ ์Œ;;;;

- dj = i + dk[k] (์—ญ์‹œ ์ž˜๋ชป ์ ์Œ....)

- ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ strip() ์จ์ฃผ์ง€ ์•Š์•„์„œ ๋Ÿฐํƒ€์ž„์—๋Ÿฌ ๋ฐœ์ƒ.... => ์‚ฌ์‹ค ์•„์ง๋„ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Œ ์™œ์ธ์ง€...

strip()์€ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋ฉด ๋˜๋Š”๊ฑฐ์ž๋‚˜์š”??

split()์„ ์•ˆ ์ ์–ด์ฃผ๋ฉด ๋œ๋‹ค ์•„๋‹Œ๊ฐ€์š”??

์•„์‹œ๋Š” ๋ฉ‹์ง„๋ถ„์˜ ๋Œ“๊ธ€ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค...

 

์ฐธ๊ณ ํ•จ๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

1) ๋ธํƒ€๋ฅผ ์ด์šฉํ•œ 2์ฐจ ๋ฐฐ์—ด ํƒ์ƒ‰

: 2์ฐจ ๋ฐฐ์—ด์˜ ํ•œ ์ขŒํ‘œ์—์„œ 4๋ฐฉํ–ฅ์˜ ์ธ์ ‘ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•(8๋ฐฉ๋„ ๊ฐ€๋Šฅ)

arr[0...N-1][0...N-1] # N * N ๋ฐฐ์—ด
di = [0, 1, 0, -1] # ํ•˜, ์šฐ, ์ƒ, ์ขŒ
dj = [1, 0, -1, 0]
for i in range(N):
	for j in range(N): # ์—ฌ๊ธฐ๊นŒ์ง€๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ”ํžˆ์•„๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด
    	for k in range(4): # ๋ธ์ฐจ๋ฅผ ์ด์šฉ
        	ni = i + di[k]
            nj = j + dj[k]
            if 0 <= ni < N and 0 <= nj < N: # ๋ฒ”์œ„์— ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์ธ๋ฑ์Šค๋ฉด...
            	f(arr[ni][nj]) # ์ด ์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋”ํ•ด์ฃผ๋“  ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ฃผ๋˜ ๋” ๋‚˜์•„๊ฐ€๋˜ ๋ฌด๊ถ๋ฌด์ง„ํ•œ ๋ฌธ์ œ๋“ค์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค

 

2) ์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•œ dfs

์žฌ๊ท€๋ฅผ ํ™œ์šฉํ•จ์œผ๋กœ์จ ์‰ฝ๊ฒŒ dfs๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค(๋‚ด์šฉ์€ ์•ˆ์‰ฌ์›€... ์ดํ•ดํ•˜๊ธฐ ํž˜๋“ค์—ˆ์Œ..)

def dfs(v):
    global visited
    # print(v, "-", end='')
    visited[v] = True # ์ด๊ฑฐ์— ํ•ด๋‹นํ•˜๋ฉด ์ง„ํ–‰
    # v ๋…ธ๋“œ์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ํ™•์ธ (์ธ์ ‘ํ•œ ๋…ธ๋“œ)
    for u in graphs[v]:
        if visited[u] == True:  # ๋ฐฉ๋ฌธ์„ ํ–ˆ๋‹ค๋ฉด
            continue
        # ๋ฐฉ๋ฌธ์„ ํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๋ฐฉ๋ฌธ์„ ์ง„ํ–‰
        else: # u๊ฐ€ visited์•ˆ์— ๋“ค์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด u๋ฅผ ๋‹ค์‹œ dfs ํ•ด์ฃผ๋ฉด ๋œ๋‹ค
            dfs(u)  # ๋ชจ๋“  ์š”์†Œ๊ฐ€ ๋‹ค ๋Œ์•„๊ฐ€์„œ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํŠธ๋ฃจ๋กœ ๋ฐ”๋€Œ๋ฉด ๋๋‚œ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts