POJ 3185 The Water Bowls (高斯消元)
来源:互联网 发布:国外网盘推荐 知乎 编辑:程序博客网 时间:2024/05/21 10:09
传送门
The Water Bowls
Time Limit: 1000MSMemory Limit: 65536KTotal 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?
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]
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]
题目大意:
有
当翻转一个杯子的时候与它相邻的两个杯子也会翻转,现在问你的是,经过最少几步能够将初始状态的杯子转化成全部是倒扣着的杯子,输出最小步数。
解题思路:
这就是一个简单的高消,首先构造出系数矩阵,然后高消,发现有自由变量,然后枚举自由变量得到最优解,都是套路题,基本上没什么好说的。
/**2016 - 09 - 09 晚上Author: ITAKMotto:今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。**/#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;}
0 0
- poj 3185 The Water Bowls 高斯消元
- poj -- 3185 The Water Bowls(高斯消元)
- poj 3185 The Water Bowls(高斯消元)
- poj 3185 The Water Bowls(高斯消元)
- POJ 3185 The Water Bowls(高斯消元)
- poj 3185 The Water Bowls (高斯消元)
- POJ 3185 The Water Bowls(高斯消元)
- POJ 3185 The Water Bowls (高斯消元)
- poj 3185 The Water Bowls(高斯消元)
- poj 3185 The Water Bowls
- POJ 3185 (The Water Bowls)
- POJ 3185 The Water Bowls
- poj 3185 The Water Bowls
- POJ 3185 The Water Bowls
- POJ 3185 The Water Bowls
- POJ-3185-The Water Bowls
- poj-3185 The Water Bowls
- The Water Bowls POJ 3185
- JSP学习之jsp指令
- C++ 两个数的绝对值之和
- 创建博客-评论部分(提交和显示)
- RecycleView实现瀑布流
- 跟我一起学OC系列-开篇
- POJ 3185 The Water Bowls (高斯消元)
- Appium+Python环境的搭建(Windows)
- EventBus使用详解(一)——初步使用EventBus
- 错误[Error] ld returned 1 exit status
- sublime3 安装 anaconda 插件
- Codeforces-689C-Mike and Chocolate Thieves
- 路径中 斜杠/和反斜杠\ 的区别
- JSP中的三大乱码问题
- springmvc和struts2的区别