poj 8469 特殊密碼鎖
阿新 • • 發佈:2019-01-23
#include <iostream> #include <string.h> #include <algorithm> #include <cstdio> using namespace std; char s1[40],s2[40],s3[40]; int c1,c2,ans1,ans2,len; void Swap(char *s,int i) { s[i-1]= s[i-1]=='1'? '0' :'1'; s[i] = s[i]=='1'? '0' :'1'; if(i+1 < len) s[i+1] = s[i+1]=='1'? '0' :'1'; } int main() { while(~scanf("%s %s",s2,s3)) { strcpy(s1,s2); len = strlen(s1); ans1= ans2=1e9; c1 = 0,c2 = 1; bool flag =0; for(int i=1;i<len;i++) { if(s1[i-1] != s3[i-1]) { Swap(s1,i); c1++; } } if(strcmp(s1,s3)==0) { flag =1; ans1=c1; } strcpy(s1,s2); s1[0]= s1[0]=='1'? '0' :'1';//相當於已經按了第一個了 s1[1]= s1[1]=='1'? '0' :'1'; for(int i=1;i<len;i++) { if(s1[i-1] != s3[i-1]) { Swap(s1,i);c2++; } } if(strcmp(s1,s3)==0) { flag =1; ans2 =c2; } if(flag) { printf("%d\n",min(ans1,ans2)); } else { puts("impossible"); } } }