随笔 - 148,  文章 - 0,  评论 - 6,  阅读 - 88384
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

The Water Bowls

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 3071

 

Accepted: 1173

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]

Source

USACO 2006 January Bronze

 解题报告:这道题是昨天的省赛选拔三的题,当时我没有负责这道题,今天看了队友写的代码,原来是搜索题!就是通过翻转全部变成0,经过的次数。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 21;
int bowl[MAX], step, flag;
bool Judge()
{
    for (int i = 0; i < 20; ++i)
    {
        if (bowl[i])//不全部为0时
        {
            return false;
        }
    }
    return true;
}
void Change(int i)
{
    bowl[i] = !bowl[i];
    if (i > 0)//边界处理
    {
        bowl[i - 1] = !bowl[i - 1];
    }
    if (i < 19)//边界处理
    {
        bowl[i + 1] = !bowl[i + 1];
    }
}
void DFS(int i, int num)
{
    if (step == num)
    {
        flag = Judge();
        return;
    }
    if (i >= 20 || flag)
    {
        return;
    }
    Change(i);//翻转
    DFS(i + 1, num + 1);
    Change(i);//再翻转回来
    DFS(i + 1, num);
}
int main()
{
    int i, ans;
    for (i = 0; i < 20; ++i)
    {
        scanf("%d", &bowl[i]);
    }
    for (step = 0; step < 20; ++step)
    {
        flag = 0;
        DFS(0, 0);
        if (flag)
        {
            ans = step;
            break;
        }
    }
    printf("%d\n", ans);
    return 0;
}

  

posted on   Stephen Li  阅读(373)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示