hdu 4200 gauss+DFS

关灯问题,问最少需要开关数(开关种类有多个)

高斯消元


/**
hdu 4200 bad writing
高斯消元+DFS

若满足n > 2D + 1,则矩阵的秩 >= n;故dfs的规模为2^(D+1)
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;

#define N 101
int n,D,g[N][N],ans[N];
int row,col,res;

void dfs(int cur)
{
    if(cur == n)
    {
        int i,j,c;
        for(i = row - 1;i >= 0; --i)
        {
            c = g[i][n];
            for (j = i+1; j < n;++j)
                c ^= ans[j]&&g[i][j];
            ans[i] = c;
        }
        c = 0;
        for(i = 0;i < n; ++i)
            c += ans[i];
        res = min(c,res);
    }
    else
    {
        ans[cur] = 0;
        dfs(cur + 1);
        ans[cur] = 1;
        dfs(cur + 1);
    }
}
int gauss()
{
    int i,j,k;
    for(row = 0,col = 0;col < n;++col)
    {
        for(i = row ;i < n;++i)
            if(g[i][col])
                break;
        if(i == n)
            continue;
        if(i != row)
        {
            for(j = 0;j <= n;++j)
                swap(g[i][j],g[row][j]);
        }
        for(i = row + 1;i < n;++i)
            if(g[i][col])
                for( j= col;j<=n;++j)
                    g[i][j] ^= g[row][j];
        ++row;
    }
    for(i = row ;i < n; ++i)
        if(g[i][n])
            return -1;

    for(i = 0; i < row; ++i)///每一行主元素化为非零
        if(!g[i][i])
        {
            for(j = i+1;j<n;++j)
                if(g[i][j])
                    break;
            if(j == n)
                break;
            for(k = 0;k < n; ++k)
                swap(g[k][i],g[k][j]);
        }

    res = 101;
    dfs(row);
    return res;
}


int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&D);
        int l,r,i,j;
        memset(g,0,sizeof(g));

        for(i = 0; i < n;++i)
        {
            scanf("%d",&g[i][n]);
            l = max(0,i - D);
            r = min(n-1,i + D);
            for(j = l; j <= r; ++j)
                g[j][i] = 1;
        }
        int ans = gauss();
        printf(ans == -1 ?"impossible\n":"%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(hdu 4200 gauss+DFS)