The Water Bowls POJ 3185
2017-04-01 12:04
357 查看
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
Explanation of the sample:
Flip bowls 4, 9, and 11 to make them all drinkable:
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state]
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 4]
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11]
题意:给0,1组成的长度为20的数列,每次能变动连续的三个,问需要多少次将所有数字变成0
方法:从左往右扫描序列,每次遇到1就将其与其后面两个数字反转(因为每个数字最多翻转一次,且从前往后扫)同理,在从后往前扫一次。记录下两次需要反转的次数,取最小值。
代码:
//By Sean Chen
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int a[30],temp[30];
int main()
{
while(scanf("%d",&a[0])!=EOF)
{
for(int i=1;i<20;i++)
scanf("%d",&a[i]);
for(int i=0;i<20;i++)
temp[i] = a[i];
int ans1=0;
for(int i=0;i<20;i++)
{
if(a[i]==1)
{
if(i==19)
{
ans1=inf;
break;
}
ans1++;
a[i+1]=!a[i+1];
a[i+2]=!a[i+2];
}
}
int ans2=0;
for(int i=19;i>=0;i--)
{
if(temp[i]==1)
{
if(i==0)
{
ans2=inf;
break;
}
ans2++;
temp[i-1]=!temp[i-1];
temp[i-2]=!temp[i-2];
}
}
int ans=min(ans1,ans2);
printf("%d\n",ans);
}
return 0;
}
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 0Sample Output
3Hint
Explanation of the sample:
Flip bowls 4, 9, and 11 to make them all drinkable:
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state]
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 4]
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9]
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11]
题意:给0,1组成的长度为20的数列,每次能变动连续的三个,问需要多少次将所有数字变成0
方法:从左往右扫描序列,每次遇到1就将其与其后面两个数字反转(因为每个数字最多翻转一次,且从前往后扫)同理,在从后往前扫一次。记录下两次需要反转的次数,取最小值。
代码:
//By Sean Chen
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int a[30],temp[30];
int main()
{
while(scanf("%d",&a[0])!=EOF)
{
for(int i=1;i<20;i++)
scanf("%d",&a[i]);
for(int i=0;i<20;i++)
temp[i] = a[i];
int ans1=0;
for(int i=0;i<20;i++)
{
if(a[i]==1)
{
if(i==19)
{
ans1=inf;
break;
}
ans1++;
a[i+1]=!a[i+1];
a[i+2]=!a[i+2];
}
}
int ans2=0;
for(int i=19;i>=0;i--)
{
if(temp[i]==1)
{
if(i==0)
{
ans2=inf;
break;
}
ans2++;
temp[i-1]=!temp[i-1];
temp[i-2]=!temp[i-2];
}
}
int ans=min(ans1,ans2);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- poj 3185 The Water Bowls
- poj 3185 The Water Bowls(数学:高斯消元)
- poj 3185 The Water Bowls(高斯消元 开关问题)
- POJ 3185 The Water Bowls(高斯消元,枚举变元)
- POJ 3185 The Water Bowls(高斯消元法,枚举自由变元)
- poj 3185 The Water Bowls
- POJ 3185 The Water Bowls (高斯消元 求最小步数)
- poj -- 3185 The Water Bowls(高斯消元)
- POJ 3185 The Water Bowls
- POJ - 3185 The Water Bowls
- POJ 3185 The Water Bowls(DFS搜索)
- POJ 3185 The Water Bowls
- poj 3185 The Water Bowls(高斯消元)
- POJ-3185 The Water Bowls 高斯消元
- POJ 3185 The Water Bowls 高斯消元+枚举变元
- 【POJ】3185 The Water Bowls
- poj 3185 The Water Bowls
- poj 3185 The Water Bowls 【高斯消元 + 枚举自由变元】
- POJ-3185-The Water Bowls
- POJ 3185 The Water Bowls(高斯消元-枚举变元个数)