比赛——The Water Bowls

来源:互联网 发布:淘宝网短棉衣 编辑:程序博客网 时间:2024/05/21 09:12

我说命运有时候真的很搞笑,上一个博客写的题目的方法思路这道题目就直接用上了。无语。。。

题目:

The Water Bowls

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 28   Accepted Submission(s) : 9
Problem Description
The cows have a line of 20 water bowls from which they drink. The bowls can be either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which holds no water). They want all 20 water bowls to be right-side-up and thus use their wide snouts to flip bowls.

Their snouts, though, are so wide that they flip not only one bowl but also the bowls on either side of that bowl (a total of three or -- in the case of either end bowl -- two bowls).

Given the initial state of the bowls (1=undrinkable, 0=drinkable -- it even looks like a bowl), what is the minimum number of bowl flips necessary to turn all the bowls right-side-up?
 


 

Input
Line 1: A single line with 20 space-separated integers
 


 

Output
Line 1: The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to 0). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to 20 0's.
 


 

Sample Input
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0
 


 

Sample Output
3

解题思路:

这道题目的意思是翻碗,一次行翻要翻得的那个和它两旁的那两个,也就是说三个。求最小需要翻的次数;

一共有20个碗,所以最多需要翻20次。从翻一次,一直找到翻20次,题目说了一定可以翻完,所以不考虑翻20次还不成功的情况。

还有就是要考虑初始情况全是0的这种情况,这种情况下输出是0,我就因为这个WA了一次,郁闷;

#include<stdio.h>
int bowl[20];
int pan()
{
 int i;
 for(i=0;i<20;i++)
  if(bowl[i]==1)
   return 0;
 return 1;
}
void change(int i)
{
 if(i>=0&&i<20)
  bowl[i]=(bowl[i]+1)%2;
}
void fan(int i)
{
 change(i-1);change(i);change(i+1);
}
int ves(int m, int n)
{
 int i;
 if(pan())return 1;
 if(n==0)return 0;
 for(i=m;i<20;i++)
 {
  fan(i);
  if(ves(i+1,n-1))
   return 1;
  fan(i);
 }
 return 0;
}
int main()
{
 int i,con[20],j;
 for(i=0;i<20;i++)
 {
  scanf("%d",&con[i]);
  bowl[i]=con[i];
 }
 if(pan())
 {
  printf("0\n");
  return 0;
 }
 for(i=1;i<=20;i++)
 {
  for(j=0;j<20;j++)
   bowl[j]=con[j];
  if(ves(0,i)==1)
   break;
 }
 printf("%d\n",i);
 return 0;
}