본문 바로가기

알고리즘

홈방범서비스[swexpertacademy]

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

서비스 범위

 

걸렸던 부분: 서비스의 범위를 잘못 이해했다.

 

 

 

 

 

 

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
package academymoi;
 
import java.util.Arrays;
import java.util.Scanner;
 
public class 홈방범서비스 {
    public static int[] cost = new int[50];
    public static int t, n, m;
    public static int[][] map;
    public static int maxCnt = 0;
 
    public static void main(String[] args) {
        makeCost();
        Scanner scan = new Scanner(System.in);
        t = scan.nextInt();
        for (int i = 1; i <= t; i++) {
            input(scan);
            securityService();
            System.out.println("#" + i + " " + maxCnt);
            maxCnt = 0;
        }
 
        scan.close();
    }
 
    private static void securityService() {
        for (int a = 0; a < n; a++) {
            for (int b = 0; b < n; b++) {
                for (int k = 1; k <= 2 * n + 5; k++) {
 
                    int cnt = areaCnt(a, b, k);
                    int price = m * cnt - cost[k];
//                    if(price>=0)System.out.println("price:"+price+" a:"+a+" b:"+b+" range:"+k+" cnt:"+cnt);
                    if (price >= 0) {
                        if (maxCnt < cnt)
                            maxCnt = cnt;
                    }
 
                }
            }
        }
    }
 
    private static int areaCnt(int x, int y, int range) {
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (abs(x - i) + abs(y - j) < range) {
                    if (map[i][j] == 1)
                        cnt += 1;
                }
            }
        }
        return cnt;
    }
 
    private static void input(Scanner scan) {
        n = scan.nextInt();
        m = 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();
            }
        }
    }
 
    private static int abs(int x) {
        if (x > 0)
            return x;
        else
            return -x;
    }
 
    private static void makeCost() {
        cost[1= 1;
        for (int i = 2; i < 50; i++) {
            cost[i] = i * i + (i - 1* (i - 1);
        }
    }
}
 
 
cs

 

 

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

보물상자 비밀번호[swexpert academy]  (0) 2019.10.01
활주로건설[swexpert academy]  (0) 2019.10.01
디저트카페[swexpert academy]  (0) 2019.10.01
탈주범 검거[swexpertacademy]  (0) 2019.09.30
미생물 격리[swexpertacademy]  (0) 2019.09.30