POJ 3185 The Water Bowls (Gaussian deyuan)

Source: Internet
Author: User
The Water Bowls
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:3340   Accepted:1298

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 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state]
0 0 0 0 0 0 1 1 1 1 1 0 0 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 0 [after flipping bowl 11]

Source

USACO 2006 January Bronze

[Submit] [Go Back] [Status] [Discuss]

Home Page Go Back To top

 

 

 

Gaussian element ..

A question about Gaussian elimination... Summarize a good template .. All others are food abuse.

 

 

 

/* POJ 1753 */# include <stdio. h> # include <string. h> # include <iostream> # include <algorithm> # include <math. h> using namespace std; const int MAXN = 30; const int INF = 0x3fffffff; int a [MAXN] [MAXN]; // Augmented Matrix int x [MAXN]; int free_x [MAXN]; // Gaussian elimination method (Gauss-Jordan elimination ). (-2 indicates a floating point solution, but no integer solution. //-1 indicates no solution, 0 indicates a unique solution, greater than 0 indicates an infinite solution, and returns the number of free variables) // There are equ equations and var variables. Number of rows in the augmented matrix is equ, 0 to equ-1, number of columns is var + 1, 0 to var.int Gauss (int equ, int var) {int I, j, k; int max_r; // the row with the largest absolute value of the current column. int col; // The int ta, tb; int LCM; int temp; int free_index; int num = 0; for (int I = 0; I <= var; I ++) {x [I] = 0; free_x [I] = 0;} // converts it to a tiered array. col = 0; // The currently processed column for (k = 0; k <equ & col <var; k ++, col ++) {// enumerate the currently processed rows. // find the row with the largest absolute value of the col column element and swap it with row k. (to reduce the error in Division) max_r = k; for (I = k + 1; I <equ; I ++) {if (abs (a [I] [col])> Bs (a [max_r] [col]) max_r = I;} if (max_r! = K) {// exchange with row k. for (j = k; j <var + 1; j ++) swap (a [k] [j], a [max_r] [j]);} if (a [k] [col] = 0) {// indicates that the column k contains all 0, and the next column of the current row is processed. k --; free_x [num ++] = col; continue;} for (I = k + 1; I <equ; I ++) {// enumerate the rows to be deleted. if (a [I] [col]! = 0) {// LCM = lcm (abs (a [I] [col]), abs (a [k] [col]); // ta = LCM/abs (a [I] [col]); // tb = LCM/abs (a [k] [col]); // if (a [I] [col] * a [k] [col] <0) tb =-tb; // if the difference is an addition for (j = col; j <var + 1; j ++) {a [I] [j] ^ = a [k] [j] ;}}// 1. solution-free: the simplified augmented array exists (0, 0 ,..., a) such a row (! = 0 ). for (I = k; I <equ; I ++, record exchange. if (a [I] [col]! = 0) return-1;} int stat = 1 <(var-k); // The free variable has var-k int res = INF; for (I = 0; I <stat; I ++) // enumerate all variables {int cnt = 0; int index = I; for (j = 0; j <var-k; j ++) {x [free_x [j] = (index & 1); if (x [free_x [j]) cnt ++; index >>=1 ;} for (j = K-1; j> = 0; j --) {int tmp = a [j] [var]; for (int l = j + 1; l <var; l ++) if (a [j] [l]) tmp ^ = x [l]; x [j] = tmp; if (x [j]) cnt ++;} if (cnt <res) res = cnt;} return res;} void init () {for (int I = 0; I <20; I ++) {for (int j = 0; j <20; j ++) a [I] [j] = 0; a [I] [I] = 1; if (I> 0) a [I] [I-1] = 1; if (I <19) a [I] [I + 1] = 1 ;}} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout); while (scanf ("% d ", & a [0] [20])! = EOF) {init (); for (int I = 1; I <20; I ++) scanf ("% d", & a [I] [20]); int ans = Gauss (20, 20); printf ("% d \ n", ans);} return 0 ;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.