[SWEA] #1210 Ladder1

KwonSC·2021년 11월 6일
0

SWEA - Java

목록 보기
5/26
post-thumbnail

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14ABYKADACFAYh&categoryId=AV14ABYKADACFAYh&categoryType=CODE


Code

import java.util.Scanner;

class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        for (int testCase = 1; testCase <= 10; testCase++) {
            int z = sc.nextInt();
            int arr[][] = new int[100][100];
            int y = 0;
            for (int i = 0; i < 100; i++) {
                for (int j = 0; j < 100; j++) {
                    int temp = sc.nextInt();
                    arr[i][j] = temp;
                    if (i == 99 && temp == 2) {
                        y = j;
                    }
                }
            }
            for (int x = 99; x >= 0; x--) {
                if (y - 1 >= 0 && arr[x][y - 1] == 1) {
                    while (y - 1 >= 0 && arr[x][y - 1] == 1) {
                        y -= 1;
                    }
                }
                else if (y + 1 <= 99 && arr[x][y + 1] == 1) {
                    while (y + 1 <= 99 && arr[x][y + 1] == 1) {
                        y += 1;
                    }
                }
            }
            System.out.printf("#%d %d\n", testCase, y);
        }
    }
}

Solution

배열을 입력받고 만약 마지막 행의 값이 2 도착지점 라면 y값에 열 위치 저장
x 가 99부터 0이상일때까지 x-- 반복 -> 도착지점부터 출발지점으로 올라감
y 는 좌우를 살피며 옆에 갈수있는 길이있다면 옆으로 빠져 갈수있는 만큼 옆으로 감
마지막에 y의 위치 출력

0개의 댓글