Clean up the room--niuke.com--interview questions

Title description

It was the weekend again, and Xiao Yi's room was in a mess.

He hopes to sort out the clutter on the ground a little so that each clump of clutter looks more compact and less messy.

There are n clumps of debris on the ground, and each clump of debris contains 4 items. The coordinates of the i-th item are represented by (ai, bi), and Xiao Yi can rotate it counterclockwise around (xi, yi) by 90^ \circ90∘ every time, which will consume his movement times. If 4 points of a clump of debris form a square with an area other than 0, we say it is compact.

Because Xiao Yi is lazy, he hopes that you can help him calculate the minimum number of steps required for each clump of debris to make it compact.

Enter description:

A number n (1 <= n <= 100) in the first line indicates the number of clumps of debris. 
The next 4n lines, each 4 lines represent a clump of debris, each line has 4 numbers ai, bi, xi, yi, (-104 <= xi, yi, ai, bi <= 104), which means the i-th item rotates Its own coordinates and center point coordinates.

Output description:

N rows, 1 number per row, indicating the minimum number of moves.

Example 1

enter

4
1 1 0 0
-1 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-2 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-1 1 0 0
-1 1 0 0
-1 1 0 0
2 2 0 1
-1 0 0 -2
3 0 0 -2
-1 1 -2 0

Output

1 -1 3 3

Description

For the first group of debris, we can rotate the second or third object once.

Reference Code:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <limits.h>
#include <cstdio>
using namespace std;
 
struct Item{
    int a, b, x, y, state;
    Item(){
        state = 0;
    }
 
    void crot(){
        state = (state + 1) % 4;
        int dx = a-x, dy = b-y;
        a = x - dy;
        b = y + dx;
    }
 
    void input(){
        cin>>a>>b>>x>>y;
        state = 0;
    }
 
    bool operator ==(const Item &item2){
        return a==item2.a && b==item2.b;
    }
 
    Item operator +(const Item &it2){
        Item res;
        res.a = a + it2.a;
        res.b = b + it2.b;
        return res;
    }
 
    Item operator -(const Item &it2){
        Item res;
        res.a = a - it2.a;
        res.b = b - it2.b;
        return res;
    }
 
    static bool ortho(const Item &it1, const Item &it2){
        if(it1.a==0 && it1.b== 0) return 0;
        if(it2.a==0 && it2.b == 0) return 0;
        return it1.a * it2.a + it1.b * it2.b == 0;
    }
};
 
struct Pack{
    vector<Item> itemList;
    vector<Item*> itp;
    int step;
 
    Pack(){
        itemList = vector<Item>(4);
        itp = vector<Item*>(4, nullptr);
        for(int i=0; i<4; ++i) itp[i] = &itemList[i];
        step = INT_MAX;
    }
 
    void input(){
        for(int i=0; i<4;++i)
            itemList[i].input();
        step = INT_MAX;
    }
 
    bool isSqaure(){
        for(int i=1; i<4; ++i){
            if(i!=1) swap(itp[i], itp[1]);
            if(*itp[0]==*itp[1] || *itp[2]==*itp[3]) return 0;
            if(!(*itp[0] + *itp[1] == *itp[2] + *itp[3])) continue;
            if(!Item::ortho(*itp[0]- *itp[1], *itp[2] - *itp[3])) continue;
            if(Item::ortho(*itp[0]- *itp[2], *itp[0] - *itp[3])) return 1;
        }
        return 0;
    }
 
    void trySqaure(int rot_idx){
        for(int i=0; i<4; ++i){
            if(rot_idx == 0 && isSqaure()){
                int tmp_step = 0;
                for(int j=0; j<4; ++j) tmp_step += itemList[j].state;
                if(step > tmp_step) step = tmp_step;
            }
            if(rot_idx > 0) trySqaure(rot_idx - 1);
            itemList[rot_idx].crot();
        }
    }
};
 
int main()
{
    int n;
    cin>>n;
    Pack eRoom;
    for(int i=0; i<n; ++i){
        eRoom.input();
        eRoom.trySqaure(3);
        cout<<(eRoom.step > 16 ? -1: eRoom.step)<<endl;
    }
}

 

Guess you like

Origin blog.csdn.net/qq_40513792/article/details/104287903