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

来源:互联网 发布:大数据薪资待遇 编辑:程序博客网 时间:2024/06/05 04:00

很明了的开关问题,但是会有多种解的情况所以要枚举一下变元,求的一个最优的结果。和上一篇的思路与过程都很形似。

PS:第三百篇博客,得继续努力。

The Water Bowls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4302 Accepted: 1696

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]
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-8#define M 1000100//#define LL __int64#define LL long long#define INF 0x7ffffff#define PI 3.1415926535898#define mod 2const int maxn = 210;using namespace std;int a[maxn][maxn];int x[maxn];int equ, var;int x1[maxn], x2[maxn];int free_x[maxn];int free_num;int f[maxn];char str;int LCM(int a, int b){    return (a/(__gcd(a, b)))*b;}int Gauss(){    int row, col, max_r;    int num = 0;    row = col = 0;    while(row < equ && col < var)    {        max_r = row;        for(int i = row+1; i < equ; i++)            if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i;        if(max_r != row)            for(int j = col; j <= var; j++) swap(a[row][j], a[max_r][j]);        if(a[row][col] == 0)        {            free_x[num++] = col;            col++;            continue;        }        for(int i = row+1; i < equ; i++)        {            if(!a[i][col]) continue;            int l = LCM(abs(a[row][col]), abs(a[i][col]));            int ta = l/a[i][col];            int tb = l/a[row][col];            if(ta*tb < 0) tb *= -1;            for(int j = col; j <= var; j++)                a[i][j] = ((a[i][j]*ta-a[row][j]*tb)%mod+mod)%mod;        }        row++;        col++;    }    for(int i = row; i < equ; i++)        if(a[i][col] != 0) return INF;    if(var == row)    {        for(int i = var-1; i >= 0; i--)        {            int tmp = a[i][var];            for(int j = i+1; j < var; j++)                if(a[i][j] != 0) tmp = ((tmp-a[i][j]*x[j])%mod+mod)%mod;            while(tmp%a[i][i] != 0)                tmp += mod;            x[i] = tmp/a[i][i]%mod;        }        int cnt = 0;        for(int i = 0; i < 20; i++)            cnt += x[i];        return cnt;    }    int res = INF;    int t = var-row;    int n = 1<<t;    for(int i = 0; i < n; i++)    {        int cnt = 0;        for(int j = 0; j < t; j++)        {            if(i&(1<<j))            {                x[free_x[j]] = 1;                cnt++;            }            else x[free_x[j]] = 0;        }        for(int j = row-1; j >= 0; j--)        {            int xx;            for(xx = j; xx < var; xx++)                if(a[j][xx]) break;            x[xx] = a[j][var];            for(int k = xx+1; k < var; k++)                if(a[j][k]) x[xx] ^= x[k];            cnt += x[xx];        }        res = min(res, cnt);    }    return res;}void init(){    equ = var = 20;    memset(x, 0, sizeof(x));    memset(a, 0, sizeof(a));}int main(){    init();    for(int i = 0; i < 20; i++) x1[i] = 0;    for(int i = 0; i < 20; i++)    {        scanf("%d",&a[i][20]);        a[i][20] ^= x1[i];    }    for(int i = 0; i < 20; i++)    {        a[i][i] = 1;        if(i-1 >= 0) a[i-1][i] = 1;        if(i+1 < 20) a[i+1][i] = 1;    }    int flag = Gauss();    cout<<flag<<endl;    return 0;}


0 0
原创粉丝点击