#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define all(v) (v).begin(),(v).end()
using namespace std;
///////////////////////////////////////////////////////
int n;
struct Point{
int x;
int y;
};
struct Mole{
Point pos;
Point home;
int counter = 0;
};
Mole mole[5];
void rotate(int i){
int tempx = mole[i].pos.x;
int tempy = mole[i].pos.y;
mole[i].pos.x = mole[i].home.x - (tempy - mole[i].home.y);
mole[i].pos.y = mole[i].home.y + (tempx - mole[i].home.x);
++mole[i].counter;
}
int calDist(Point p,Point q){
return (p.x-q.x)*(p.x-q.x) + (p.y-q.y)*(p.y-q.y);
}
bool check(){
vector<int> dis;
dis.pb(calDist(mole[1].pos , mole[2].pos));
dis.pb(calDist(mole[1].pos , mole[3].pos));
dis.pb(calDist(mole[1].pos , mole[4].pos));
dis.pb(calDist(mole[2].pos , mole[3].pos));
dis.pb(calDist(mole[2].pos , mole[4].pos));
dis.pb(calDist(mole[3].pos , mole[4].pos));
sort(all(dis));
if(dis[0]==dis[1] and dis[1]==dis[2] and dis[3]==dis[2] and dis[4]!=dis[3] and dis[4]==dis[5]){
dis.clear();
return true;
}
dis.clear();
return false;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
FOR(t,1,n){
FOR(i,1,4){
cin>>mole[i].pos.x>>mole[i].pos.y>>mole[i].home.x>>mole[i].home.y;
}
int index=0;
int min_moves = INT_MAX;
int moves = 0;
while(index<256){
int curr = 4;
rotate(curr);
++moves;
while(mole[curr].counter==4){
mole[curr].counter = 0;
moves-=3;
--curr;
rotate(curr);
}
if(check()){
min_moves = min(min_moves , moves);
}
++index;
}
if(min_moves!=INT_MAX) cout<<min_moves<<endl;
else cout<<-1<<endl;
// DEBUG(t);
}
return 0;
}
interestingly, resubmitting the exact same code get you to runtime error on test 6. 27469444. I suspect the fault is on the judge and not your code for your first submission. Furthermore, changing vector to array gets WA on test 9. 27469694 I think I will leave the rest of the debugging to you
Thanks for the debugging. Is there any difference when we use vectors instead of arrays?
actually that is not the problem. I don't actually know where the problem lies, but I remember getting the same problem once when I keep clearing vector. That's why I change that.
I added the following code into your rotate function
And got tle on test 1. So you are accessing mole[x] where x < 0 or x > 4, it's undefined behaviour.
Thank you very much. I understood my mistake now.
I encountered this error here :
( n & 1 ) ? cout << string( n / 2 - 1 , 'a' ) : cout << string( n / 2 , 'a' ) ;
Here, when n = 1 my code while try to create string of a's with length -1. So this was the reason for this runtime error in my case. So i handled this edge case before this line of code.