任意门:http://poj.org/problem?id=3185

The Water Bowls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7676   Accepted: 3036

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]

Source

题意概括:

N个开关,打开一个开关相邻的开关状态会取反,给一个初始的所有开关状态,要求求出最小的改变开关的次数使得所有开关的状态为关闭;

解题思路:

构造增广矩阵类似于根据开关的关系构造有向图的邻接矩阵;

构造增广矩阵,高斯消元,枚举自由元(二进制枚举状态),寻找最小值;

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = ;
int a[MAXN][MAXN]; //增广矩阵
int freeX[MAXN]; //自由元
int x[MAXN]; //解集
int equ, var;
int free_num;
int N; int Gauss()
{
int maxRow, col, k;
free_num = ;
for(k = , col = ; k < equ && col < var; k++, col++){
maxRow = k;
for(int i = k+; i < equ; i++){
if(abs(a[i][col]) > abs(a[maxRow][col])){
maxRow = i;
}
} if(a[maxRow][col] == ){
k--;
freeX[free_num++] = col;
continue;
}
if(maxRow != k){
for(int j = col; j < var+; j++){
swap(a[k][j], a[maxRow][j]);
}
} for(int i = k+; i < equ; i++){
if(a[i][col] != ){
for(int j = col; j < var+; j++)
a[i][j] ^= a[k][j]; }
}
} for(int i = k; i < equ; i++) //无解
if(a[i][col] != ) return -; if(k < var) return var-k; //多解返回自由元个数 for(int i = var-; i >= ; i--){ //唯一解,回代
x[i] = a[i][var];
for(int j = i+; j < var; j++){
x[i] ^= (a[i][j] && x[j]);
}
}
return ;
} void solve()
{
int t = Gauss();
if(t == -){ //无解的情况,其实题目保证有解
printf("inf\n");
return;
}
else if(t == ){ //唯一解
int ans = ;
for(int i = ; i < N; i++){
ans += x[i];
}
printf("%d\n", ans);
return;
}
else{ //多解,枚举自由元
int ans = INF;
int tot = (<<t);
for(int i = ; i < tot; i++){
int cnt = ;
for(int j = ; j < t; j++){
if(i&(<<j)){
x[freeX[j]] = ;
cnt++;
}
else x[freeX[j]] = ;
} for(int j = var-t-; j >= ; j--){
int index;
for(index = j; index < var; index++)
if(a[j][index])
break;
x[index] = a[j][var]; for(int s = index+; s < var; s++)
if(a[j][s])
x[index] ^= x[s];
cnt += x[index];
}
ans = min(ans, cnt);
}
printf("%d\n", ans);
}
return;
} int main()
{
N = ;
equ = ;
var = ;
memset(a, , sizeof(a));
memset(x, , sizeof(x));
for(int i = ; i < N; i++){
a[i][i] = ;
if(i > ) a[i-][i] = ;
if(i < N-) a[i+][i] = ;
}
for(int i = ; i < N; i++){
scanf("%d", &a[i][N]);
}
solve();
return ;
}

POJ 3185 The Water Bowls 【一维开关问题 高斯消元】的更多相关文章

  1. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  2. poj 3185 The Water Bowls

    The Water Bowls 题意:给定20个01串(最终的状态),每个点变化时会影响左右点,问最终是20个0所需最少操作数? 水题..直接修改增广矩阵即可:看来最优解不是用高斯消元(若是有Gaus ...

  3. POJ 3185 The Water Bowls (高斯消元)

    题目链接 题意:翻译过来就是20个0或1的开关,每次可以改变相邻三个的状态,问最小改变多少次使得所有开关都置为0,题目保证此题有解. 题解:因为一定有解,所以我们可以正序逆序遍历两次求出较小值即可.当 ...

  4. POJ 1830 开关问题 高斯消元,自由变量个数

    http://poj.org/problem?id=1830 如果开关s1操作一次,则会有s1(记住自己也会变).和s1连接的开关都会做一次操作. 那么设矩阵a[i][j]表示按下了开关j,开关i会被 ...

  5. POJ - 1681: Painter's Problem (开关问题-高斯消元)

    pro:开关问题,同上一题. 不过只要求输出最小的操作步数,无法完成输出“inf” sol:高斯消元的解对应的一组合法的最小操作步数. #include<bits/stdc++.h> #d ...

  6. POJ - 1222: EXTENDED LIGHTS OUT (开关问题-高斯消元)

    pro:给定5*6的灯的状态,如果我们按下一个灯的开关,它和周围4个都会改变状态.求一种合法状态,使得终状态全为关闭: sol:模2意义下的高斯消元. 终于自己手打了一个初级板子. #include& ...

  7. poj 3185 The Water Bowls(反转)

    Description The cows have a line of water bowls water bowls to be right-side-up and thus use their w ...

  8. POJ 3185 The Water Bowls (高斯消元 求最小步数)

    题目链接 题意:有20个数字,0或1.如果改变一个数的状态,它左右两边的两个数的状态也会变反.问从目标状态到全0,至少需要多少次操作. 分析: 和上一题差不多,但是比上一题还简单,不多说了,但是在做题 ...

  9. poj 3185 The Water Bowls 高斯消元枚举变元

    题目链接 给一行0 1 的数, 翻转一个就会使他以及它左右两边的都变, 求最少多少次可以变成全0. 模板题. #include <iostream> #include <vector ...

随机推荐

  1. Java调度线程池ScheduleExecutorService

    如果在一个ScheduleExecutorService中提交一个任务,这个任务的调度周期设置 的时间比任务本身执行的时间短的话会出现什么情况?也就是在线程调度时间已经到了 但是上次的任务还没有做完的 ...

  2. PIXI AnimatedSprite 及打字爆炸动画(5)

    效果 : 消除字母 当前位置出现爆炸效果 这里使用到了AnimatedSprite 动画  Members An AnimatedSprite is a simple way to display a ...

  3. 97 条 Linux 运维工程师常用命令总结[转]

    1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件 -A 通-a,但不列出"."和".." -l 列出 ...

  4. async/await 的一些知识 (死锁问题)

    博文 Don't Block on Async Code What is the purpose of "return await" in C#? Any difference b ...

  5. 32位x86处理器编程导入——《x86汇编语言:从实模式到保护模式》读书笔记08

    在说正题之前,我们先看2个概念. 1.指令集架构(ISA) ISA 的全称是 instruction set architecture,中文就是指令集架构,是指对程序员实际"可见" ...

  6. 使用pygame开发一个弹幕射击游戏(一)

    本文作为开发过程记录用. 目前游戏画面: 下一个添加的功能:敌机可以进行射击. 弹幕类 from pygame.sprite import Sprite from pygame import tran ...

  7. [转]Razor里写函数

    本文转自:http://blog.csdn.net/leftfist/article/details/43243177 asp.net mvc的视图里使用Razor来书写服务器代码,人尽皆知.可以常常 ...

  8. C#动态方法调用 提高程序的扩展性

    此篇将介绍C#如何在运行时动态调用方法.当某些类型是运行时动态确定时,编译时的静态编码是无法解决这些动态对象或类的方法调用的.此篇则给你一把利剑,让动态对象的方法调用成为可能. 1.动态调用dll里的 ...

  9. Java集合篇四:Map的基本应用

    package com.test.collection; import java.util.HashMap; import java.util.Hashtable; import java.util. ...

  10. CRM——讲师与学生

    一.课程记录和学习记录 1.初始化 course_record, study_record.2.学习记录3.录入成绩4.显示成绩 ajax 查询 柱状图展示成绩 highcharts 5.上传作业(o ...