POJ 3185 The Water Bowls (高斯消元)

传送门

The Water Bowls
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 5946Accepted: 2346

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]

题目大意:

20 个杯子排成一排,每个杯子只有两种状态,一种是正面朝上的,另一种是杯子倒扣在桌子上的,现在给你一个初始状态,你现在要将所有的杯子都变成倒扣着的,

当翻转一个杯子的时候与它相邻的两个杯子也会翻转,现在问你的是,经过最少几步能够将初始状态的杯子转化成全部是倒扣着的杯子,输出最小步数。

解题思路:

这就是一个简单的高消,首先构造出系数矩阵,然后高消,发现有自由变量,然后枚举自由变量得到最优解,都是套路题,基本上没什么好说的。

My Code

/**
2016 - 09 - 09  晚上
Author: ITAK

Motto:

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
**/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9+5;
const int MAXN = 400;
const int MOD = 1e9+7;
const double eps = 1e-7;
const double PI = acos(-1);
using namespace std;
int equ, var;///equ个方程 var个变量
int a[MAXN][MAXN];///增广矩阵
int x[MAXN];///解集
int x_i[MAXN];
bool free_x[MAXN];///判断是不是自由变元
int free_num;///自由变元的个数
int Gauss()
{
    int Max_r;///当前列绝对值最大的存在的行
    ///col:处理当前的列
    int row,col = 0;
    int free_x_num;
    int free_index;
    free_num = 0;
    for(int i=0; i<=var; i++)
    {
        x[i] = 0;
        free_x[i] = 1;
    }
    for(row=0; row<equ&&col<var; row++,col++)
    {
        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(a[Max_r][col] == 0)
        {
            free_x[col] = 1;
            x_i[free_num++] = col;
            row--;
            continue;
        }
        if(Max_r != row)
            for(int i=col; i<var+1; i++)
                swap(a[row][i], a[Max_r][i]);
        ///消元
        for(int i=row+1; i<equ; i++)
            if(a[i][col])
                for(int j=col; j<var+1; j++)
                    a[i][j] ^= a[row][j];
    }
    for(int i=row; i<equ; i++)
        if(a[i][col])
            return -1;///无解
    ///保证对角线主元非 0
    for(int i=0;  i<equ; i++)
    {
        if(!a[i][i])
        {
            int j;
            for(j=i+1; j<var; j++)
                if(a[i][j])
                    break;
            if(j == var)
                break;
            for(int k=0; k<equ; k++)
                swap(a[k][i], a[k][j]);
        }
    }
    if(row < var)
        return var - row;///自由变元的个数
    ///回代,得到解集
    for(int i=var-1; i>=0; i--)
    {
        x[i] = a[i][var];
        for(int j=i+1; j<var; j++)
            x[i] ^= (a[i][j] && x[j]);
    }
    return 0;///唯一解
}
void Debug()
{
    puts("");
    cout<<"+++++++++++++++++++++++++++分界线++++++++++++++++++++++++++++++"<<endl;
    for(int i=0; i<equ; i++)
    {
        for(int j=0; j<var+1; j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    cout<<"+++++++++++++++++++++++++++分界线++++++++++++++++++++++++++++++"<<endl;
    puts("");
}
void Init()
{
    memset(a, 0, sizeof(a));
    memset(x, 0, sizeof(x));
    for(int i=0; i<equ; i++)
    {
        a[i][i] = 1;
        if(i > 0)
            a[i-1][i] = 1;
        if(i < equ)
            a[i+1][i] = 1;
    }
}
int main()
{
    equ = var = 20;
    int xx;
    while(cin>>xx)
    {
        Init();
        a[0][var] = xx;
        for(int i=1; i<equ; i++)
            cin>>a[i][var];
        int tmp = Gauss(), ans = INF;
        for(int i=0; i<(1<<tmp); i++)
        {
            int sum = 0;
            for(int j=0; j<tmp; j++)
            {
                if(i & (1<<j))
                    x[x_i[j]] = 1;
                else
                    x[x_i[j]] = 0;
            }
            for(int ii=var-tmp-1; ii>=0; ii--)
            {
                x[ii] = a[ii][var];
                for(int j=ii+1; j<var; j++)
                    x[ii] ^= (a[ii][j] && x[j]);
            }
            for(int ii=0; ii<var; ii++)
                sum += x[ii];
            ans = min(ans, sum);
        }
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值