POJ3185:The Water Bowls
来源:互联网 发布:存心自有天知 编辑:程序博客网 时间:2024/06/05 19:15
The Water Bowls
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6177 Accepted: 2431
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
Hint
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]
#include<stdio.h>#include<stdlib.h>int a[21],flag,step;int test(){ int i; for(i=0;i<20;i++){ if(a[i]){ return 0; //有1 不满足条件 } } return 1;}void turn(int n){ a[n]=!a[n]; //翻转 if(n>0){ a[n-1]=!a[n-1]; } if(n<19){ a[n+1]=!a[n+1]; }}void dfs(int i,int j){ if(step==j){ flag=test(); return; } if(i>20||flag){ //结束循环 return; } turn(i); dfs(i+1,j+1); //这层递归为翻转第n个碗 turn(i); dfs(i+1,j); //这层递归不翻转}int main(){ int i; for(i=0;i<20;i++){ scanf("%d",&a[i]); } for(step=0;step<20;step++){ flag=0; dfs(0,0); if(flag){ break; } } printf("%d\n",step); return 0;}
- POJ3185-The Water Bowls
- POJ3185:The Water Bowls
- POJ3185-The Water Bowls
- poj3185 The Water Bowls 高斯消元
- POJ3185 The Water Bowls【高斯消元法】
- POJ3185 The Water Bowls 反转(开关)
- POJ3185——The Water Bowls
- poj3185--The Water Bowls(高斯消元问题3)
- POJ3185 The Water Bowls 高斯消元+枚举
- poj3185 The Water Bowls 开关问题
- POJ3185-The Water Bowls-反转问题
- poj3185 The Water Bowls 开关问题
- PKU 3185 The Water Bowls
- poj 3185 The Water Bowls
- POJ 3185 (The Water Bowls)
- PKU 3185 The Water Bowls
- POJ 3185 The Water Bowls
- poj 3185 The Water Bowls
- 【zabbix】利用LLD自动发现功能监控多Redis实例
- 蓝桥杯训练 表达式计算
- git常用命令
- 1017. A除以B (20) PAT乙级
- cmd中编译成功后运行时出现wrong name 错误问题
- POJ3185:The Water Bowls
- hdu 1176 免费馅饼
- 腾讯笔试题--微信红包
- 如何快速转载CSDN中的博客
- 半平面的交,二分法(离海最远的点,LA 3890)
- ROS(6)消息和服务具体实现
- 数据结构实验之栈四:括号匹配+(字符串中空格的输入)
- 商务综合管理-day1-项目搭建
- 树莓派服务器搭建