본문 바로가기

■ 알고리즘 문제 풀이/SWEA

[SWEA-D4] 1211. Ladder2

▶문제설명

[SWEA] 1211. Ladder2

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



▶Solution


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
 
class Solution{
    public static int map[][] = new int[100][100];
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = 0;
        int answer = -1;
        
        for (int t = 0; t < 10; t++) {
            T = sc.nextInt();
            int[] PathLengthArr = new int[100];
            
            for (int i = 0; i < 100; i++)
                for (int j = 0; j < 100; j++)
                    map[i][j] = sc.nextInt();
 
            for (int i = 0; i < 100; i++) {
                // 출발점을 찾으면 알고리즘 수행 후 목적지 도착하면 브레이크
                if (map[0][i] == 1) { // 출발점인 경우
                    PathLengthArr[i] = FindTarget(0, i);
                }
            }
             int min=999999;
                int maxIdx = -1;
            for(int i = 0 ; i<100 ; i++){
                int pathlength = PathLengthArr[i];
                if(pathlength == 0continue;
                if ( pathlength <= min){
                    min = pathlength;
                    maxIdx = i;
                }
            }
            answer = maxIdx;
            System.out.printf("#%d %d\n", T, answer);
        }
      
    }
 
    public static int FindTarget(int i, int j) {
        boolean[][] visited = new boolean[100][100];
        int PathLength = 0;
        
        while (i<100) {
            visited[i][j] = true;
            PathLength++;
            //System.out.println(i + " : " + j);
            if (map[i][j] == 2) {
                break;
            }
            if (0 <= j - 1 && map[i][j - 1== 1 && visited[i][j - 1== false) { // 왼쪽이 뚫린 경우
                j--;
            } 
            else if (j + 1 < 100 && map[i][j + 1== 1 && visited[i][j + 1== false) { // 오른쪽이 뚫린 경우
                j++;
            } 
            else { // 아래가 뚫린 경우
                i++;
            }
        }
        return PathLength;
    }
}
cs


'■ 알고리즘 문제 풀이 > SWEA' 카테고리의 다른 글

[SWEA] 5653. 줄기세포배양  (0) 2019.02.26
[SWEA-D3] 1213. String  (0) 2019.02.20
[SWEA-D4] 1210. Ladder1  (0) 2019.02.20
[SWEA-D3] 1209. Sum  (0) 2019.02.20
[SWEA-D3] 1206. View  (0) 2019.02.20