POJ3185——The Water Bowls

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

典型的高消解异或方程组,需要枚举自由变元。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn=30;
int x[maxn];//存最后的解
int mat[maxn][maxn];
int free_x[maxn];//存自由变元

int Gauss(int equ,int var)
{
int i,j,k;
int free_index,free_num;
int max_r,col,num;
col=0;
num=0;
memset(x,0,sizeof(x));
for(int i=0;i<var+1;i++)
{
x[i]=0;
free_x[i]=0;
}
for(k=0;k<equ && col<var;k++,col++)
{
max_r=k;//找到col列上绝对值最大的那行(k行的下面那些行)
for(i=k+1;i<equ;i++)
{
if(abs(mat[i][col]) > abs(mat[max_r][col]))
max_r=i;
}
if(max_r!=k)//找到后和第k行交换
{
for(j=k;j<var+1;j++)
swap(mat[k][j],mat[max_r][j]);
}
if(!mat[k][col])
{
k--;
free_x[num++]=col;//如果第k行第col这个数为0,也就是系数为0,那他就是个自由变量
continue;
}
for(i=k+1;i<equ;i++)
{
if(mat[i][col])
{
for(j=col;j<var+1;j++)
mat[i][j]^=mat[k][j];//异或以后消去k+1行到最后一行的第col列的系数,形成上三角矩阵
}
}
}
for(i=k;i<equ;i++)
if(mat[i][col])
return -1;//无解
//接下来得枚举自由变元
int all=1<<(var-k);//var-k个自由变元,那么有2的指数次个状态
int res=0x3f3f3f3f;
for(i=0;i<all;i++)
{
int cnt=0;
int index=i;
for(j=0;j<var-k;j++)
{
x[free_x[j]]=(index&1);
if(index&1)//如果是1,则要反转,也就是要操作一次
cnt++;
index>>=1;
}
//同时还要判断确定的变量
for(j=k-1;j>=0;j--)
{
int temp=mat[j][var];
for(int l=j+1;l<var;l++)
if(mat[j][l])
temp^=mat[j][l]*x[l];
x[j]=temp;
if(x[j])
cnt++;
}
if(cnt < res)
res = cnt;
}
return res;
}

int num[22];

int main()
{
while(~scanf("%d",&num[0]))
{
memset(mat,0,sizeof(mat));
for(int i=1;i<20;i++)
scanf("%d",&num[i]);
for(int i=0;i<20;i++)
{
mat[i][i]=1;
if(i>0)
mat[i][i-1]=1;
if(i<19)
mat[i][i+1]=1;
}
for(int i=0;i<20;i++)
mat[i][20]=num[i];
int res=Gauss(20,20);
printf("%d\n",res);
}
return 0;
}


【顶级EI完美复现】电力系统碳排放流的计算方法【IEEE 14节点】(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI完美复现】电力系统碳排放流的计算方法【IEEE 14节点】(Matlab代码实现)》的技术文档,核心内容是基于IEEE 14节点电力系统模型,利用Matlab实现碳排放流的精确计算方法。该方法通过建立电力系统中各节点的功率流动与碳排放之间的映射关系,实现对电能传输过程中碳足迹的追踪与量化分析,属于电力系统低碳调度与碳流管理领域的关键技术。文中强调“顶级EI完美复现”,表明其算法和仿真结果具有较高的学术严谨性和可重复性,适用于科研验证与教学演示。; 适合人群:电力系统、能源与动力工程、电气工程及其自动化等相关专业的研究生、科研人员以及从事电力系统低碳化、碳排放核算工作的技术人员。; 使用场景及目标:①用于电力系统碳排放流理论的学习与仿真验证;②支撑含新能源接入的电力系统低碳调度、碳交易、绿色电力溯源等课题的研究;③为撰写高水平学术论文(如EI/SCI期刊)提供可靠的代码基础和技术参考。; 阅读建议:读者应具备电力系统分析、Matlab编程的基础知识,建议结合电力系统潮流计算、节点导纳矩阵等前置知识进行学习,并通过调整系统参数和运行方式,深入理解碳排放流的分布规律与影响因素。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值