poj 3185 The Water Bowls (dfs)
#include<stdio.h> int ans,a[22],f,step; int judge() { int i; for(i=1;i<=20;i++) if(a[i]==1)return 0; return 1; } void change(int x) { a[x]=!a[x]; if(x-1>=1)a[x-1]=!a[x-1]; if(x+1<=20)a[x+1]=!a[x+1]; } void dfs(int x,int num) { if(step==num) { if(judge()) { f=1; } return ; } if(f||x>20)return ; change(x); dfs(x+1,num+1);//反转 change(x); dfs(x+1,num);//不反转 } int main() { int i; for(i=1;i<=20;i++) { scanf("%d",&a[i]); } ans=0; f=0; for(step=0;step<20;step++) { dfs(0,0); if(f) { ans=step; break; } } printf("%d\n",ans); }