Post

[BaekJoon] #3009 - 네 번째 점 [Java][C++]

[BaekJoon] #3009 - 네 번째 점 [Java][C++]

문제 링크


1. 아이디어

세 점이 주어졌을 때, 직사각형을 만들 수 있는 남은 한 점의 좌표를 구하는 문제로 간단하게는 각 좌표가 두 번씩 등장해야 한다는 점을 활용한 조건문으로 해결할 수 있다.

다른 방법은 비트 XOR 연산자를 활용하면 되는데 비트 XOR 연산자는 두 비트가 다르면 1, 같으면 0을 반환한다. 따라서 $x$, $y$ 좌표 각각 같은 좌표 2개와 다른 좌표 1개로 이루어졌는데, 세 점의 각 좌표를 전부 XOR 연산하면 같은 두 좌표가 상쇄되어 0이 되고 0과 자기 자신과의 XOR 연산은 자기 자신이 나와서 남은 좌표를 구할 수 있다.


2. 복잡도

접근시간공간
좌표 비교$O(1)$$O(1)$
XOR 상쇄$O(1)$$O(1)$

3. 코드

풀이 1: 좌표 비교 [Java][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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;

        st = new StringTokenizer(br.readLine());
        int x1 = Integer.parseInt(st.nextToken());
        int y1 = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        int x2 = Integer.parseInt(st.nextToken());
        int y2 = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        int x3 = Integer.parseInt(st.nextToken());
        int y3 = Integer.parseInt(st.nextToken());

        int x4;
        if (x1 == x2) {
            x4 = x3;
        } else if (x2 == x3) {
            x4 = x1;
        } else {
            x4 = x2;
        }

        int y4;
        if (y1 == y2) {
            y4 = y3;
        } else if (y2 == y3) {
            y4 = y1;
        } else {
            y4 = y2;
        }

        System.out.println(x4 + " " + y4);
    }
}
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
#include <bits/stdc++.h>
using namespace std;

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

    int x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

    int x4;
    if (x1 == x2) {
        x4 = x3;
    } else if (x2 == x3) {
        x4 = x1;
    } else {
        x4 = x2;
    }

    int y4;
    if (y1 == y2) {
        y4 = y3;
    } else if (y2 == y3) {
        y4 = y1;
    } else {
        y4 = y2;
    }

    cout << x4 << ' ' << y4;
}

풀이 2: XOR 상쇄 [Java][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
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;

        st = new StringTokenizer(br.readLine());
        int x1 = Integer.parseInt(st.nextToken());
        int y1 = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        int x2 = Integer.parseInt(st.nextToken());
        int y2 = Integer.parseInt(st.nextToken());

        st = new StringTokenizer(br.readLine());
        int x3 = Integer.parseInt(st.nextToken());
        int y3 = Integer.parseInt(st.nextToken());

        int x4 = x1 ^ x2 ^ x3;
        int y4 = y1 ^ y2 ^ y3;
        System.out.println(x4 + " " + y4);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
using namespace std;

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

    int x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

    int x4 = x1 ^ x2 ^ x3;
    int y4 = y1 ^ y2 ^ y3;
    cout << x4 << ' ' << y4;
}

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