๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Problem Solving/SWEA

swea14190. ๋ฏผ์ฝ”์”จ์˜ ๊ฝƒ๋ฐญ(python)

by DevIseo 2022. 4. 11.

https://swexpertacademy.com/main/code/userProblem/userProblemDetail.do?contestProbId=AX_PdObKe04DFARi&categoryId=AX_PdObKe04DFARi&categoryType=CODE 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

from collections import deque
T = int(input())
for tc in range(1,T+1):
    row,col = map(int,input().split())
    arr=[list(map(int,input().split())) for _ in range(row)]
    sy,sx=map(int,input().split())
    visit = [[0] * col for _ in range(row)]
    time=1 #1์ผ๋ถ€ํ„ฐ๋‹ˆ๊นŒ
    lst=[]

    q =deque()
    q.append((sy,sx,time))
    visit[sy][sx]= 1
    lst.append(arr[sy][sx]+time)


    def bfs():
        if q:
            temp=q[0][2] #๋‚ ์งœ
        global time,lst
        while q:
            ny,nx,nmove =q.popleft()


            if nmove !=temp:
                q.append((ny,nx,nmove))
                break

            directy=[-1,1,0,0]
            directx=[0,0,-1,1]
            for i in range(4):
                dy=ny+directy[i]
                dx=nx+directx[i]

                if 0<=dy<row and 0<=dx<col:
                    if arr[dy][dx]>0 and visit[dy][dx]==0:
                        visit[dy][dx]=1
                        q.append((dy,dx,nmove+1))
                        lst.append(arr[dy][dx]+time)

    Mflower=0
    Mtime=0
    def calculation(): #์ตœ๋Œ€ ๊ฝƒ์˜ ๊ฐฏ์ˆ˜์™€ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” ํ•ด๋‹น ์ผ ๊ตฌํ•˜๊ธฐ
        global Mflower,Mtime,time,lst
        flower = 0
        lst.sort()
        for i in range(len(lst)):
            if lst[i]>time:
                lst=lst[i:]
                break
        flower=len(lst)
        if Mflower<flower:
            Mflower=flower
            Mtime = time



    while q: #q๊ฐ€ ์žˆ์„ ๋•Œ๊นŒ์ง€ while๋ฌธ ๋Œ๊ธฐ
        calculation()
        time += 1
        bfs()

    print(f'#{tc} {Mtime}์ผ {Mflower}๊ฐœ')

๋Œ“๊ธ€