The Water Bowls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4623 | Accepted: 1812 |
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
USACO 2006 January Bronze
题意很简单,每一次改变会改变三个数(自身和左右两侧),问最少多少步可以变为全为0
高斯消元,得出异或方程组,求出自由元,然后进行枚举,找出最小值。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define INF 0x3f3f3f3f int Map[25][25] , a[25] , freex[25] , x[25] ; void swap1(int p,int q) { int i , temp ; temp = a[p] ; a[p] = a[q] ; a[q] = temp ; for(i = 0 ; i < 20 ; i++) { temp = Map[p][i] ; Map[p][i] = Map[q][i] ; Map[q][i] = temp ; } return ; } int solve() { int i , j , k , t = 0 , num1 = 0 ; for(i = 0 ; i < 20 && t < 20 ; i++ , t++) { for(j = i ; j < 20 ; j++) if( Map[j][t] ) break ; if( j == 20 ) { freex[num1++] = t ; i-- ; continue ; } if( i != j ) swap1(i,j) ; for(j = i+1 ; j < 20 ; j++) { if( Map[j][t] ) { a[j] = a[j]^a[i] ; for(k = t ; k < 20 ; k++) Map[j][k] = Map[j][k] ^ Map[i][k] ; } } } for( ; i < 20 ; i++) if( a[i] ) return -1 ; if( num1 > 0 ) return num1 ; for(i = 19 ; i >= 0 ; i--) { x[i] = a[i] ; for(j = i+1 ; j < 20 ; j++) x[i] ^= ( Map[i][j]*x[j] ) ; } return num1 ; } int main() { int i , j , k , min1 = INF , key , ans ; memset(Map,0,sizeof(Map)) ; for(i = 0 ; i < 20 ; i++) scanf("%d", &a[i]) ; for(i = 0 ; i < 20 ; i++) { Map[i][i] = 1 ; if( i-1 >= 0 ) Map[i][i-1] = 1 ; if( i+1 < 20 ) Map[i][i+1] = 1 ; } key = solve() ; if( key == 0 ) { ans = 0 ; for(i = 0 ; i < 20 ; i++) ans += x[i] ; min1 = min(min1,ans) ; } else if( key > 0 ) { int temp = 1 << key , t ; for(t = 0 ; t < temp ; t++) { ans = 0 ; memset(x,0,sizeof(x)) ; for(j = 0 ; j < key ; j++) if( t & (1<<j) ) { x[ freex[j] ] =1 ; ans++ ; } for(i = 19 ; i >= 0 ; i--) { for(k = 0 ; k < 20 ; k++) if( Map[i][k] ) break ; x[k] = a[i] ; for(j = k+1 ; j < 20 ; j++) x[k] ^= ( Map[i][j]*x[j] ) ; ans += x[k] ; } min1 = min(min1,ans) ; } } printf("%d\n", min1) ; return 0; }