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

      

  • 相关阅读:
    博客是一种心情
    双显示器和双鼠标
    C++ Primer Plus 第六版 代码笔记
    Bug,昂首走进2005
    让万能五笔2004免费版改首页的功能见鬼去吧。
    Gmail提供POP3和SMTP已经老长时间了
    快过年回家了,发点实用的东西给大家
    关于博客园Post的时候排版的一点小建议
    在.Net下进行MicroStation(GG)的开发(一)
    验证域的另一方法
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2437482.html
Copyright © 2020-2023  润新知