본문 바로가기

알고리즘

활주로건설[swexpert academy]

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeW7FakkUDFAVH

 

헤매서 참고자료 열람하고 품. (시간 너무 많이 소요) 엊그제?

 

 

 

2번째때 풀때 큰 실수를 함. (16:04 ~ 17:48)  -> 이 문제 더 단축 시켜야

같은값 2 2 2 2 2 2 2

체크를 합으로 체크를 함

1 3 2 2 2 2 2 이것도 합이 같다. 

빈도수로 체크하는게 가장 안전하다.

 

 

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.util.Arrays;
import java.util.Scanner;
 
public class Solution {
    public static int t,n,x;
    public static int[][] map;
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        t = scan.nextInt();
        for(int i=1;i<=t;i++
        {
            input(scan);
            int x = buildRoad();
            System.out.println("#"+i+" "+x);
        }
        
        
        scan.close();
    }
    private static int abs(int x) {if(x>0)return x;else return -x;}
    
    private static int buildRoad() {
        int cnt=0;
        for(int i=0;i<n;i++
        {
            int[] height = new int[n];
            for(int j=0;j<n;j++) {height[j] = map[i][j];}
            
            if(verify(height)) 
            {
                cnt+=1;
            }
        }
        
        for(int i=0;i<n;i++
        {
            int[] height = new int[n];
            for(int j=0;j<n;j++) {height[j] = map[j][i];}
            if(verify(height)) 
            {
                cnt+=1;
            }
        }
        return cnt;
    }
    private static boolean verify(int[] height) {
        
        int std = height[0];
        int stdCnt=0;
        for(int i=0;i<n;i++
        {
            if(std==height[i])stdCnt+=1;
        }
        if(stdCnt==n)return true;
        
        
        for(int i=0;i<n-1;i++
        {
            if(abs(height[i]-height[i+1])>=2) {return false;}
        }
        
        
        boolean[] up = new boolean[n];
        boolean[] down = new boolean[n];
        for(int i=0;i<n-1;i++
        {
            if(height[i]+1==height[i+1]) 
            {
                int s = i-x+1;
                int e = i;
                if(!isFlat(height,s,e)) {return false;}
                else 
                {
                    for (int t = s; t <= e; t++) {
                        up[t] = true;
 
                    }
                }
            }
            if(height[i]==height[i+1]+1
            {
                int s = i+1;
                int e = i+x;
                if(!isFlat(height,s,e)) {return false;}
                else 
                {
                    for(int k=s;k<=e;k++)down[k]=true;
                }
            }
        }
 
        if(check(up,down)) 
        {
            return true;
        }
        else 
        {
            return false;
        }
        
    }
    
    
    private static boolean check(boolean[] up, boolean[] down) {
        
        for (int i = 0; i < n; i++) {
            if (up[i] == true && down[i] == true)
                return false;
        }
        
        return true;
    }
    private static boolean isFlat(int[] height,int s, int e) {
        
        if(s<0 || e>=n) 
        {
            return false;
        }
        
        int std=height[s];
        for(int i=s;i<=e;i++
        {
            if(std!=height[i])return false;
        }
        return true;
    }
    
    private static void input(Scanner scan) {
        n = scan.nextInt();x = scan.nextInt();
        map = new int[n][n];
        for(int j=0;j<n;j++
        {
            for(int k=0;k<n;k++
            {
                map[j][k] = scan.nextInt();
            }
        }
    }
}
 
 
cs

 

 

 

 

 

 

'알고리즘' 카테고리의 다른 글

벽돌 깨기[swexpert academy]  (0) 2019.10.02
보물상자 비밀번호[swexpert academy]  (0) 2019.10.01
홈방범서비스[swexpertacademy]  (0) 2019.10.01
디저트카페[swexpert academy]  (0) 2019.10.01
탈주범 검거[swexpertacademy]  (0) 2019.09.30