标签:
1 #include<stdio.h> 2 #include<string.h> 3 #define MIN(x,y)(x<y?x:y) 4 int len,flot,ans; 5 int s[25]; 6 void turn(int x){ 7 s[x]=!s[x]; 8 if(x-1>=0)s[x-1]=!s[x-1]; 9 if(x+1<len)s[x+1]=!s[x+1]; 10 } 11 bool find(){ 12 for(int i=0;i<len;i++){ 13 // printf("%d ",s[i]); 14 //if(s[i])puts(""); 15 if(s[i])return false; 16 } 17 return true; 18 } 19 void dfs(int x,int time){ 20 if(find()){ 21 flot=1; 22 ans=MIN(ans,time); 23 return; 24 } 25 if(x>=len)return; 26 dfs(x+1,time); 27 turn(x); 28 dfs(x+1,time+1); 29 turn(x); 30 } 31 int main(){ 32 char m[20]; 33 while(~scanf("%s",m)){ 34 len=strlen(m); 35 for(int i=0;i<len;i++)s[i]=m[i]-‘0‘; 36 s[len]=‘\0‘; 37 ans=0x3f3f3f3f; 38 flot=0; 39 dfs(0,0); 40 //printf("%d\n",ans); 41 if(flot)printf("%d\n",ans); 42 else puts("NO"); 43 } 44 return 0; 45 }
代码2;
1 #include<stdio.h> 2 #include<string.h> 3 #define MIN(x,y)(x<y?x:y) 4 int len,flot,ans; 5 int s[25],cnt[25]; 6 void turn(int x){ 7 s[x]=!s[x]; 8 if(x-1>=0)s[x-1]=!s[x-1]; 9 if(x+1<len)s[x+1]=!s[x+1]; 10 } 11 bool find(){ 12 for(int i=0;i<len;i++){ 13 // printf("%d ",s[i]); 14 //if(s[i])puts(""); 15 if(s[i])return false; 16 } 17 return true; 18 } 19 void dfs(int x){ 20 if(find()){ 21 flot=1; 22 int temp=0; 23 for(int i=0;i<len;i++)if(cnt[i]==1)temp++; 24 ans=MIN(ans,temp); 25 return; 26 } 27 if(x>=len)return; 28 for(cnt[x]=0;cnt[x]<2;){ 29 turn(x); 30 cnt[x]++; 31 dfs(x+1); 32 } 33 } 34 int main(){ 35 char m[20]; 36 while(~scanf("%s",m)){ 37 len=strlen(m); 38 for(int i=0;i<len;i++)s[i]=m[i]-‘0‘; 39 s[len]=‘\0‘; 40 ans=0x3f3f3f3f; 41 flot=0; 42 memset(cnt,0,sizeof(cnt)); 43 dfs(0); 44 //printf("%d\n",ans); 45 if(flot)printf("%d\n",ans); 46 else puts("NO"); 47 } 48 return 0; 49 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4862178.html