POJ 3185 The Water Bowls (高斯消元)
2016-09-10 09:32:09      个评论    来源：ITAK

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 个杯子排成一排，每个杯子只有两种状态，一种是正面朝上的，另一种是杯子倒扣在桌子上的，现在给你一个初始状态，你现在要将所有的杯子都变成倒扣着的，

MyCode：

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

Motto:

**/

#include
#include
#include
#include
#include
#include
#include
#include
#include
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 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=0; i--)
{
x[i] = a[i][var];
for(int j=i+1; j 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>a[i][var];
int tmp = Gauss(), ans = INF;
for(int i=0; i<(1<=0; ii--)
{
x[ii] = a[ii][var];
for(int j=ii+1; j

```