// Google Adsense

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=4&contestProbId=AV14ABYKADACFAYh&categoryId=AV14ABYKADACFAYh&categoryType=CODE&problemTitle=L&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=4&pageSize=10&pageIndex=1 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

🔥 작성코드 (🌪️62,472kb 227ms)

T = 10

for _ in range(T):
    test_case = int(input())
    arr = [list(map(int, input().split())) for _ in range(100)]

    di = [1, 0, 0]
    dj = [0, 1, -1]
    result = -1
    for i in range(100):
        if result != -1:
            break
        d = 0
        ni = 0
        nj = i
        if arr[0][i] == 1:
            while ni < 99 and 0 <= nj < 100:
                ni += di[d]
                nj += dj[d]
                if d == 0:
                    if nj + 1 < 100 and arr[ni][nj+1] == 1:
                        d = 1
                    elif nj - 1 >= 0 and arr[ni][nj-1] == 1:
                        d = 2
                else:
                    if nj < 0 or nj >= 100 or arr[ni][nj] == 0:
                        ni -= di[d]
                        nj -= dj[d]
                        d = 0
                if arr[ni][nj] == 2:
                    result = i
                    break

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

 

⭕ 해설

  1.  시작점이  1일 때마다 도착지를 찾습니다.
  2.  좌, 우를 계속 확인하면서 아래로 내려갑니다.
  3.  만약 좌, 우 중 하나가 있다면 방향을 바꿔줍니다. (십자가 모양은 없기 때문에 순서 상관없이 확인해도 됨)
  4.  좌, 우로 가다가 양쪽이 막혀있거나, 갈 수 없는 길이라면 내려갑니다.
  5.  끝 지점에 가면 while문이 종료 됩니다.
  6.  만약 2를 찾았다면 시작점을 결과에 저장해주고 출력해줍니다.

 

🚨 그냥 끝 지점에서 2를 찾아서 반대로 왔으면 한 번만 돌았으면 됐는데..

 

 

+ Recent posts