频道栏目
首页 > 程序开发 > 软件开发 > 其他 > 正文
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
        
   
点击复制链接 与好友分享!回本站首页
上一篇:Leetcode 46 Permutations
下一篇:Cats(3)- freeK-Free编程更轻松
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站