본문 바로가기

백준

2527 직사각형

 

직사각형을 만드는 경우가 x, y가 다 겹치면 직사각형이고 둘 중 하나가 1이면 선분이 겹친다고 생각했다. 

 

그래서 아래와 같이 코딩했는데 선분인지 확인하는 부분이 문제인 것 같다. 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()
 
using namespace std;
using i64 = long long;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;



void solve()
{
    ii a1, a2, b1, b2;
    cin >> a1.xx >> a1.yy >> a2.xx >> a2.yy;
    cin >> b1.xx >> b1.yy >> b2.xx >> b2.yy;
    
    bool x = false, y = false;
    
    if (max(a1.xx, b1.xx) < min(a2.xx, b2.xx))
        x = true;
    if (max(a1.yy, b1.yy) < min(a2.yy, b2.yy))
        y = true;
    
    if (((a1.xx == b2.xx) && (a1.yy == b2.yy)) || ((a2.xx == b1.xx) && (a1.yy == b2.yy)) || ((a1.xx == b2.xx) && (a2.yy == b1.yy)) || ((a2.xx == b1.xx) && (a2.yy == b1.yy)))
        printf("c\n");
    else if (x && y)
        printf("a\n");
    else if (x ^ y)
        printf("b\n");
    else
        printf("d\n");
}

int main() {
    
    for (int i = 0; i < 4; i++)
        solve();
    
    return 0;
}

 

그래서 고쳤다!

 

 

이 부분이 문제여서 조건을 더 추가했다. 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define xx first
#define yy second
#define all(x) (x).begin(), (x).end()
 
using namespace std;
using i64 = long long;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;



void solve()
{
    ii a1, a2, b1, b2;
    cin >> a1.xx >> a1.yy >> a2.xx >> a2.yy;
    cin >> b1.xx >> b1.yy >> b2.xx >> b2.yy;
    
    bool x = false, y = false;
    
    if (max(a1.xx, b1.xx) < min(a2.xx, b2.xx))
        x = true;
    if (max(a1.yy, b1.yy) < min(a2.yy, b2.yy))
        y = true;
    
    if (((a1.xx == b2.xx) && (a1.yy == b2.yy)) || ((a2.xx == b1.xx) && (a1.yy == b2.yy)) || ((a1.xx == b2.xx) && (a2.yy == b1.yy)) || ((a2.xx == b1.xx) && (a2.yy == b1.yy)))
        printf("c\n");
    else if (x && y)
        printf("a\n");
    else if ((y && ((a2.xx == b1.xx) || (a1.xx == b2.xx))) || (x && ((b1.yy == a2.yy) || (a1.yy == b2.yy))))
        printf("b\n");
    else
        printf("d\n");
}

int main() {
    
    for (int i = 0; i < 4; i++)
        solve();
    
    return 0;
}

 

맞았음 휴

 

if (max(a1.xx, b1.xx) < min(a2.xx, b2.xx))

 

참고로 겹치는 부분은 이렇게 쓸 수 있다. 

 

'백준' 카테고리의 다른 글

6416 트리인가?  (0) 2020.08.08
14437 준오는 심술쟁이!!  (0) 2020.08.07
2635 수 이어가기  (0) 2020.08.06
5636 소수 부분 문자열  (0) 2020.08.04
15624 피보나치 수 7  (0) 2020.08.04