此类题型往往状态有限且可以通过特定形式表现出来,例如bite序列等,往往通过一定的状态压缩和酶具有有效序列的方式解决问题。
说明:
-
scanf(" %c", &c);其中格式中含有空格,可以有效避免读入空格或者换行符等,使得c一定是字符形式
-
0x7f7f7f7f有时会很好用
3.位运算符及位运算要合理应用,注意不确定就加括号
#include <cstdio>
using namespace std;
int a[20], b[20], change[20];
char c;
int calu(int x){
int cnt = 0;
while(x){
if(x & 1) cnt ++;
x = (x >> 1);
}
return cnt;
}
int deal(int a[]){
int ans = 0x7f7f7f7f;
int cur[20], cnt;
for(change[0] = 0; change[0] <= ((1 << 4) - 1); change[0]++){
cnt = calu(change[0]);
cur[0] = a[0] ^ change[0] ^ (change[0] >> 1) ^ (change[0] << 1) & ((1 << 4) - 1);
cur[1] = a[1] ^ change[0];
for(int i = 1; i < 4; i++){
// printf("cur[i]=%d\n", cur[i]);
change[i] = cur[i - 1];
cnt += calu(change[i]);
cur[i] = cur[i] ^ change[i] ^ (change[i] >> 1) ^ (change[i] << 1) & ((1 << 4) - 1);
cur[i + 1] = a[i + 1] ^ change[i];
}
if(cur[3] == 0){
ans = min(ans, cnt);
}
}
return ans;
}
int main(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
scanf(" %c", &c);
if(c == 'b') a[i] |= (1 << j);
else b[i] |= (1 << j);
}
// printf("a[i]=%d\n", a[i]);
}
int anw = min(deal(a), deal(b));
if(anw > 16) cout << "Impossible" << endl;
else cout << anw << endl;
return 0;
}