[BOJ] 11758 : CCW
๋ฌธ์ ๋งํฌ
11758๋ฒ: CCW
์ฒซ์งธ ์ค์ P1์ (x1, y1), ๋์งธ ์ค์ P2์ (x2, y2), ์ ์งธ ์ค์ P3์ (x3, y3)๊ฐ ์ฃผ์ด์ง๋ค. (-10,000 ≤ x1, y1, x2, y2, x3, y3 ≤ 10,000) ๋ชจ๋ ์ขํ๋ ์ ์์ด๋ค. P1, P2, P3์ ์ขํ๋ ์๋ก ๋ค๋ฅด๋ค.
www.acmicpc.net
CCW ๋ฌธ์
ํ์ด
์ฒ์์ ๋๋ CCW๋ผ๋ ์๊ณ ๋ฆฌ์ฆ์ด ์๋ ์ค ๋ชจ๋ฅด๊ณ ์ง์ ๊ธฐ์ธ๊ธฐ์ ๋ฐฉํฅ์ ๊ตฌํด์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ คํ๋ค. ์ด๊ฑด ์๋๊ฒ๊ฐ์์ ๊ตฌ๊ธ๋ง ํ๋ค๊ฐ, ๊ทธ๋ผ ๊ทธ๋ ์ง CCW ์๊ณ ๋ฆฌ์ฆ์ด ์๋ ๊ฑธ ์์๋ค.
CCW๋ Counter Clockwise์ ์ค์๋ง๋ก ์ A,B,C๊ฐ ์ฃผ์ด์ก์ ๋ ์๊ณ๋ฐฉํฅ์ผ๋ก ๋์ฌ์๋์ง, ์ผ์ง์ ์์ ๋์ฌ์๋์ง, ๋ฐ์๊ณ๋ฐฉํฅ์ผ๋ก ๋์ฌ์๋์ง ํ๋ณํด์ฃผ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค.
์ฐธ๊ณ ๋งํฌ
๋๋ ๋ฐ๊ตฌ๋ฆฌ๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ CCW ์๊ณ ๋ฆฌ์ฆ์ ์ดํดํ๋ค.
[์๊ณ ๋ฆฌ์ฆ] CCW๋ก ์ธ ์ ์ ๋ฐฉํฅ์ฑ ํ๋ณํ๊ธฐ (tistory.com)
[์๊ณ ๋ฆฌ์ฆ] CCW๋ก ์ธ ์ ์ ๋ฐฉํฅ์ฑ ํ๋ณํ๊ธฐ
0. ๋ค์ด๊ฐ๊ธฐ ์ ์ ์ฒซ ์๊ณ ๋ฆฌ์ฆ ํฌ์คํธ์ ๋๋ค. ์ด๋ฒ์ ์ธ ๋ด์ฉ์ CCW์ ๋๋ค. ์๋๋ ๊ธฐํ ์๊ณ ๋ฆฌ์ฆ๋ค์ ์ ๋ฐ์ ์ผ๋ก ๋ค๋ฃจ๋ ค๊ณ ํ๋๋ฐ ์๊ฐ๋ณด๋ค ๊ธ์ด ๊ธธ์ด์ ธ์ CCW๋ง ์ฐ๊ฒ ๋์์ต๋๋ค. ๋ณธ ๊ธ์ ๋ด์ฉ
degurii.tistory.com
๊ณต์
๋ ๋ฒกํฐ์ ๋ํด์ ์ธ์ ์ ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค.
u๋ฒกํฐ๋ (m1,m2,m3) ์ด๊ณ v๋ฒกํฐ๋ (n1,n2,n3) ์ธ๋ฐ ํด๋น ๋ฌธ์ ๋ 2์ฐจ์์ด๋ฏ๋ก z์ถ ์ขํ์ธ m3์ n3๋ 0์ด๋ค.
๋ฐ๋ผ์ u x v = (0, 0, m1n2 - m2n1) ์ด๋ผ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค.
u x v ๋ฒกํฐ๋ z์ถ์ผ๋ก์ผ ๋ฐฉํฅ๋ง ์กด์ฌํ๋ฏ๋ก xyํ๋ฉด์ ์์ง์ด๋ผ๊ณ ํ ์ ์๋ค. ๋ฐ๊ตฌ๋ฆฌ๋ ๋ธ๋ก๊ทธ์ ์๋ ์ค๋ฅธ์์ ๋ฒ์น์ ๋ฐ๋ผ์ D = m1n2 - m2n1 ์ด๋ผ๊ณ ํ๋ฉด
1. D > 0 ์ผ ๋๋ ์ธ์ ํ ๋ฒกํฐ๊ฐ xyํ๋ฉด์์ z์ถ์ +๋ฐฉํฅ์ผ๋ก ์์ง(์์ง๊ฐ ์๋ก)์ด๋ฏ๋ก ์ค๋ฅธ์ ๋ฒ์น์ ์ฐ๋ฉด ๋ฐ์๊ณ๋ฐฉํฅ์์ ์ ์ ์๋ค.
2. D = 0 ์ผ ๋๋ ๋ ๋ฒกํฐ๊ฐ ์ผ์ง์ ์์ ์กด์ฌํ๋ค.
3. D < 0 ์ผ ๋๋ ์ธ์ ํ ๋ฒกํฐ๊ฐ xyํ๋ฉด์์ z์ถ์ -๋ฐฉํฅ์ผ๋ก ์์ง(์์ง๊ฐ ์๋๋ก)์ด๋ฏ๋ก ์ค๋ฅธ์ ๋ฒ์น์ ์ฐ๋ฉด ์๊ณ๋ฐฉํฅ์์ ์ ์ ์๋ค.
์ A,B,C ๋ฅผ ๊ฐ๊ฐ A(x1, y1, 0), B(x2, y2, 0), C(x3, y3, 0) ์ด๋ผ๊ณ ํ์ ๋
AB๋ฒกํฐ๋ (x2 - x1, y2 - y1, 0 ) AC๋ฒกํฐ๋ (x3 - x1, y3 - y1, 0 ) ์ด๋ผ๊ณ ํ ์ ์๋ค.
์์ ๊ณต์์ ๋์ ํ๊ธฐ ์ํด์ x2 - x1 = m1, y2 - y1 = m2 , x3 - x1 = n1, y3 - y1 = n2 ๋ผ๊ณ ํ๋ฉด
D = m1n2 - m2n1 = (x2 - x1)(y3 - y1) - (y2 - y1)(x3 - x1) ์ผ๋ก ๋ฐ๊ฟ ์ ์๋ค.
์ A, B, C ์ ๋ํด์ D ๊ฐ์ ๊ตฌํ ๋ค์์ ํ๋ณ์์ ์ ์ฉํ๋ฉด ๋ต์ ๊ตฌํ ์ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
#include <map>
#define mod 998244353
using namespace std;
typedef long long ll;
typedef priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> Priority_queue;
const int INF = INT_MAX;
int CCW(pair<int, int> P1, pair<int, int> P2, pair<int, int> P3) {
return (P2.first - P1.first) * (P3.second - P1.second) - (P3.first - P1.first) * (P2.second - P1.second);
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
pair<int, int> P1;
pair<int, int> P2;
pair<int, int> P3;
cin >> P1.first >> P1.second;
cin >> P2.first >> P2.second;
cin >> P3.first >> P3.second;
int D = CCW(P1, P2, P3);
if (D < 0)
cout << "-1\n";
else if (D == 0)
cout << D << "\n";
else cout << "1\n";
return 0;
}
|
cs |
์ํ์ ์ฝ๋ฉ๊ณผ ์ ๋ชฉํ๋ ค๋๊น ๋ ์ด๋ ค์ด ๊ฒ๊ฐ๋ค. ์ ์ปด๊ณต๊ณผ๊ฐ ์ํ์ ์ํด์ผํ๋ค ํ๋์ง ์ ๊ฒ๊ฐ๋ค.