[SWEA] #1211 Ladder2

KwonSC·2021년 11월 6일
0

SWEA - Java

목록 보기
6/26
post-thumbnail

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


Code

import java.util.ArrayList;
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 result = -1;
            int arr[][] = new int[100][100];
            ArrayList<Integer> start = new ArrayList<Integer>();
            for (int i = 0; i < 100; i++) {
                for (int j = 0; j < 100; j++) {
                    int temp = sc.nextInt();
                    arr[i][j] = temp;
                    if (i == 0 && arr[i][j] == 1) {
                        start.add(j);
                    }
                }
            }
            int min = 10000;
            for (Integer st : start) {
                int len = 1;
                int y = st;
                for (int x = 0; x < 100; x++) {
                    len += 1;
                    if (y - 1 >= 0 && arr[x][y - 1] == 1) {
                        while (y - 1 >= 0 && arr[x][y - 1] == 1) {
                            y -= 1;
                            len += 1;
                        }
                    }
                    else if (y + 1 <= 99 && arr[x][y + 1] == 1) {
                        while (y + 1 <= 99 && arr[x][y + 1] == 1) {
                            y += 1;
                            len += 1;
                        }
                    }
                }
                if (len <= min) {
                    min = len;
                    if (result <= st) {
                        result = st;
                    }
                }
            }
            System.out.printf("#%d %d\n", testCase, result);
        }
    }
}

Solution

Start라는 ArrayList를 선언, 배열을 입력받으면서 여러 출발위치 행이 0일때 입력값이 1인값들을 미리 저장
for-each문으로 Start를 순회, Ladder1과 같은 로직으로 행이 99일때까지 내려가면서 len거리++, 마지막에 len을 min 최소값 과 비교하면서 갱신, 만약 최소값이 같을 때에는 가장 큰 열의 좌표를 출력해야하니 열의 좌표 갱신후 출력

0개의 댓글