작성일 :

문제 링크

2477번 - 참외밭

설명

ㄱ자 형태의 육각형 밭이 주어집니다. 이는 큰 직사각형에서 작은 직사각형 하나를 뺀 모양입니다.

둘레를 따라 도는 6개의 방향과 길이가 주어지고, 단위 면적당 참외 개수 K가 주어질 때 전체 참외 수를 구하는 문제입니다.


접근법

밭의 넓이는 큰 직사각형에서 작은 직사각형을 뺀 값입니다.

먼저, 가로 방향 변들 중 가장 긴 것이 큰 직사각형의 가로이고, 세로 방향 변들 중 가장 긴 것이 큰 직사각형의 세로입니다. 각각의 인덱스도 함께 저장합니다.

다음으로, 입력 순서가 육각형을 따라가므로 큰 가로 변과 큰 세로 변이 서로 이웃합니다. 이 두 변 다음에 오는 두 변이 바로 작은 직사각형의 가로와 세로입니다.

이후, 큰 직사각형 넓이에서 작은 직사각형 넓이를 빼고, K를 곱하면 전체 참외 수가 됩니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;

namespace Solution {
  struct Edge { public int dir, len; }

  class Program {
    static void Main(string[] args) {
      var K = int.Parse(Console.ReadLine()!);
      var edges = new List<Edge>(6);

      var maxW = 0;
      var idxW = 0;
      var maxH = 0;
      var idxH = 0;

      for (var i = 0; i < 6; i++) {
        var parts = Console.ReadLine()!.Split();
        var d = int.Parse(parts[0]);
        var l = int.Parse(parts[1]);
        edges.Add(new Edge { dir = d, len = l });

        if (d == 1 || d == 2) {
          if (l > maxW) {
            maxW = l;
            idxW = i;
          }
        } else {
          if (l > maxH) {
            maxH = l;
            idxH = i;
          }
        }
      }

      var pos = idxW;
      if ((idxW + 1) % 6 == idxH || (idxH + 1) % 6 == idxW) {
        if ((idxW + 1) % 6 == idxH)
          pos = idxH;
      }

      var smallW = edges[(pos + 2) % 6].len;
      var smallH = edges[(pos + 3) % 6].len;

      var area = maxW * maxH - smallW * smallH;
      Console.WriteLine(area * K);
    }
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vp;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int K; cin >> K;
  vp e(6);

  int maxW = 0, idxW = 0;
  int maxH = 0, idxH = 0;

  for (int i = 0; i < 6; i++) {
    int d, l; cin >> d >> l;
    e[i] = {d, l};
    if (d == 1 || d == 2) {
      if (l > maxW) {
        maxW = l;
        idxW = i;
      }
    } else {
      if (l > maxH) {
        maxH = l;
        idxH = i;
      }
    }
  }

  int pos = idxW;
  if ((idxW + 1) % 6 == idxH || (idxH + 1) % 6 == idxW) {
    if ((idxW + 1) % 6 == idxH)
      pos = idxH;
  }

  int smallW = e[(pos + 2) % 6].second;
  int smallH = e[(pos + 3) % 6].second;

  int area = maxW * maxH - smallW * smallH;
  cout << area * K << "\n";

  return 0;
}