原题链接:
实现代码:
#include <iostream> #include <cstring> using namespace std; void filp(string &s,int i); int main() { int count = 0; string start,switchs,end; cin >> start >> end; switchs = start; for(unsigned int i =1;i<switchs.length();++i) { if(switchs[i-1] != end[i-1]) { ++count; filp(switchs,i); } } if(switchs == end) cout << count; else { switchs = start; count = 1; filp(switchs,0); for(unsigned int i =1;i<switchs.length();++i) { if(switchs[i-1] != end[i-1]) { ++count; filp(switchs,i); } } if(switchs == end) cout << count; else cout << "impossible"; } } void filp(string &s,int i) { if(s[i] == '1') s[i] = '0'; else s[i] = '1'; if(i == 0) { if(s[i+1] == '1') s[i+1] = '0'; else s[i+1] = '1'; } else if(i == (int)s.length()-1) { if(s[i-1] == '1') s[i-1] = '0'; else s[i-1] = '1'; } else { if(s[i+1] == '1') s[i+1] = '0'; else s[i+1] = '1'; if(s[i-1] == '1') s[i-1] = '0'; else s[i-1] = '1'; } }