Post

[백준] 9063번 - 대지 [Java][C++]

[백준] 9063번 - 대지 [Java][C++]

문제 링크


1. 문제 풀이

주어진 좌표들을 모두 포함하는 가장 작은 직사각형의 넓이를 구하는 문제다. 주어진 좌표들을 모두 포함하려면 좌표들의 $x$, $y$ 좌표의 최솟값과 최댓값에 해당하는 위치에 직사각형의 각 변을 두면 된다. 이를 위해 각 변의 위치 좌표에 대한 변수를 선언하고 각각 최솟값, 최댓값으로 갱신해줬다.


2. 코드

1. 구현 [Java]

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
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int minX = 10000;
        int minY = 10000;
        int maxX = -10000;
        int maxY = -10000;

        int N = Integer.parseInt(br.readLine());
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());

            minX = Math.min(minX, x);
            minY = Math.min(minY, y);
            maxX = Math.max(maxX, x);
            maxY = Math.max(maxY, y);
        }

        System.out.println((maxX - minX) * (maxY - minY));
    }
}

2. 구현 [C++]

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
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int mn_x = 10000;
    int mn_y = 10000;
    int mx_x = -10000;
    int mx_y = -10000;

    int n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;

        mn_x = min(mn_x, x);
        mn_y = min(mn_y, y);
        mx_x = max(mx_x, x);
        mx_y = max(mx_y, y);
    }

    cout << (mx_x - mn_x) * (mx_y - mn_y);
}

3. 풀이 정보

1. 구현 [Java]

언어시간메모리코드 길이
Java 11256 ms27640 KB820 B

2. 구현 [C++]

언어시간메모리코드 길이
C++ 178 ms2020 KB477 B

This post is licensed under CC BY 4.0 by the author.