본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 9298 : Ant Entrapment

반응형

https://www.acmicpc.net/problem/9298

 

9298번: Ant Entrapment

For each case output the line “Case x:” where x is the case number, on a single line, followed by the string “Area” and the area of the fence as a floating-point value and then a comma, followed by a space and then “Perimeter” and the perimeter

www.acmicpc.net

구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

테스트 케이스 t, 좌표의 수 n을 선언하고 입력받습니다. 이 후 for loop를 수행해 입력해줍니다.

📔 풀이과정

x의 최대, 최소 y의 최대 최소 좌표를 입력받을때마다 저장해줍니다.

해당하는 좌표의 최대 최소의 차이가 높이와 너비를 결정하므로 변수 height, width를 선언해 저장해줍니다.

울타리는 직사각형이므로 height * 2 + width * 2입니다.

너비는 height * width가 됩니다.

📔 정답출력

형식에 맞게 저장해줍니다.


📕 Code

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int main(){
    int t, n;
    cin >> t;
    for(int i = 1; i <= t; i++){
        cin >> n;
        double ans, height, width;
        double maxX = -INF, maxY = -INF, minX = INF, minY = INF;

        for(int i = 0; i < n; i++){
            double x,y;
            cin >> x >> y;
            maxX = max(maxX, x);
            maxY = max(maxY, y);
            minX = min(minX, x);
            minY = min(minY, y);
        }

        height = maxY - minY;
        width = maxX - minX;

        printf(
            "Case %d: Area %f, Perimeter %f\n", 
            i, 
            height * width,
            height * 2 + width * 2
        );
    }
}