Algorithm/SWEA

[SWEA 2117] 홈 방범 서비스 (Java)

반응형

 

[SWEA 2117] 홈 방범 서비스 (Java) 

 

문제 출처 : 링크

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

소프트웨어 모의 테스트 문제다.

시뮬레이션 문제로, 마름모를 구현할 수만 있으면 잘 풀 수 있다. (잘 생각이 안나서 더럽게 구현했음ㅜㅜ)

이익이 0 이상일 때를 체크해서 가장 많은 집의 수를 출력하면 된다.

 

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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution_2117_홈방범서비스 {
 
    
    static int N,M;
    static int[][] map;
    static int sum;
    static int resultSum;
    static int oneCnt = 0;
    
    
    public static void main(String[] args) throws Exception {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        int ts = Integer.parseInt(br.readLine());
        
        for (int t = 1; t <= ts; t++) {
            
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            
            N = Integer.parseInt(st.nextToken());
            M = Integer.parseInt(st.nextToken());
            
            map = new int[N][N];
            
            for (int i = 0; i < map.length; i++) {
                st = new StringTokenizer(br.readLine(), " ");
                for (int j = 0; j < map[i].length; j++) {
                    map[i][j] = Integer.parseInt(st.nextToken());
                
                }
            }
            
            resultSum = 0;
            
            for (int yy = 0; yy < map.length; yy++) {
                
                for (int xx = 0; xx < map.length; xx++) {
                    
                    for (int j = 1; j <= N+1; j++) {
                        sum = 0;
                        checking(yy,xx,j);
                        int res = sum*- ((j*j)+(j-1)*(j-1)); // 이익 계산
                        if(res >= 0) { // 이익이 마이너스가 아닐때
                            if(resultSum < sum) { // 최대  sum값 저장
                                resultSum = sum;
                            }
                        }
                    }
                }
            }
            System.out.println("#"+t+" " + resultSum);
            
        }
        
    }
    
    
    //마름모 구역 합 체크
    public static void checking(int y, int x, int k) {
        int start = x-k+1;
        int end = x+k-1;
        
        
        int first = 0;
        int fs = start;
        int fe = end;
        int firstY = y;
        
        //가운데부터 위
        while(first <= k) {
            
            for (int i = fs; i <= fe; i++) {
                if(firstY>=0 && firstY<&& i>=0 && i<N) {
                    sum += map[firstY][i];
                }
                else continue;
            }
            
            fs++;
            fe--;
            first++;
            firstY--;
        }
        
        // 가운데에서 아래
        int second = 0;
        int ss = start+1;
        int se = end-1;
        int secondY = y+1;
        
        while(second <= k-1) {
            for (int i = ss; i <= se; i++) {
                if(secondY>=0 && secondY<&& i>=0 && i<N) {
                    sum += map[secondY][i];
                }
                else continue;
            }
            
            ss++;
            se--;
            second++;
            secondY++;
        }
        
    }
 
}
cs
반응형