题目链接:https://vjudge.net/contest/276374#problem/A

题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置,第i-1个位置的盘子都会翻转,第一个和最后一个例外(只有两个)。然后问你最少的操作数能够使得盘子全部变成反着的(0代表反,1代表正)。

bfs的做法:

具体思路:bfs,注意起点为0个操作的情况,然后逐步的去找满足题目条件的最优步数。如果是起点是初始状态,然后去找全部都是翻转的情况,这样的话会mle,因为递归层数会明显的比第一种的多。

AC代码:

 #include<iostream>
#include<stack>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
# define ll long long
# define inf 1ll<<
const int mod = ;
const int maxn = <<;
struct node
{
int num;
int step;
node() {}
node(int xx,int yy)
{
num=xx,step=yy;
}
}q[maxn];
int vis[maxn];
bool check(int t)
{
for(int i=; i<; i++)
{
if((<<i)&t)
return false;
}
return true;
}
int bfs(int t)
{
int pre=,last=;
q[last++]=(node(t,));
while(pre<last)
{
node top=q[pre++];
if(check(top.num))
return top.step;
for(int i=; i<; i++)
{
int tmp=top.num;
if(!(tmp&(<<i)))continue;
if(i==)
{
tmp^=(<<);
tmp^=(<<);
}
else if(i==)
{
tmp^=(<<);
tmp^=(<<);
}
else
{
tmp^=(<<i);
tmp^=(<<(i-));
tmp^=(<<(i+));
}
if(vis[tmp])
continue;
q[last++]=node(tmp,top.step+);
}
}
return false;
}
int main()
{
//freopen("data1.out","r",stdin);
int s=,tmp;
for(int i=; i<; i++)
{
scanf("%d",&tmp);
if(tmp)
s|=(<<i);
}
int ans=bfs(s);
printf("%d\n",ans);
return ;
}

高斯消元的方法:

就是构建20个方程,每一个方程的构建就和题目中的一样,最后得出的所有x的解就是最终答案。

AC代码:

 #include<iostream>
#include<stack>
#include<cstring>
#include<iomanip>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
# define ll long long
# define inf 0x3f3f3f3f
const int mod = ;
const int maxn = ;
int a[maxn][maxn];
int equ,var;
int b[maxn][maxn];
int x[maxn];
int free_x[maxn];
int free_num;
int Gass()
{
int max_r,col,k;
free_num=;
for(k=,col=; k<equ&&col<var; k++,col++)
{
max_r=k;
for(int i=k+; i<equ; i++)
{
if(abs(a[i][col])>abs(a[max_r][col]))
max_r=i;
}
if(a[max_r][col]==)
{
k--;
free_x[free_num++]=col;
continue;
}
if(max_r!=k)
{
for(int j=col; j<var+; j++)
swap(a[k][j],a[max_r][j]);
}
for(int i=k+; i<equ; i++)
{
if(a[i][col]!=)
{
for(int j=col; j<var+; j++)
a[i][j]^=a[k][j];
}
}
}
for(int i=k; i<equ; i++)
if(a[i][col]!=)
{
return -;
}
if(k<var)
return var-k;
for(int i=var-; i>=; i--)
{
x[i]=a[i][var];
for(int j=i+; j<var; j++)
x[i]^=(a[i][j]&&x[j]);
}
return ;
}
int n;
void init()
{
memset(x,,sizeof(x));
memset(a,,sizeof(a));
equ=;
var=;
for(int i=; i<; i++)
{
a[i][i]=;
if(i>)
a[i-][i]=;
if(i<-)
a[i+][i]=;
}
}
int solve()
{
int t=Gass();
if(t==-)
{
return t;
}
else if(t==)
{
int ans=;
for(int i=; i<n*n; i++)
ans+=x[i];
return ans;
}
else
{
int ans=inf;
int tot=(<<t);
for(int i=; i<tot; i++)
{
int cnt=;
for(int j=; j<t; j++)
{
if(i&(<<j))
{
cnt++;
x[free_x[j]]=;
}
else
{
x[free_x[j]]=;
}
}
for(int j=var-t-; j>=; j--)
{
int dex;
for(dex=j; dex<var; dex++)
if(a[j][dex])
break;
x[dex]=a[j][var];
for(int l=dex+; l<var; l++)
{
if(a[j][l])
x[dex]^=x[l];
}
cnt+=x[dex];
}
ans=min(ans,cnt);
}
return ans;
}
}
int main()
{
// freopen("hqx.txt","r",stdin);
init();
int tmp;
for(int i=; i<; i++)
{
scanf("%d",&tmp);
a[i][]=tmp;
}
int t=solve();
printf("%d\n",t);
return ;
}

A - The Water Bowls POJ - 3185 (bfs||高斯消元)的更多相关文章

  1. POJ 2947-Widget Factory(高斯消元解同余方程式)

    题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...

  2. poj 2065 SETI 高斯消元

    看题就知道要使用高斯消元求解! 代码如下: #include<iostream> #include<algorithm> #include<iomanip> #in ...

  3. POJ 2065 SETI [高斯消元同余]

    题意自己看,反正是裸题... 普通高斯消元全换成模意义下行了 模模模! #include <iostream> #include <cstdio> #include <c ...

  4. POJ.2065.SETI(高斯消元 模线性方程组)

    题目链接 \(Description\) 求\(A_0,A_1,A_2,\cdots,A_{n-1}\),满足 \[A_0*1^0+A_1*1^1+\ldots+A_{n-1}*1^{n-1}\equ ...

  5. poj The Clocks 高斯消元

    由于数据量不大,所以这题有很多解法. 我用的是高斯消元化为逆矩阵解决的…… 代码如下: #include<stdio.h> #include<iostream> using n ...

  6. Greedy:The Water Bowls(POJ 3185)

    水池 题目大意:给定一个20的数组,全都是0和1,可以翻一个数改变成另一个数(0或者1),但是其左右两边的数都会跟着变为原来的相反数,问你怎么用最小的操作数使全部数变成0 这一题的:满足 1:翻转次序 ...

  7. POJ 2065 SETI (高斯消元 取模)

    题目链接 题意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'*'对应0,‘a’对应1,‘b’对应2.... 例如str[] = "abc&quo ...

  8. poj 3744 矩阵 高斯消元

    着实被批评了一下,自己的数论确实太烂了. 题意:一条路上,有n个炸弹,给出每个炸弹的位置,一次走一步的概率是p,走两步的概率是1-p.求安全走完的概率. 定义dp[i] = dp[i-1]*p + d ...

  9. POJ 1830 【高斯消元第一题】

    首先...使用abs()等数学函数的时候,浮点数用#include<cmath>,其它用#include<cstdlib>. 概念: [矩阵的秩] 在线性代数中,一个矩阵A的列 ...

随机推荐

  1. Python面向对象静态方法,类方法,属性方法

    Python面向对象静态方法,类方法,属性方法 属性: 公有属性 (属于类,每个类一份) 普通属性 (属于对象,每个对象一份) 私有属性 (属于对象,跟普通属性相似,只是不能通过对象直接访问) 方法: ...

  2. Ubuntu16.04 ERROR 1698 (28000): Access denied for user 'root'@'localhost' 解决流程

    mysql版本 5.7.22 安装完成后出现问题 ERROR 1698 (28000): Access denied for user 'root'@'localhost' 可能是因为初始密码为空:按 ...

  3. poj 2114 Boatherds (树分治)

    链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...

  4. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) A,B,C

    A.题目链接:http://codeforces.com/contest/828/problem/A 解题思路: 直接暴力模拟 #include<bits/stdc++.h> using ...

  5. tensorflow_mmp

    搭建win10+py3.6+cuda9.1+cudnn7+tf1.5(如果想知道为什么这么搭配,自己去装两天就知道了) tensorflow_gpu......whl下载 剩下的官网下载,cuda9. ...

  6. NOIP2013题解

    NOIP2013题解 Day1 转圈游戏 circle 快速幂模板题. #include<iostream> using namespace std; int n,m,k,x; int f ...

  7. 【AGC016E】Poor Turkeys

    Description 有\(n\)(\(1 \le n \le 400\))只鸡,接下来按顺序进行\(m\)(\(1 \le m \le 10^5\))次操作.每次操作涉及两只鸡,如果都存在则随意拿 ...

  8. 洛谷 P3253 [JLOI2013]删除物品 解题报告

    P3253 [JLOI2013]删除物品 题目描述 箱子再分配问题需要解决如下问题: (1)一共有\(N\)个物品,堆成\(M\)堆. (2)所有物品都是一样的,但是它们有不同的优先级. (3)你只能 ...

  9. luogu1850 [NOIp2016]换教室 (floyd+dp)

    首先floyd求出每两点间的距离(注意自己到自己的距离要设成0) 然后就是dp了 一开始照着Lifeguards的样子,钦定了一下i这个点一定要选,然后发现复杂度不对,还想了好长时间优化 然后一翻题解 ...

  10. 部署kubernetes1.8.3高可用集群

    Kubernetes作为容器应用的管理平台,通过对pod的运行状态进行监控,并且根据主机或容器失效的状态将新的pod调度到其他node上,实现了应用层的高可用. 针对kubernetes集群,高可用性 ...