2차원 배열을 검사할 때 1차원으로 생각해줬고,

경사로를 놓을 수 없는 경우 

  • 경사로를 놓은 곳에 또 경사로를 놓는 경우 -- visited 로 해결
  • 낮은 칸과 높은 칸의 높이 차이가 1이 아닌 경우 -- Math.abs(height[i] - height[i+1]) >1 로 해결
  • 낮은 지점의 칸의 높이가 모두 같지 않거나, L개가 연속되지 않은 경우 -- 56,66 번째 for 문 범위설정 중요 
  • 경사로를 놓다가 범위를 벗어나는 경우 -- 57 , 66 번째 if문안에서 처리

 

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
public class Main148890_경사로 {
    private static int N;
    private static int L;
    private static int[][] map;
 
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        N = Integer.parseInt(st.nextToken());
        L = Integer.parseInt(st.nextToken());
 
        map = new int[N][N];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine(), " ");
            for (int j = 0; j < map.length; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
            }
        }
 
        int result = 0;
        boolean flag = false;
        for (int i = 0; i < N; i++) {
            if (check(i, 0false))
                result++// 행 부터 검사
            if (check(0, i, true))
                result++// 열 검사
        }
        System.out.println(result);
    }// end of main
 
    public static boolean check(int x, int y, boolean flag) {
        int[] height = new int[N]; // 1차원으로 만든 후 ㅓㅁ사
        boolean[] visited = new boolean[N]; // 경사로 사용 여부
        for (int i = 0; i < N; i++) {
            if (!flag) {
                height[i] = map[x][i];
            } else {
                height[i] = map[i][y];
            }
        }
 
        for (int i = 0; i < height.length - 1; i++) { // 경사로를 놓을 수 없는 경우
            if (height[i] == height[i + 1])
                continue;
            
            if (Math.abs(height[i] - height[i + 1]) > 1)
                return false// 높이가 2경우 차이날떄
            
            if (Math.abs(height[i] - height[i + 1]) == 1) { // 이제 사다리를 놓을 수 있는지 없는지 판단
                if (height[i] - 1 == height[i + 1]) { // 앞에가 높으니깐 앞에 이후로 L(경사로 길이)만큼 놓을 수 있는지 판단
                    for (int j = i + 1; j <= i + L; j++) {
                        if (j >= N || visited[j] || height[i + 1!= height[j]) {
                            return false;
                        } else {
                            visited[j] = true;
 
                        }
                    }
                } else { // 뒤에가 높으니깐 뒤 앞으로 L 만큼 놓을 수 있는지 판단
                    for (int j = i; j >= i-L+1; j--) {
                        if(j <0 || visited[j] || height[i] != height[j]) {
                            return false;
                        }else {
                            visited[j] = true;
                        }
                    }
                }
            }
 
            
 
        }
 
        return true;
 
    }
}// end of class
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

+ Recent posts