Poj 1681 painter ' s Problem Gaussian elimination + shape compression Enumeration Free variable _ state compression

Source: Internet
Author: User
Painter ' s Problem
Time Limit: 1000MS Memory Limit: 10000K
Total submissions: 5075 accepted: 2458

Description There is a square wall which is made to n*n small square bricks. Some bricks are white while Some bricks are. Bob is a painter and him wants to paint the bricks yellow. But there is something wrong with Bob ' s brush. Once he uses this brush to paint brick (i, j), the bricks at (I, j), (I-1, J), (I+1, J), (I, j-1) and (I, j+1) all their color. Your task is to find the minimum number of bricks Bob should paint into order to make all the bricks yellow.

Input the "a single Integer T" (1 <= t <=) that indicates the number of test cases. Then follow the t cases. Each test case begins with a line contains an integer n (1 <= n <=), representing the size of wall. The next n lines represent the original wall. Each line contains n characters. The j-th character of the i-th line figures out the color of brick at position (I, J). We use a ' W ' to express a white brick while a ' y ' to express a yellow brick.

Output a line contains the minimum number of bricks Bob should paint. If Bob can ' t paint all the bricks yellow, print ' inf '.

Sample Input

2
3
yyy
yyy
yyy
5
wwwww
wwwww
wwwww wwwww wwwww

Sample Output

0

15

To give you a n*n matrix, each position corresponds to a character, y represents the position is yellow, W represents the position is white. If you choose to cover the position (I, j), the color of the position (I, J) is Huang (or by Bai Changhuang), and the effect affects the positions (I-1, J), (I, J-1), (I+1, J), (I, j+1). Now let you choose the least coverage position so that all the positions on the matrix are yellow, if you can reach the target output of the minimum number of coverage, if the target output INF is not achieved.

Idea: N*n an equation, n*n a variable, and solve the free variable. The state compresses the free variable and maintains the minimum value.

The equations are well listed, and the initial state and end state topics have been given.

--(related position) *1 ^ (non-relevant position) *0 = Initial state ^ End state

AC Code:

#include <cstdio> #include <cstring> #include <algorithm> #define MAXN #define INF 0x3f3f3f3f using
namespace Std;
Char STR[MAXN][MAXN];
int A[MAXN][MAXN];
BOOL free_x[maxn];//tag is free variable int free_rec[maxn];//record free variable int x[maxn];//solution set int equ, VAR;
int n;
    void Init_a () {scanf ("%d", &n);
    Equ = var = n*n;
    memset (A, 0, sizeof (a));
        for (int i = 0; i < n; i++) {scanf ('%s ', str[i]);
            for (int j = 0; J < N; j +) {int num = i*n+j;
            if (str[i][j] = = ' Y ') a[num][var] = 0^0;
            else A[num][var] = 0^1;
            A[num][num] = 1;
            if (i > 0) a[num][num-n] = 1;
            if (J > 0) a[num][num-1] = 1;
            if (i < n-1) a[num][num+n] = 1;
        if (J < n-1) a[num][num+1] = 1;
    ()} int Gauss () {int max_r, K;
    int col = 0; int num = 0;//free variable number for (k = 0; k < equ && Col < var; k++, col++) {
        Max_r = k;
        for (int i = k+1 i < equ i++) if (A[i][col] > A[max_r][col]) max_r = i;
        if (Max_r!= k) for (int i = col; i < var+1; i++) Swap (a[k][i), a[max_r][i]);
            if (a[k][col] = = 0) {k--;
        free_rec[num++] = col;//record free variable continue; for (int i = k+1 i < equ i++) if (A[i][col]!= 0) for (int j = col; j < Var+1;
    J + +) A[i][j] ^= a[k][j];
    for (int i = k+1 i < equ; i++) if (A[i][col]!= 0)//k+1 no solution in Equ-1-col rows with a non-0 occurrence return-1;//
if (K < Var) return var-k;//Returns the number of free variable returned 0;
    } void Solve (int S) {int state = (1<<s);
    int ans = INF;
        for (int i = 0; i < state; i++)//Makeup Press Enumerate all states {int cnt = 0;
 for (int j = 0; J < S; j)//Enumerate the free variable and determine the solution of the free variable {if ((1<<j) & i) {               cnt++;
            X[FREE_REC[J]] = 1;
        else X[free_rec[j]] = 0; }//In 0 ~ k-1 line to form the upper triangular matrix and 00 ...
            0 lines will only appear in K ~ equ-1 for (int j = var-s-1 J >= 0; j--) {int temp = a[j][var];//This line corresponds to the final result of the equation
                    Derivation for (int l = j+1 L < var; l++)//To solve the variable if (a[j][l) according to the determined variable.
            Temp ^= x[l]; X[J] = temp;//The solution of the variable cnt = = X[j]?
        1:0;
    ans = min (ans, CNT);
printf ("%d\n", ans);
    int main () {int t;
    scanf ("%d", &t);
        while (t--) {init_a ();
        int free_num = Gauss ();
        if (Free_num = = 1) printf ("inf\n");
    else solve (free_num);
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.