알고리즘 문제/BOJ
16937번 두 스티커
parkit
2019. 4. 14. 00:40
728x90
반응형
https://www.acmicpc.net/problem/16937
처음에 배열로 접근하다가 피 봤다.
2차원 배열 인덱스를 활용하기에는 헷갈렸다.
단순 길이로 생각하고 풀었다.
단, chk() 함수에서 2가지 경우가 있다는 것만 생각하면 된다.
아래 그림 참고.
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <iostream> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> #include <map> #include <set> #include <sstream> #include <tuple> #pragma warning(disable:4996) #pragma comment(linker, "/STACK:336777216") using namespace std; int H = 0, W = 0, N = 0, MAX = 0; bool use[101] = { false, }; vector<pair<int, int> > v; bool chk(int y1, int x1, int y2, int x2, int idx) { if (idx == 0) { int my = max(y1, y2); if (my > H || x1 + x2 > W) return false; } else if (idx == 1) { int mx = max(x1, x2); if (mx > W || y1 + y2 > H) return false; } return true; } int area(int y1, int x1, int y2, int x2) { int ret = 0; if (chk(y1, x1, y2, x2, 0)) ret = max(ret, y1*x1 + y2*x2); if (chk(y1, x1, y2, x2, 1)) ret = max(ret, y1*x1 + y2*x2); if (chk(y1, x1, x2, y2, 0)) ret = max(ret, y1*x1 + x2*y2); if (chk(y1, x1, x2, y2, 1)) ret = max(ret, y1*x1 + x2*y2); if (chk(x1, y1, y2, x2, 0)) ret = max(ret, x1*y1 + y2*x2); if (chk(x1, y1, y2, x2, 1)) ret = max(ret, x1*y1 + y2*x2); if (chk(x1, y1, x2, y2, 0)) ret = max(ret, x1*y1 + y2*x2); if (chk(x1, y1, x2, y2, 1)) ret = max(ret, x1*y1 + y2*x2); return ret; } void simulation(vector<pair<int, int> > vc, int pos) { if (vc.size() == 2) { int var = area(vc.at(0).first, vc.at(0).second, vc.at(1).first, vc.at(1).second); if (var == 0) return; MAX = max(MAX, var); return; } for (int i = pos; i < v.size(); i++) if (!use[i]) { use[i] = true; vc.push_back(v.at(i)); simulation(vc, i); use[i] = false; vc.pop_back(); } } int main(void) { int R = 0, C = 0; scanf("%d %d %d", &H, &W, &N); for (int i = 0; i < N; i++) { scanf("%d %d", &R, &C); v.push_back({ R, C }); } vector<pair<int, int> > vc; simulation(vc, 0); printf("%d\n", MAX); return 0; } | cs |
728x90
반응형