본문 바로가기

Algorithm/Brute Force

(C++) - 백준(BOJ) 8295 : Rectangles

반응형

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

 

8295번: Rectangles

The first and only line of the standard input contains three integers: n, m and p (1 ≤ n, m ≤ 100, 4 ≤ p ≤ 2(n+m)), representing the dimensions of the grid and the lower bound for the perimeter of the rectangles.

www.acmicpc.net

모든 경우의 수를 탐색하는 brute force 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

세로의 크기 n, 가로의 크기 m, 최소 만족 둘레의 길이 p, 정답을 출력할 변수 ans를 선언한 후 적절히 입력받습니다.

📔 풀이과정

1. 모든 형태의 직사각형에 대해 즉, 가로와 세로에 대해 2중 for loop를 수행하며 둘레의 길이가 p이상인지 확인합니다.

2. p이상이라면 그 때 직사각형 개수를 구해 ans에 누적하여 더해줍니다.

📔 정답출력

ans를 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;
int n, m, p, ans;
int main(){
  cin >> n >> m >> p;
  for(int height = 1; height <= n; height++){
    for(int width = 1; width <= m; width++){
      int perimeter = (height + width) * 2;
      if(perimeter < p) continue;
      ans += (n - height + 1) * (m - width + 1);
    }
  }
  cout << ans;
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.