Code may be a little twist, absolutely original!
description
There is a special binary combination lock button connected by n composition (n <30), the button has a concave / convex two states, by hand button changes its state.
However, a headache is that when two button states when you press a button, it is saying the reverse is also adjacent. Of course, if you press the left or the most right-most button, which will only affect adjacent to a button saying.
The current lock state is known, need to solve the problem is, you need at least according to how many times a button in order to lock into a desired target state.
Entry
Two lines, given by the two other long string of 1s and 0s that represents the current / target lock state, where 0 represents concave, convex represents.
Export
At least the number of operations carried out by pressing the button, if you can not achieve change, the output impossible.
Sample input
011 000
Sample Output
1
#include <iostream>
#include <string>
using namespace std;
int PressCount(string passwndold,string passwndnew)
{
if(passwndold==passwndnew) return 0;
if(passwndnew.length()!=passwndold.length()) return -1;
int presscount=0,k=0;
for(int i=0;i<passwndold.length();i++)
{
if(passwndold[i]==passwndnew[i])
{
continue;
}
else
{
if(i==(passwndold.length()-1))
{
presscount=-1;
return presscount;
}
else if(i==(passwndold.length()-2))
{
if(passwndold[i+1]!=passwndnew[i+1])
{
presscount++;
}
else if(passwndold[i+1]==passwndnew[i+1])
{
presscount=-1;
}
return presscount;
}
else if(i==(passwndold.length()-3))
{
if((passwndold[i+1]!=passwndnew[i+1])&&(passwndold[i+2]!=passwndnew[i+2]))
{
presscount++;
}
else if((passwndold[i+1]!=passwndnew[i+1])&&(passwndold[i+2]==passwndnew[i+2]))
{
presscount=-1;
}
else if((passwndold[i+1]==passwndnew[i+1])&&(passwndold[i+2]!=passwndnew[i+2]))
{
if(passwndold.length()==3)
{
presscount+=2;
}
else
{
presscount=-1;
}
}
else if((passwndold[i+1]==passwndnew[i+1])&&(passwndold[i+2]==passwndnew[i+2]))
{
presscount=-1;
}
return presscount;
}
//if((passwndold[i+1]!=passwndnew[i+1])&&(passwndold[i+2]!=passwndnew[i+2]))
//{
// presscount++;
// i=i+3;
// continue;
//}
if(passwndold[i+1]=='0')
{
passwndold[i+1]='1';
presscount++;
}
else
{
passwndold[i+1]='0';
presscount++;
}
if(passwndold[i]=='0')
{
passwndold[i]='1';
}
else
{
passwndold[i]='0';
}
if(passwndold[i+2]=='0')
{
passwndold[i+2]='1';
}
else
{
passwndold[i+2]='0';
}
}
}
return presscount;
}
int main()
{
string passwndold,passwndnew;
cin>>passwndold;
cin>>passwndnew;
//考虑第一个按钮的特殊性
int a=0,b=0;
a=PressCount(passwndold,passwndnew);
if(passwndold[0]=='0')
{
passwndold[0]='1';
if(passwndold[1]=='0')
{
passwndold[1]='1';
}
else
{
passwndold[1]='0';
}
}
else
{
passwndold[0]='0';
if(passwndold[1]=='0')
{
passwndold[1]='1';
}
else
{
passwndold[1]='0';
}
}
b=PressCount(passwndold,passwndnew);
b+=1;
if((a==-1)&&(b==0))
{
cout<<"impossible"<<endl;
}
else if((a!=-1)&&(b==0))
{
cout<<a<<endl;
}
else if((a==-1)&&(b!=0))
{
cout<<b<<endl;
}
else if((a!=-1)&&(b!=0))
{
if(a<=b)
{
cout<<a<<endl;
}
else
{
cout<<b<<endl;
}
}
// cout<<passwndold<<endl;
return 0;
}