728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ๐Ÿ˜‚

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

๋„์ฐฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด 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. ๋ฏธ๋กœ
# ์ถœ๋ฐœ์ง€์—์„œ ๋ชฉ์ ์ง€์— ๋„์ฐฉํ•˜๋Š” ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ

# ์‚ฌ๋ฐฉ ํƒ์ƒ‰
dx = [0, 1, 0, -1] # ๋ฒ”์œ„ ์ œ๋Œ€๋กœ ํ™•์ธ
dy = [-1, 0, 1, 0]
def miro(i, j):
    global result # ๊ฒฐ๊ณผ๊ฐ’ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•ด์ฃผ๊ธฐ
    visited[i][j] = True # ๋ฐฉ๋ฌธํ•œ ๊ณณ 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}')

 

๋‚ด๊ฐ€ ์ž˜ ๋ชป ํ’€์—ˆ๋˜ ์ ...

 

1. ์‚ฌ๋ฐฉ ํƒ์ƒ‰ ๋ฒ”์œ„ ํ™•์ธ!!! : ์ž˜๋ชป ์ ์—ˆ๋“œ๋ž˜์š”.. ๊ผญ ํ™•์ธํ•˜๊ณ  ์ ๊ธฐ;;;

2. Runtime error : strip()์„ ์‚ฌ์šฉํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค๊ณ  ๊ทธ๋Ÿฌ๋Š”๋ฐ...

    ์šฐ๋ฆฌ ๋ˆˆ์—๋Š” ์•ˆ ๋ณด์ด์ง€๋งŒ \n ์ด ํฌํ•จ ๋˜์–ด ์žˆ์„์ง€๋„ ๋ชจ๋ฅธ๋‹ค๊ณ ํ•˜๋„ค์š”....

    ๊ทธ๋ฆฌ๊ณ  input()์€ ์•Œ์•„์„œ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ด์ฃผ์ง€๋งŒ

    sys๋ฅผ ์‚ฌ์šฉํ•  ๋–„๋Š” ๊ทธ๋Ÿฐ ๊ธฐ๋Šฅ์ด ์—†์–ด์„œsys.stdin.readline().strip() ๊ฐ™์ด ๋ฌด์กฐ๊ฑด ์จ์ค˜์•ผ ํ•œ๋ฐ์š”!! ๐Ÿ˜ต‍๐Ÿ’ซ

 

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

 

1) ํƒ์ƒ‰

: ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •

 ex) DFS, BFS

 

 

2) DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰)

  • ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ์— ๊ธฐ์ดˆํ•œ๋‹ค..
  • ์‹ค์žฌ๋กœ๋Š” ์Šคํƒ๋ณด๋‹ค๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ„๊ฒฐํ•˜๊ฒŒ(?) ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค
  • ์‹œ๊ฐ„ ๋ณต์žก๋„ : O(N)
# dfs & ์žฌ๊ท€ ํ•จ์ˆ˜
def dfs(v): # v : ์‹œ์ž‘์ 
    global visited, graphs  # ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋งŒ๋“ค์–ด์ค€๊ฒŒ ์•„๋‹ˆ๋ผ ์•ˆ์ •์„ฑ์„ ์œ„ํ•ด ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค
    visited[v] = True
    
    for i in graphs[v]: # ์ธ์ ‘ํ•ด ์žˆ๋Š” ๊ณณ ์ค‘์— 
        if not visited[i]: # ๋ฐฉ๋ฌธ์„ ์•ˆํ–ˆ๋‹ค๋ฉด
            dfs(i)  # ๊ทธ ๊ณณ์„ ํƒ์ƒ‰!!
        
        else:
            continue # ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ ํŒจ์Šค # ์ด ๋ถ€๋ถ„ ์•ˆ ์ ์–ด์ค˜๋„ ๋œ๋‹ค
            
graph = [] # ๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ(์ธ์ ‘ ํ–‰๋ ฌ ์–ด๋ ค์›€ ใ…œใ…œ)
visited = [] # ๊ฐ ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ๋œ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” 1 ์ฐจ์› ๋ฆฌ์ŠคํŠธ(true, flase/ 0,1๋กœ ๊ตฌ๋ฌธ)

dfs(0)

 

 => ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•  ๋–„๋Š” dfs ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹๋‹ค๊ณ  ํ•˜๋„ค์š”..

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts