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

      

  • 相关阅读:
    (转)卡特兰数
    fatal error LNK1123: 转换到 COFF 期间失败
    cocos2dx 中文乱码问题
    c++ primer查漏补缺(一)命名空间
    Centos 7 二进制部署高可用Kubernetes v1.17.x
    hostAliases给pod增加域名解析
    常用的清理 Kubernetes 集群资源命令
    Kubernets 污点与容忍
    Docker 常用命令大全
    Kubernets健康检查——配置存活、就绪和启动探测器
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2437482.html
Copyright © 2011-2022 走看看