justgo_developer

[JAVA] 백준 14890 경사로 본문

IT/코딩 문제 풀이

[JAVA] 백준 14890 경사로

다날92 2018. 12. 1. 23:47
728x90
반응형
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.*;
 
public class Main {
    
    static int N;
    static int L;
    static int[][] map;
    static boolean[][] visit;
    static int cnt;
 
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        N = sc.nextInt();
        L = sc.nextInt();
        
        map = new int[101][101];
        visit = new boolean[101][101];
        cnt = 0;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                map[i][j] = sc.nextInt();
            }
        }
        for(int i=0;i<N;i++){
            init();
            x_dfs(0,i);
            init();
            y_dfs(i,0);
        }
        System.out.println(cnt);
    }
    public static void x_dfs(int x, int y){
        if(x == N-1){
            cnt++;
            return;
        }
        
        if(map[x][y] == map[x+1][y]){
            x_dfs(x+1,y);
        }
        else if(map[x+1][y]-1 == map[x][y]){
            if(x-L+1<0)
                return;
            for(int i=x;i>x-L;i--){
                if(visit[i][y]==false){
                    visit[i][y] = true;
                }
                else
                    return;
            }
            x_dfs(x+1,y);
            
        }
        else if(map[x+1][y]+1 == map[x][y]){
            if(x+L>=N)
                return;
            for(int i=x+1;i<x+1+L;i++){
                if(visit[i][y]==false && map[x][y]==map[i][y] +1)
                    visit[i][y] = true;
                else
                    return;
            }
            x_dfs(x+1,y);
        }
    }
    public static void y_dfs(int x, int y){
        if(y == N-1){
            cnt++;
            return;
        }
        
        if(map[x][y] == map[x][y+1]){
            y_dfs(x,y+1);
        }
        else if(map[x][y+1]-1 == map[x][y]){
            if(y-L+1<0)
                return;
            for(int i=y;i>y-L;i--){
                if(visit[x][i]==false){
                    visit[x][i] = true;
                }
                else
                    return;
            }
            y_dfs(x,y+1);
            
        }
        else if(map[x][y+1]+1 == map[x][y]){
            if(y+L>=N)
                return;
            for(int i=y+1;i<y+1+L;i++){
                if(visit[x][i]==false && map[x][y]==map[x][i] +1)
                    visit[x][i] = true;
                else
                    return;
            }
            y_dfs(x,y+1);
        }
    }
    public static void init(){
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                visit[i][j] = false;
            }
        }
    }
 
}
 
cs


728x90
반응형