poj 3185 The Water Bowls(反转)
Description
The cows have a line of 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 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 (=undrinkable, =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 : A single line with space-separated integers
Output
Line : The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to ). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to 's.
Sample Input
Sample Output
Hint
Explanation of the sample: Flip bowls , , and to make them all drinkable:
[initial state]
[after flipping bowl ]
[after flipping bowl ]
[after flipping bowl ]
Source
题意:翻盖有奖:将一列碗翻成口朝上,一把下去可能同时反转3个或2个(首尾),求最小翻转次数。
这里给出两种方法:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int a[N],aa[N];
int main()
{
for(int i=;i<;i++){
scanf("%d",&a[i]);
}
int ans=;
for(int i=;i<;i++){
int cnt=;
memcpy(aa,a,sizeof(a));
if(i==){
aa[]++;
aa[]++;
cnt++;
}
else if(i==){
aa[]++;
aa[]++;
cnt++;
}
else if(i==){
aa[]++;
aa[]++;
aa[]++;
aa[]++;
cnt+=;
}
for(int i=;i<=;i++){
if(aa[i]&){
aa[i]++;
aa[i+]++;
aa[i+]++;
cnt++;
}
}
int flag=;
for(int j=;j<;j++){
if(aa[j]&){
flag=;
break;
}
}
if(flag){
ans=min(ans,cnt);
}
}
printf("%d\n",ans);
return ;
}
第二种方法:dfs枚举
思路:枚举反转的步数,dfs,判断是否可行,可行则输出,否则继续。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int a[N];
int flag;
int is_ok(){
int f=;
for(int i=;i<;i++){
if(a[i]==){
f=;
break;
}
}
if(f){
return ;
}
else{
return ;
}
}
void turn(int i){
a[i]=!a[i];
if(i>){
a[i-]=!a[i-];
}
if(i<){
a[i+]=!a[i+];
}
}
void dfs(int cur,int num,int step){
if(flag) return;
if(num==step){
flag=is_ok();
return;
}
if(cur>=) return; turn(cur);
dfs(cur+,num+,step);
turn(cur);
dfs(cur+,num,step);
}
int main()
{
int num=;
for(int i=;i<;i++){
scanf("%d",&a[i]);
if(a[i]==){
num++;
}
}
if(num==){
printf("0\n");
return ;
} int ans;
for(int i=;i<;i++){
flag=;
dfs(,,i);
if(flag){
ans=i;
break;
}
}
printf("%d\n",ans);
return ;
}
poj 3185 The Water Bowls(反转)的更多相关文章
- POJ 3185 The Water Bowls 【一维开关问题 高斯消元】
任意门:http://poj.org/problem?id=3185 The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 3185 The Water Bowls
The Water Bowls 题意:给定20个01串(最终的状态),每个点变化时会影响左右点,问最终是20个0所需最少操作数? 水题..直接修改增广矩阵即可:看来最优解不是用高斯消元(若是有Gaus ...
- POJ 3185 The Water Bowls(高斯消元-枚举变元个数)
题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...
- POJ 3185 The Water Bowls (高斯消元)
题目链接 题意:翻译过来就是20个0或1的开关,每次可以改变相邻三个的状态,问最小改变多少次使得所有开关都置为0,题目保证此题有解. 题解:因为一定有解,所以我们可以正序逆序遍历两次求出较小值即可.当 ...
- POJ 3185 The Water Bowls (高斯消元 求最小步数)
题目链接 题意:有20个数字,0或1.如果改变一个数的状态,它左右两边的两个数的状态也会变反.问从目标状态到全0,至少需要多少次操作. 分析: 和上一题差不多,但是比上一题还简单,不多说了,但是在做题 ...
- poj 3185 The Water Bowls 高斯消元枚举变元
题目链接 给一行0 1 的数, 翻转一个就会使他以及它左右两边的都变, 求最少多少次可以变成全0. 模板题. #include <iostream> #include <vector ...
- POJ:3185-The Water Bowls(枚举反转)
The Water Bowls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7402 Accepted: 2927 Descr ...
- POJ3185 The Water Bowls 反转(开关)
Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...
- POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题
http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...
随机推荐
- bzoj3407 [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题
Description 贝茜像她的诸多姊妹一样,因为从约翰的草地吃了太多美味的草而长出了太多的赘肉.所以约翰将她置于一个及其严格的节食计划之中.她每天不能吃多过H(5≤日≤45000)公斤的干 ...
- Android framework浅析[转]
Android系统从底向上一共分了4层,每一层都把底层实现封装,并暴露调用接口给上一层. 1. Linux内核(Linux Kernel) 1)Android运行在linux kernel 2.6之上 ...
- 学生管理系统(list)
学生管理系统:学习了一点文件指针的操作和链表操作,以前总想搞下子,刚好碰到同学要做这个,自己瞎搞了一通. 实现功能:数据添加,查找,删除,插入,修改只是在查找加几句就没写. #include < ...
- 苹果试图做?XCode6 放弃prefix.pch档
当我们升级到XCode6后, 新建project发现默认是没有pch文件的.非常多人開始不习惯了,苹果到底为什么要取消这一个pch文件. 苹果觉得,因为组件单一模块的原因.你不应该在你的prefix代 ...
- Android项目实战-云词典
前段时间在网上看到滴答滴答的一个项目,字典文章,找到一个简单的字.整句翻译功能,词汇,但漫长的岁月项目,二手API数据不再可用,我决定使用其现有的框架来实现其功能,主要采用的技术GSON和Volley ...
- Codevs1992题解
题目大意 求有向图中经过某一点k的最大环(数据规模不支持floyd). 题解 以k为起点在正向图中spfa求单源最短路.再在反向图中spfa求单源最短路. 枚举除k外的每个点i.假设有一个同一时候包括 ...
- ASP.NET Signalr 2.0 实现一个简单的聊天室
学习了一下SignalR 2.0,http://www.asp.net/signalr 文章写的很详细,如果头疼英文,还可以机翻成中文,虽然不是很准确,大概还是容易看明白. 理论要结合实践,自己动手做 ...
- EF查询数据库框架的搭建
一个简单的EF查询框架除了运行项目外,大概需要5个类库项目,当然这个不是一定要这样做,这可以根据自己的需要设置有多少个项目.这里介绍的方法步骤只适合EF零基础的人看看就是了. 在开始之前,先建立一个运 ...
- sql一些命令
1.创建表 create table tSId ( tSid int identity(1,1) primary key, tSName varchar(10) check(len(tSName)&g ...
- css基础之 语法
CSS 实例 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器通常是您需要改变样式的 HTML 元素. 每条声明由一个属性和一个值组成. 属性(property)是您希望设置的样 ...