Algorithm/SWEA

[SWEA 4301] 콩 많이 심기(Java)

반응형

 

 

[SWEA 4301] 콩 많이 심기(Java)

 

문제 출처 : 링크

 

SW Expert Academy

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

swexpertacademy.com

규칙을 찾고 미리 배열에 채운다.

 

그리고 입력 받은 N과 M을 통해서 2차원배열의 [0][0] ~ [N-1][M-1] 혹은 [0][0] ~ [M-1][N-1] 중에 큰 수를 저장하여 출력하면 된다.

 

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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution_4301_콩많이심기 {
 
    static int N, M;
    static int[][] map;
 
    public static void main(String[] args) throws Exception {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        int ts = Integer.parseInt(br.readLine().trim());
 
        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[1001][1001];
 
            add(map);
 
            int max = Integer.MIN_VALUE;
 
            if (M == 2 && N == 2) {
                max = 4;
            } else {
                int sum1 = 0;
                for (int i = 0; i < M; i++) {
                    for (int j = 0; j < N; j++) {
                        sum1 += map[i][j];
                    }
                }
 
                int sum2 = 0;
                for (int i = 0; i < N; i++) {
                    for (int j = 0; j < M; j++) {
                        sum2 += map[i][j];
                    }
                }
 
                if (sum1 < sum2)
                    sum1 = sum2;
 
                if (max < sum1)
                    max = sum1;
            }
            
            System.out.println("#" + t + " " + max);
        }
 
    }
 
    public static void add(int[][] map) {
 
        allAdd(00);
 
        for (int i = 3; i < map.length; i += 4) {
            allAdd2(i, 0);
        }
        for (int i = 0; i < map.length; i += 4) {
            allAdd(0, i);
        }
 
    }
 
    public static void allAdd(int y, int x) {
        for (int i = y; i < map.length; i++) {
            if (x < map.length)
                map[i][x] = 1;
            if (x + 1 < map.length) {
                map[i][x + 1= 1;
            }
            x++;
        }
 
    }
 
    public static void allAdd2(int y, int x) {
        // map[y-1][x] = 1;
        for (int i = y; i < map.length; i++) {
            if (x - 1 >= 0 && x - 1 < map.length)
                map[i][x - 1= 1;
            if (x >= 0 && x < map.length) {
                map[i][x] = 1;
            }
            x++;
        }
    }
 
}
cs
반응형