zoukankan      html  css  js  c++  java
  • POJ 3185 The Water Bowls(DFS搜索)

    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;
    }
    

      

  • 相关阅读:
    交叉熵的数学原理及应用——pytorch中的CrossEntropyLoss()函数
    pytorch中如何使用DataLoader对数据集进行批处理
    Pytorch中的自动求导函数backward()所需参数含义
    Pytorch中的torch.cat()函数
    Pytorch中的squeeze()和unsqueeze()函数
    UBUNTU18.04安装网易云音乐并直接图标启动
    UBUNTU18.4环境下使用更好用的搜索引擎(无奈,只能起这样的标题)
    Ubuntu 18.04换国内源 中科大源 阿里源 163源 清华源
    共享栈
    C++(十七) — 宏代码、内联函数
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2437482.html
Copyright © 2011-2022 走看看