解題思路
矩形可旋轉,先把每片的兩邊整理為小邊在前,再對六個二元組排序。長方體設三邊 a≤b≤c,六面排序必為 (a,b) 兩片、(a,c) 兩片、(b,c) 兩片,因此相鄰的 0/1、2/3、4/5 必須各自相同。
配對成功仍不充分,還要檢查第 0 片小邊=第 2 片小邊、第 0 片大邊=第 4 片小邊、第 2 片大邊=第 4 片大邊,才能共同形成 a、b、c。相等邊長或正方形也符合這套條件,不需特判立方體。每六對尺寸為一筆,讀到 EOF,輸出 POSSIBLE 或 IMPOSSIBLE。
Step-by-step animation
示意:2×3、2×4、3×4 各兩片
步驟 1 / 3
排序
六片依小邊、大邊排序。
2×3
2×3
2×4
2×4
3×4
3×4
- 面數
- 6
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int w, h;
while (cin >> w >> h) {
vector<pair<int, int>> a;
for (int i = 0; i < 6; ++i) {
if (i)
cin >> w >> h;
a.push_back({min(w, h), max(w, h)});
}
sort(a.begin(), a.end());
bool ok = a[0] == a[1] && a[2] == a[3] && a[4] == a[5] && a[0].first == a[2].first &&
a[0].second == a[4].first && a[2].second == a[4].second;
cout << (ok ? "POSSIBLE" : "IMPOSSIBLE") << '\n';
}
}先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。