if the input is 2d array contains 1 and 0 like is
1 0
0 1
1 1
it will always be two numbers for each index
it needs to be changed where the state of each element is opposite to the state of the element in the same row. Moreover, the element on a particular side of a column must be of an opposite state of the same column element before and after it and it has to be done in the least amount of changes to the array
i tried to implement it using brute force but i can't get to work
vector<pair<long long int,long long int>> cloests,temp;
vector<int> movess;
for(int j = 0;j < cloests.size();j++)
{
temp = cloests;
int moves =0 ;
for(int i = 0;i < cloests.size();i++)
{
if(temp[i].first == temp[i].second)
{
if(i != 0)
{
if(temp[i].second == temp[i-1].second)
{
if(temp[i].second == 1)
{
temp[i].second =0;
moves++;
}
else
{
temp[i].second = 1;
moves++;
}
}
}
else
{
if(temp[i].second == 1)
{
temp[i].second =0;
moves++;
}
else
{
temp[i].second = 1;
moves++;
}
}
}
if(i != cloests.size()-1)
if(temp[i].first == temp[i+1].first)
{
if(temp[i+1].first == 1)
{
temp[i+1].first =0;
moves++;
}
else
{
temp[i+1].first = 1;
moves++;
}
}
}
movess.push_back(moves);
}
for(int i = 0 ;i < movess.size();i++)
{
cout << movess[i] << endl;
}
Aucun commentaire:
Enregistrer un commentaire