POJ 1681 Painter's Problem (高斯消元)

题意

和POJ的1222差不多,把一个格子变色然后相邻的4个也会变色,求令其全部变成黄色需要的最少步数。

思路

POJ1222当时直接状态压缩枚举过的,这题数据也比较小,应该可以状压DP来做,不过复杂度而论当然还是高斯消元要好。
我们可以先预处理出一个 n2n2 的方程组然后搞死小圆(误
消元的时候求出自由变元存在数组里,然后状态压缩来枚举自由变元求一下利用这些自由变元求出目标状态(全0)的话操作数是多少,即x[i]等于1的个数,取最小值就可以了。

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define LL long long
#define Lowbit(x) ((x)&(-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1|1
#define MP(a, b) make_pair(a, b)
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int maxn = 1e5 + 10;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef pair<int, int> pii;
char s[22];
int a[300][300];
int x[300], free_x[300];

int gauss(int equ, int var)
{
    int free_num = 0;
    int k, col, max_r;
    for (k = 0, col = 0; k < equ && col < var; k++, col++)
    {
        max_r = k;
        for (int i = k + 1; i < equ; i++)
            if (abs(a[i][col]) > abs(a[max_r][col])) max_r = i;
        if (max_r != k)
        {
            for (int j = k; j < var + 1; j++)
                swap(a[k][j], a[max_r][j]);
        }
        if (a[k][col] == 0)
        {
            k--;
            free_x[free_num++] = col;
            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; i < equ; i++)
        if (a[i][col] != 0) return -1;
    int sta = 1 << (var - k);   //枚举自由变元
    int ans = INF;
    for (int i = 0; i < sta; i++)
    {
        //printf("%d\n", i);
        int cnt = 0;
        int index = i;
        for (int j = 0; j < var - k; j++)
        {
            if ((1 << j) & i)
            {
                cnt++;
                x[free_x[j]] = 1;
            }
            else
                x[free_x[j]] = 0;
        }
        for (int j = k - 1; j >= 0; j--)
        {
            x[j] = a[j][var];
            for (int l = j + 1; l < var; l++)
                if (a[j][l]) x[j] ^= x[l];
            if (x[j]) cnt++;
        }
        ans = min(ans, cnt);
    }
    return ans;
}

void init(int n)
{
    memset(a, 0, sizeof(a));
    memset(x, 0, sizeof(x));
    memset(free_x, 1, sizeof(free_x)); //不确定的变元
    for (int i = 0; i < n; i++)        //构造方程组
        for (int j = 0; j < n; j++)
        {
            int t = i * n + j;
            a[t][t] = 1;
            if (j < n-1) a[i*n+j+1][t] = 1;
            if (i < n-1) a[(i+1)*n+j][t] = 1;
            if (i > 0) a[(i-1)*n+j][t] = 1;
            if (j > 0) a[i*n+j-1][t] = 1;
        }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int n;
        scanf("%d", &n);
        //getchar();
        init(n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", s);
            for (int j = 0; j < n; j++)
            {
                if (s[j] == 'y') a[i*n+j][n*n] = 0;
                else a[i*n+j][n*n] = 1;
            }
        }
        int ans = gauss(n*n, n*n);
        if (ans == -1)
            printf("inf\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(poj)