zoukankan      html  css  js  c++  java
  • ACM-牛喝水

    题目描述:牛喝水 

    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?

    输入

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

    输出

    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.

    样例输入

    0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

    样例输出

    3


    思路:就像是背包问题或者是开关灯问题,每到一步有放和不放/开不开 两种状态,每种状态做尝试,遍历搜索即可。
    备注:之前尝试DFS模拟,但是反转的情况考虑不尽,所以直接用每步判断比较省事。

    // 牛喝水.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    
    
    #include <iostream>
    using namespace std;
    
    const int MAX = 1000;
    
    int n = 20, ans, flag, arr[MAX];
    
    int check()
    {
        for (int i = 0; i < n; i++)
        {
            if (arr[i] == 1)
                return 0;
        }
        return 1;
    }
    
    void printa()
    {
        for (int i = 0; i < n; i++)
        {
            cout << arr[i] << " ";
        }
        cout << endl;
    }
    
    void change(int pos)
    {
        arr[pos] = !arr[pos];
        if (pos - 1 >= 0) arr[pos - 1] = !arr[pos - 1];
        if (pos + 1 <= n) arr[pos + 1] = !arr[pos + 1];
    }
    
    //也许是反转的情况考虑少了。。。。。
    //void DFS(int a[])
    //{
    //    printa();
    //
    //    if (is(a) == 1) return;
    //
    //    for (int i = 0; i < n; i++)
    //    {
    //        if (a[i] == 1)
    //        {
    //            //cout << "i:" << i << "	a[i]:" << a[i] << endl;
    //            if ((i+1) < n && a[i + 1] == 1)
    //            {
    //                a[i] = change(a[i]);
    //                a[i + 1] = change(a[i + 1]);
    //                if (i + 2 < n) a[i + 2] = change(a[i + 2]);
    //            }            
    //            else
    //            {
    //                a[i] = change(a[i]);
    //                if (i - 1 >= 0) a[i - 1] = change(a[i - 1]);
    //                if (i + 1 < n) a[i + 1] = change(a[i + 1]);
    //            }
    //            num++;
    //            break;
    //
    //        }
    //        
    //    }
    //    DFS(a);
    //}
    
    void DFS(int pos,int sum, int start)
    {
        //cout << "pos:" << pos << "	sum" << sum << "	start:" << start << endl;
        if (flag) return;
        if (sum == start) { flag = check(); ans = sum;  return; }
        
        if (n - pos + 1 < start - sum) return;
    
        change(pos);
        DFS(pos + 1, sum + 1,start);
    
        change(pos);
        DFS(pos + 1, sum, start);
    }
    
    
    int main()
    {
        for (int i = 0; i < n; i++) cin >> arr[i];
    
        flag = 0;
        ans = -1;
        for (int i = 0; i < n; i++)
        {
            DFS(0, 0, i);
            if (flag)
            {
                cout << ans << endl;
                break;
            }
        }
        if (flag == 0) cout << "20" << endl;
    
        return 0;
    }
  • 相关阅读:
    c++中的const关键字
    用类模板实现容器存储自定义数据类型(类似于STL里面的vector)
    用类模板实现容器存储普通数据类型(类似于STL里面的vector)
    pgmpy包的安装,以及conda的安装
    【SQL语言】SQL语言基础02
    【win7系统】win7系统常遇到的一些问题
    【博客收集】一些关于个人博客、程序员书单、程序资源、学习方法的博文(持续更新中~)!
    【分享】一些好的网站与技术博客
    【ORACLE】Oracle 忘记用户名和密码的和用户被锁定的解决办法
    【ORACLE】SQL Developer 与 PL/SQL Developer与SQL PLUS的使用问题
  • 原文地址:https://www.cnblogs.com/x739400043/p/8537323.html
Copyright © 2011-2022 走看看