Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef pair<int, int> ii;
- int n;
- int sum, best = 1e9;
- vector<ii> best_state;
- bool get_bit(int x, int k) {
- return (x >> k) & 1;
- }
- void rotate(ii &box) {
- int b = get_bit(box.first, 4);
- box.first <<= 1;
- box.first |= b << 1;
- ++box.second;
- }
- void backtrack(vector<ii> boxes, int i) {
- if(i == n) {
- sum = 0;
- for(auto &x : boxes) {
- sum += !!x.second;
- }
- if(sum < best) {
- best = sum;
- best_state = boxes;
- }
- return;
- }
- for(int k = 1; k < 4; k++) {
- if(get_bit(boxes[i-1].first, 2) == get_bit(boxes[i].first, 4))
- backtrack(boxes, i + 1);
- rotate(boxes[i]);
- }
- }
- int main()
- {
- freopen("in.txt", "r", stdin);
- //freopen("27-11.inp", "r", stdin);
- //freopen("27-11.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- cin >> n;
- vector<ii> a(n);
- for(auto &x : a) {
- for(int i = 1; i <= 4; ++i) {
- int t; cin >> t;
- x.first |= (t << i);
- }
- }
- for(int k = 0; k < 4; ++k) {
- backtrack(a, 1);
- rotate(a[0]);
- }
- // Cau 1:
- cout << best << '\n';
- for(int i = 1; i <= n; i++) {
- if (best_state[i - 1].second) {
- cout << i << ' ';
- }
- }
- cout << '\n';
- // Cau 2:
- for(auto x : best_state)
- cout << get_bit(x.first, 1) << ' ';
- cout << '\n';
- for(auto x : best_state)
- cout << get_bit(x.first, 3) << ' ';
- cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment