poj 3185 The Water Bowls 高斯消元枚举变元

题目链接

给一行0 1 的数, 翻转一个就会使他以及它左右两边的都变, 求最少多少次可以变成全0。

模板题。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int a[50][50], ans[50], x[50];
int n, l[50], free_x[50];
int gauss(int equ,int var)
{
    int i,j,k, max_r, col = 0, temp, free_index, num = 0;
    for(int i=0;i<=var;i++)
    {
        x[i]=0;
        free_x[i]=0;
    }
    for(k = 0;k < equ && col < var;k++,col++)
    {
        max_r=k;
        for(i=k+1;i<equ;i++)
        {
            if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
        }
        if(max_r!=k)
        {
            for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);
        }
        if(a[k][col]==0)
        {
            k--;
            free_x[num++]=col;
            continue;
        }
        for(i=k+1;i<equ;i++)
        {
            if(a[i][col]!=0)
            {
                for(j=col;j<var+1;j++)
                {
                    a[i][j] ^= a[k][j];
                }
            }
        }
    }
    for (i = k; i < equ; i++)
    {
        if (a[i][col] != 0) return -1;
    }
    int stat = 1<<(var-k);
    int res = inf;
    for(i=0;i<stat;i++)
    {
        int cnt=0;
        int index=i;
        for(j=0;j<var-k;j++)
        {
            x[free_x[j]]=(index&1);
            if(x[free_x[j]]) cnt++;
            index>>=1;
        }
        for(j=k-1;j>=0;j--)
        {
            int tmp=a[j][var];
            int t=0;
            while(a[j][t]==0)t++;
            for(int l=t+1;l<var;l++)
              if(a[j][l]) tmp^=x[l];
            x[t]=tmp;
            if(x[t])cnt++;
        }
        if(cnt<res)res=cnt;
    }
    return res;
}
int main()
{
    for(int i = 0; i<20; i++) {
        scanf("%d", &a[i][20]);
    }
    for(int i = 0; i<20; i++) {
        if(i) {
            a[i][i-1] = 1;
        }
        if(i!=19)
            a[i][i+1] = 1;
        a[i][i] = 1;
    }
    int ans = gauss(20, 20);
    cout<<ans<<endl;
    return 0;
}
时间: 2024-10-10 02:46:09

poj 3185 The Water Bowls 高斯消元枚举变元的相关文章

POJ 3185 The Water Bowls 高斯消元

高斯消元+位运算枚举自由变元 #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <set> #include <bitset> #include <queue> #include <stack> #include <string> #include <iostream> #i

poj 3185 The Water Bowls(高斯消元)

The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4352   Accepted: 1721 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

poj3185--The Water Bowls(高斯消元问题3)

The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4623   Accepted: 1812 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

POJ 3185 - The Water Bowls

据网上传闻,用高斯消元解?(我就是在学高斯消元的时候看到有拿这个题当练手题的) 但是,看到discuss上有人说根本不用什么高斯消元和搜索,我一想也是--这题显然用贪心啊-- 首先前提:翻转问题,1.每个碗只有主动翻转一次和不主动翻转两种情况:2.主动翻转碗的顺序对结果没有影响. 于是我们的思路是,强制按照从左到右这个顺序翻碗碗, 那么,如果有个bowl[i]是1(并且bowl[1]--bowl[i-1]都已经是0),我们要把他变成0,只能翻bowl[i+1],否则,若是我们翻bowl[i],就

POJ 3185 The Water Bowls(高斯消元法,枚举自由变元)

题目: The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5013   Accepted: 1960 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 refresh

POJ 1681---Painter&#39;s Problem(高斯消元)

POJ   1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a painter and he wants to paint all the bricks yellow. But there is something

POJ 1681 Painter&#39;s Problem (高斯消元)

题目地址:POJ 1681 跟前两题几乎一模一样的...不多说了.高斯消元+自由元枚举. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include &

POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)

题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现在还只是会套模板,不能独立的思考,好伤心.... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath&g

POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是还保留着一些对工人制造工具的记录,对于每个老员工,他的记录包括,他开始工作的时间(在某个星期的星期几),被炒鱿鱼的时间(某个星期的星期几),在第几个星期不知道.....在这段时间里,他正好加工了k件物品,给出了这k件物品的编号.我们要做的就是通过这些记录,来确定每种工具的加工时间是多少. 分析: 对