Notice
Recent Posts
Recent Comments
Link
할껀하고놀자
[SWEA] 6853번 직사각형과 점 본문
728x90
#include<iostream>
#include<algorithm>
#include<tuple>
#include<vector>
using namespace std;
int main() {
int tc;
cin >> tc;
for (int t = 0; t < tc; t++) {
int x1, y1, x2, y2, N;
cin >> x1 >> y1 >> x2 >> y2 >> N;
vector<pair<int, int>> v;
for (int i = 0; i < N; i++) {
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
}
sort(v.begin(), v.end());
int out, line, in;
tie(out, line, in) = make_tuple(0, 0, 0);
for (int i = 0; i < v.size(); i++) {
if (v[i].first<x1 || v[i].first>x2) { // 이러면 y가 어딜가도 범위 밖이다.
out++;
}
else if (x1 == v[i].first || v[i].first == x2) { // 세로점에 있는 경우.
if (y1 <= v[i].second && v[i].second <= y2) {
line++;
}
else {
out++;
}
}
else if (x1 < v[i].first && v[i].first < x2) { // 내부에 있는 경우
if (v[i].second == y1 || v[i].second == y2) {
line++;
}
else if(y1<=v[i].second && v[i].second<=y2){
in++;
}
else {
out++;
}
}
}
cout << "#" << (t + 1) << " " << in << " " << line << " " << out << endl;
}
return 0;
}
포인트 : x 값을 기준으로 세가지로 분류하였음. 외부, 선 상, 선 내부. 그 후 y 값을 기준으로 3단계로 분류하였다.
이건 처음 짠 코드인데 오답이다. 세로로 같은 경우에서 세부 분류를 하지 못하여 오답이 나왔다. 세로로 같은 경우에도 out이 있을 수 있다는 것을 알았음. 이어 x가 범위 내부에 있는것도 out이 나올 수 있다는 것도 알았다.
#include<iostream>
#include<algorithm>
#include<tuple>
#include<vector>
using namespace std;
int main() {
int tc;
cin >> tc;
for (int t = 0; t < tc; t++) {
int x1, y1, x2, y2,N;
cin >> x1 >> y1 >> x2 >> y2 >> N;
vector<pair<int, int>> v;
for (int i = 0; i < N; i++) {
int x, y;
cin >> x >> y;
v.push_back(make_pair(x, y));
}
sort(v.begin(), v.end());
int out, line, in;
tie(out, line, in) = make_tuple(0, 0, 0);
for (int i = 0; i < v.size(); i++) {
if (v[i].first<x1 || v[i].first>x2) { // 이러면 y가 어딜가도 범위 밖이다.
out++;
}
else if (x1==v[i].first || v[i].first == x2) { // 세로로 같은 경우 선 안에 있다.
line++;
}
else if (x1 < v[i].first && v[i].first < x2) {
if (v[i].second == y1 || v[i].second == y2) { // 가로로 같은 경우 선 안에 있다.
line++;
}
else { // 아니면 선 안에 존재한다.
in++;
}
}
}
cout << "#" << (t + 1) << " " << in << " " << line << " " << out << endl;
}
return 0;
}
'[IT] > SWEA' 카테고리의 다른 글
[SWEA] 7272번 안경이 없어! (0) | 2019.06.03 |
---|
Comments