[Gauss]POJ3185 The Water Bowls

题意:反正就是要给的一串01的变成全0 能影响自己和左右 最少需要几步

01方程组 异或解

  1 int a[300][300];  // 增广矩阵
  2 int x[300];  // 解
  3 int free_x[300]; // 标记是否为自由未知量
  4
  5 int n;
  6 void debug()
  7 {
  8     for(int i=0;i<n*n;i++)
  9     {
 10         for(int j=0;j<n*n;j++)
 11             printf("%d ", a[i][j]);
 12         printf("\n");
 13     }
 14 }
 15
 16 int Gauss(int n, int m) // n个方程 m个未知数 即 n行m+1列
 17 {
 18     //转换为阶梯形式
 19     int col=0, k, num=0;
 20     for(k=0;k<n && col<m;k++, col++)
 21     {//枚举行
 22         int max_r=k;
 23         for(int i=k+1;i<n;i++)//找到第col列元素绝对值最大的那行与第k行交换
 24             if(abs(a[i][col])>abs(a[max_r][col]))
 25                 max_r=i;
 26         if(max_r!=k)// 与第k行交换
 27             for(int j=col;j<m+1;j++)
 28                 swap(a[k][j], a[max_r][j]);
 29         if(!a[k][col])// 说明该col列第k行以下全是0了
 30         {
 31             k--;
 32             free_x[num++]=col;
 33             continue;
 34         }
 35         for(int i=k+1;i<n;i++)// 枚举要删除的行
 36             if(a[i][col])
 37                 for(int j=col;j<m+1;j++)
 38                     a[i][j]^=a[k][j];
 39     }
 40
 41 //    debug();
 42 //    printf("%d %d\n", col, k);
 43
 44     for(int i=k;i<n;i++)
 45         if(a[i][col])
 46             return -1; // 无解
 47
 48 //    if(k<m)   //m-k为自由未知量个数
 49 //    {
 50         int stat=1<<(m-k);
 51         int ans=INT_MAX;
 52         for(int i=0;i<stat;i++)
 53         {
 54             int cnt=0;
 55             for(int j=0;j<m-k;j++)
 56                 if(i&(1<<j))
 57                 {
 58                     x[free_x[j]]=1;
 59                     cnt++;
 60                 }
 61                 else
 62                     x[free_x[j]]=0;
 63             for(int j=k-1;j>=0;j--)
 64             {
 65                 int tmp;
 66                 for(tmp=j;tmp<m;tmp++)
 67                     if(a[j][tmp])
 68                         break;
 69                 x[tmp]=a[j][m];
 70                 for(int l=tmp+1;l<m;l++)
 71                     if(a[j][l])
 72                         x[tmp]^=x[l];
 73                 cnt+=x[tmp];
 74             }
 75             if(cnt<ans)
 76                 ans=cnt;
 77         }
 78         return ans;
 79 //    }
 80
 81 //    //  唯一解 回代
 82 //    for(int i=m-1;i>=0;i--)
 83 //    {
 84 //        x[i]=a[i][m];
 85 //        for(int j=i+1;j<m;j++)
 86 //            x[i]^=(a[i][j] && x[j]);
 87 //    }
 88 //    int ans=0;
 89 //    for(int i=0;i<n*n;i++)
 90 //        ans+=x[i];
 91 //    return ans;
 92 }
 93
 94
 95 void init()
 96 {
 97     n=20;
 98     memset(a, 0, sizeof(a));
 99     memset(x, 0, sizeof(x));
100     for(int i=0;i<n;i++)
101     {
102         a[i][i]=1;
103         if(i>0)
104             a[i][i-1]=1;
105         if(i<n-1)
106             a[i][i+1]=1;
107     }
108 }
109
110 int main()
111 {
112     int X;
113     while(~scanf("%d", &X))
114     {
115         init();
116         a[0][20]=X;
117         for(int i=1;i<n;i++)
118             scanf("%d", &a[i][n]);
119         int t=Gauss(n, n);
120 //        if(t==-1)
121 //        {
122 //            printf("Oh,it‘s impossible~!!\n");
123 //            continue ;
124 //        }
125         printf("%d\n", t);
126     }
127     return 0;
128 }

POJ 3185

时间: 2024-08-08 06:49:11

[Gauss]POJ3185 The Water Bowls的相关文章

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 ri

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 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

The Water Bowls [POJ3185] [开关问题]

题意 一串长度为20的0,1数列,每次翻转i,会影响i-1,i+1,也被翻转,最少翻转成0的步骤数是多少? 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 分析 这一道开关问题和POJ3276很类似,但是那个是有固定长度的,我们可以看做是一个点向后的一个区间进行翻转,就不会对前面产生影响. 这道题就不一样,会影响前面的,那我们就看做是i为作用点,i+1,i+2为附带效应点,就可以转换成上面那种类型了.只是要

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

Greedy:The Water Bowls(POJ 3185)

水池 题目大意:给定一个20的数组,全都是0和1,可以翻一个数改变成另一个数(0或者1),但是其左右两边的数都会跟着变为原来的相反数,问你怎么用最小的操作数使全部数变成0 这一题的:满足 1:翻转次序不改变结果 2.  从特定次序翻转以后左侧的元素不会再改变 其实就是3276的变形,只是他这次固定变三个数,而且是一前一后,我们把方向dir的查看往前挪一个数就好了,但是这样我们就不能知道第一个数是否需要翻转,所以我们分两种情况来讨论就好了 一开始我想着像3276那样枚举,可是1<<20次实在是太

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

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