编程介的小学生 2017-01-29 16:38 采纳率: 20.5%
浏览 851
已采纳

The Water Bowls

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]

  • 写回答

1条回答 默认 最新

  • love_QJP 2017-02-01 11:40
    关注

    #include
    #include

    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;
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题