题目描述:牛喝水 

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 = ; int n = , ans, flag, arr[MAX]; int check()
{
for (int i = ; i < n; i++)
{
if (arr[i] == )
return ;
}
return ;
} void printa()
{
for (int i = ; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
} void change(int pos)
{
arr[pos] = !arr[pos];
if (pos - >= ) arr[pos - ] = !arr[pos - ];
if (pos + <= n) arr[pos + ] = !arr[pos + ];
} //也许是反转的情况考虑少了。。。。。
//void DFS(int a[])
//{
// printa();
//
// if (is(a) == 1) return;
//
// for (int i = 0; i < n; i++)
// {
// if (a[i] == 1)
// {
// //cout << "i:" << i << "\ta[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 << "\tsum" << sum << "\tstart:" << start << endl;
if (flag) return;
if (sum == start) { flag = check(); ans = sum; return; } if (n - pos + < start - sum) return; change(pos);
DFS(pos + , sum + ,start); change(pos);
DFS(pos + , sum, start);
} int main()
{
for (int i = ; i < n; i++) cin >> arr[i]; flag = ;
ans = -;
for (int i = ; i < n; i++)
{
DFS(, , i);
if (flag)
{
cout << ans << endl;
break;
}
}
if (flag == ) cout << "" << endl; return ;
}

ACM-牛喝水的更多相关文章

  1. [Swust OJ 781]--牛喝水

    Time limit(ms): 1000 Memory limit(kb): 65535   The cows have a line of 20 water bowls from which the ...

  2. FZYZOJ-1569 喝水

    P1569 -- 喝水 时间限制:2000MS      内存限制:131072KB 状态:Accepted      标签:    无   无   无 Description GH的N个妹子要喝水, ...

  3. [BZOJ 3441]乌鸦喝水

    3441: 乌鸦喝水 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 148[Submit][Status][Discuss] ...

  4. Bzoj3441 乌鸦喝水

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 258  Solved: 97 Description [题目背景]     一只乌鸦在自娱自乐,它在面 ...

  5. ACM牛人博客

    ACM牛人博客 kuangbin kuangbin(新) wuyiqi wuyiqi(新) ACM!荣耀之路! 九野的博客 传说中的ACM大牛!!! read more

  6. BZOJ:3441 乌鸦喝水

    bzoj:3441 乌鸦喝水 题目传送门 Description 一只乌鸦在自娱自乐,它在面前放了n个有魔力的水缸,水缸里装有无限的水. 他准备从第1个水缸飞到第n个水缸,共m次.在飞过一个水缸的过程 ...

  7. 8.18 NOIP模拟测试25(B) 字符串+乌鸦喝水+所驼门王的宝藏

    T1 字符串 卡特兰数 设1为向(1,1)走,0为向(1,-1)走,限制就是不能超过$y=0$这条线,题意转化为从(0,0)出发,走到(n+m,n-m)且不越过$y=0$,然后就裸的卡特兰数,$ans ...

  8. 推荐一款健康App 多喝水,引领全民时尚喝水生活习惯

    推荐一款健康App 多喝水,引领全民时尚喝水生活习惯 1 介绍 多喝水,一款鼓励大众喝水的APP.我们倡导大众健康生活,培养人们爱喝水的习惯,让每一次喝水,都能产生价值,让人们在喝水的同时,可享受赚钱 ...

  9. 通过喝水清晰简单了解I/O五大模型

    一般单次I/O请求会分为两个阶段,每个阶段对于I/O的处理方式是不同的 I/O会经历一个等待资源的阶段 阻塞,指的是在数据不可用时,I/O请求会一直阻塞,直到数据返回 数据不可用时,立即返回,直到被通 ...

随机推荐

  1. C# 中的委托和事件 转载张子阳的

        C# 中的委托和事件 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人 ...

  2. No qualifying bean of type 'org.springframework.ui.Model' available

    原因:@Autowired 下面没有注入类

  3. routes 学习

    对于routes的学习,感觉还是看官方文档理解的比较快,主要说明connect和resource Setting up routes¶ It is assumed that you are using ...

  4. java SHA1加密算法

    package com.cn.test.rsa; import java.security.MessageDigest; import java.security.NoSuchAlgorithmExc ...

  5. JS enter键一键登录

    $("body").keydown(function (event) { ) { //enter键键值为13 $('.finish-btn').click(); // $('.fi ...

  6. Day2-C-迷宫问题 -POJ3984

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  7. 网络流之最大流(详解 附《算法导论》上证明 及P4843 清扫雪道)

    由于此篇笔记写于本地,篇幅较大,导出困难,所以将其转换为了图片,方便阅读.

  8. 使用Spring Cloud Gateway保护反应式微服务(一)

    反应式编程是使你的应用程序更高效的一种越来越流行的方式.响应式应用程序异步调用响应,而不是调用资源并等待响应.这使他们可以释放处理能力,仅在必要时执行处理,并且比其他系统更有效地扩展. Java生态系 ...

  9. Java导出Excel(项目实战Demo)

    Controller 层 /** * 考勤机刷卡明细导出Excel * * @throws Exception */ // @RequiresPermissions("report:Expo ...

  10. Core Data 基本数据操作 增删改查 排序

    所有操作都基于Core Data框架相关 API,工程需要添加CoreData.framework支持 1.增  NSEntityDescription insertNewObjectForEntit ...