特殊密码锁
001:特殊密码锁
- 总时间限制:
- 1000ms
- 内存限制:
- 1024kB
- 描述
-
有一种特殊的二进制密码锁,由n个相连的按钮组成(n<30),按钮有凹/凸两种状态,用手按按钮会改变其状态。
然而让人头疼的是,当你按一个按钮时,跟它相邻的两个按钮状态也会反转。当然,如果你按的是最左或者最右边的按钮,该按钮只会影响到跟它相邻的一个按钮。
当前密码锁状态已知,需要解决的问题是,你至少需要按多少次按钮,才能将密码锁转变为所期望的目标状态。
- 输入
- 两行,给出两个由0、1组成的等长字符串,表示当前/目标密码锁状态,其中0代表凹,1代表凸。
- 输出
- 至少需要进行的按按钮操作次数,如果无法实现转变,则输出impossible。
- 样例输入
-
011 000
- 样例输出
-
1
#include <iostream> #include <cstring> using namespace std; int *stringtoint(string &s) { int *a = new int[s.size()]; for (int i = 0; i < s.size() ; i++) { a[i] = (int)s[i] - 48; } return a; } int main() { string light, option; int count = 0; cin >> light; cin >> option; int count0; for (int k = 0; k < 2; k++) { count0 = count; count = 0; int *in = stringtoint(light); int *out = stringtoint(option); int key = k, i; for ( i = 0; i < light.size(); i++) { if (key) { count++; // cout << "i" << i << endl; if (i > 0) in[i - 1] = !in[i - 1]; in[i] = !in[i]; if (i < light.size() - 1) { in[i + 1] = !in[i + 1]; } } // cout << "in" << i << "=" << in[i] << endl; if (in[i] == out[i]) key = 0; else key = 1; } i = light.size() - 1; if (in[i] == out[i]) { } else { count = 0; } // cout << count << endl; } if (count > count0) { int temp = count; count = count0; count0 = temp; } if (count != 0) cout << count; else if (count0 != 0) cout << count0; else cout << "impossible"; return 0; }