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

      

  • 相关阅读:
    推荐.Net、C# 逆向反编译四大工具利器
    逆向工具/反编译工具 集合
    秒杀系统-并发处理
    react动态渲染组件
    通过dom获取react节点
    mac上安装windows触摸板不能右键
    STM32F103智能配网mqtt协议EMQ安卓App远程控制LED采集温湿度ESP8266
    itop4412 基于物联网技术的商品支付系统 毕业设计
    STM32F103 单片机最小系统 核心板 C8T6 MINI 飞控 小车 主控制板
    31865 MAX31865 RTD铂电阻温度检测器 PT100至PT1000传感器模块
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2437482.html
Copyright © 2011-2022 走看看