Enumeration special locks

This problem has been pass rate 100%, ssfd ...... and this slag or because of mental retardation and WA ......

topic

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.

Input
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.
Output
at least need to press the button of the number of operations, if the change can not be achieved, the output impossible.
Sample input
011
000
Sample Output
1

analysis

This question is very simple, in fact, a variant lights problem, but the beginning of a sucker, direct enumeration of each state (think of it as a case of lights out of order), did not pay attention to the 30th power, but there is the largest astronomical n of 30,2 Oh, so the harvest TLE.
Actually, only the enumerator to the first state of the switch. After determining each of the switches, the switches corresponding to the irregularities determined only by its back that switch, i.e. a state that the back of the switch is determined. Finally, whether the last switch, the first switch if the two states, and finally the switches are not the same as the target, then the output impossible. Only 4ms.

AC Code

#include <iostream>
#include <bitset>
#include <cstring>
#include <algorithm>
using namespace std;
bitset <30> initt;
bitset <30> goal;
int main() {
	char s[31], e[31];
	cin.getline(s,30);
	cin.getline(e,30);
	int lens = strlen(s);
	for (int i = 0; i < lens; ++i) {
		if (s[i] == '1') initt[i] = 1;
		else initt[i] = 0;
		if (e[i] == '1') goal[i] = 1;
		else goal[i] = 0;
	}
	bitset <30> sw;
	int minop = 31;
	for (int x = 0; x <= 1; ++x) {
		bitset <30> init = initt;
		sw[0] = x;
		if (sw[0]) {
			init.flip(0);
			init.flip(1);
		}
		for (int i = 1; i < lens; ++i) {
			if (init[i-1] == goal[i-1]) sw[i] = 0;
			else {
				sw[i] = 1;
				init.flip(i-1);
				init.flip(i);
				if (i < lens - 1) init.flip(i+1);
			}
		}
		if (init[lens-1] == goal[lens-1]) {
			if (minop > sw.count()) minop = sw.count();
		}
	}
	if (minop == 31) cout << "impossible" << endl;
	else cout << minop << endl;
	return 0;
}

It is almost a template question.

Guess you like

Origin blog.csdn.net/weixin_44288817/article/details/90108697