LightOJ 1092 Lighted Panels(状压+高斯消元)

来源:互联网 发布:ubuntu tar 编辑:程序博客网 时间:2024/05/21 11:24

Lighted Panels
Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu
Submit

Status

uDebug

Description
You are given an R x C 2D grid consisting of several light panels. Each cell contains either a ‘’ or a ‘.’. ‘’ means the panel is on, and ‘.’ means it’s off. If you touch a panel, its state will be toggled. That means, if you touch a panel that’s on, it will turn off, and if you touch a panel that’s off, it will turn on. But if we touch a panel, all its horizontal, vertical, and diagonal adjacent panels will also toggle their states.

Now you are given the configuration of the grid. Your goal is to turn on all the lights. Print the minimum number of touches required to achieve this goal.

Input
Input starts with an integer T (≤ 125), denoting the number of test cases.

Each test case starts with two integers R (1 ≤ R ≤ 8) and C (1 ≤ C ≤ 8). Then there will be R lines each containing C characters (‘*’ or ‘.’).

Output
For each test case, print the case number and the minimum number of touches required to have all the light panels in the board on at the same time. If it is not possible then print “impossible”.

Sample Input
4
5 5





1 2
.*
3 3
**.
**.

4 4
*…
**..
..**
…*
Sample Output
Case 1: 1
Case 2: impossible
Case 3: 2
Case 4: 10
Source
Problem Setter: Jane Alam Jan
转自学霸博客:http://www.tichocan.com/?p=514
题意:一个n*m的矩阵,‘*’表示灯亮,‘.’表示灯暗。每次对一个点操作,它和它周围8个点都会改变状态,问至少操作几次才可以让所有灯亮

搜索也是可以过的吧,毕竟数据小

先说说什么是高斯消元法(转载自其他题目的题解):

题意:有一个5*6的矩阵,每个位置都表示按钮和灯,1表示亮,0表示灭。每当按下一个位置的按钮,它和它周围灯的状态全部翻转,问在这样的一个方阵中按下哪些按钮可以把整个方阵都变成灭的,这时1表示按了,0表示没按。

这个游戏有一些技巧:
1、按按钮的顺序可以随便。
2、任何一个按钮都最多需要按下1次。因为按下第二次刚好抵消第一次,等于没有按。

这个问题可以转化成数学问题。
一个灯的布局可以看成一个0、1矩阵。以3×3为例:
0 1 0
1 1 0
0 1 1
表示一个布局。其中0表示灯灭,1表示灯亮。
每次按下按钮(POJ1222)或者叫一个宿舍关灯(0998),可以看成在原矩阵上加(模2加,就是按位异或)上一个如下的矩阵:
0 1 0
1 1 1
0 1 0
上述矩阵中的1表示按下第2行第2列的按钮时,作用的范围。如果按左上角的按钮,就是:
1 1 0
1 0 0
0 0 0

我们记L为待求解的原始布局矩阵。A(i,j)表示按下第i行第j列的按钮时的作用范围矩阵。在上述例子中,
L=
0 1 0
1 1 0
0 1 1

A(1,1)=
1 1 0
1 0 0
0 0 0

A(2,2)=
0 1 0
1 1 1
0 1 0

假设x(i,j)表示:想要使得L回到全灭状态,第i行第j列的按钮是否需要按下。0表示不按,1表示按下。那么,这个游戏就转化为如下方程的求解:
L + x(1,1)*A(1,1) + x(1,2)*A(1,2) + x(1,3)*A(1,3) + x(2,1)*A(2,1) + … + x(3,3)*A(3,3) = 0

其中x(i,j)是未知数。方程右边的0表示零矩阵,表示全灭的状态。直观的理解就是:原来的L状态,经过了若干个A(i,j)的变换,最终变成0:全灭状态。
由于是0、1矩阵,上述方程也可以写成:
x(1,1)*A(1,1) + x(1,2)*A(1,2) + x(1,3)*A(1,3) + x(2,1)*A(2,1) + … + x(3,3)*A(3,3) = L

这是一个矩阵方程。两个矩阵相等,充要条件是矩阵中每个元素都相等。将上述方程展开,便转化成了一个9元1次方程组:

简单地记做:AA * XX = LL

这个方程有唯一解:
x(1,1) x(1,2) x(1,3)
x(2,1) x(2,2) x(2,3)

x(3,1) x(3,2) x(3,3)

1 1 1
0 0 0
0 0 1

也就是说,按下第一行的3个按钮,和右下角的按钮,就

能使L状态变成全灭状态。
对于固定行列的阵列来说,AA矩阵也是确定的。是否存在解,解是否唯一,只与AA矩阵有关。对于唯一解的情形,只要将LL乘以AA的逆矩阵即可。具体求AA的逆矩阵的方法,可以用高斯消元法。
由于是0、1矩阵,上述方程也可以写成:

将1式两边同时加上一个L矩阵就可以变成
x(1,1)*A(1,1) + x(1,2)*A(1,2) + x(1,3)*A(1,3) + x(2,1)*A(2,1) + … + x(3,3)*A(3,3) = L

A(1,1)把矩阵 转化为一个列向量,L也转化为一个列向量,

将sigma xi*Ai=Li 对应位置的值相等就可以建立方程组了

X1*A(1,1)1+X2*A(1,2)1+X3*A(1,3)1+…………X30*A(30,30)1=L1; mod 2

X1*A(1,1)2+X2*A(1,2)2+X3*A(1,3)2+…………X30*A(30,30)2=L2; mod 2

X1*A(1,1)3+X2*A(1,2)3+X3*A(1,3)3+…………X30*A(30,30)3=L3 mod 2

…….

…….

…….

X1*A(1,1)30+X2*A(1,2)30+X3*A(1,3)30+…………X30*A(30,30)30=L30; mod 2

其中A(i,j)k 表示列向量A中第K个元素

这里的*表示点乘,Xi取(1,0) +表示模2加法,所以在高斯消元的时候可以用^异或运算

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define INF 0x3f3f3f3fusing namespace std;// a[][]表示增广矩阵,free_num 表示自由元个数,free_x[]表示不确定自由元/*  -2表示浮点数解,无整数解    -1表示无解    0表示唯一解    大于0表示无穷多解,并且返回自由变元的个数 */int a[70][70], x[70], free_num, free_x[70];char mat[10][10];int Id[10][10];int Gauss( int equ, int var ){    int max_r;    int col;    int free_num = 0;    for( int i = 0; i <= var; i++ ){        x[i] = 0;    }    col = 0;    int k;    for( k = 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( a[max_r][col] == 0 ){            k--;            free_x[free_num++] = col;            continue;        }        if( max_r != k ){            for( int j = col; j < var+1; j++ ){                swap( a[k][j], a[max_r][j]);            }        }        for( int i = k + 1; i < equ; i++ ){            if( a[i][col] ){                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] ){            return -1;        }    }    if( k < var ){        return var - k;    }    for( int i = var - 1; i >= 0; i-- ){        x[i] = a[i][var];        for( int j = i + 1; j < var; j++ ){            x[i] ^= ( a[i][j] && x[j] );        }    }    return 0;}int solve( int n, int m ){    memset( a, 0, sizeof( a ));    for( int i = 0; i < n; i++ ){        for( int j = 0; j < m; j++ ){            a[Id[i][j]][Id[i][j]] = 1;            if (i > 0) {                a[Id[i][j]][Id[i - 1][j]] = 1;                if (j > 0) {                    a[Id[i][j]][Id[i - 1][j - 1]] = 1;                }                if (j + 1 < m) {                    a[Id[i][j]][Id[i - 1][j + 1]] = 1;                }            }            if (i + 1 < n) {                a[Id[i][j]][Id[i + 1][j]] = 1;                if (j > 0) {                    a[Id[i][j]][Id[i + 1][j - 1]] = 1;                }                if (j + 1 < m) {                    a[Id[i][j]][Id[i + 1][j + 1]] = 1;                }            }            if (j > 0) {                a[Id[i][j]][Id[i][j - 1]] = 1;                if (i > 0) {                    a[Id[i][j]][Id[i - 1][j - 1]] = 1;                }                if (i + 1 < n) {                    a[Id[i][j]][Id[i + 1][j - 1]] = 1;                }            }            if (j + 1 < m) {                a[Id[i][j]][Id[i][j + 1]] = 1;                if (i > 0) {                    a[Id[i][j]][Id[i - 1][j + 1]] = 1;                }                if (i + 1 < n) {                    a[Id[i][j]][Id[i + 1][j + 1]] = 1;                }            }            if (mat[i][j] == '.') {                a[Id[i][j]][n * m] = 1;            }            else {                a[Id[i][j]][n * m] = 0;            }        }    }    int t = Gauss( n*m, n*m );    if( t == -1 ){        return t;    }    if( t == 0 ){        int ans = 0;        for( int i = 0; i < n*m; i++ ){            ans += x[i];        }        return ans;    }    int tot = ( 1 << t );    int ans = INF;    for( int i = 0; i < tot; i++ ){        int cnt = 0;        for( int j = 0; j < t; j++ ){//状压枚举自由元            if( i & ( 1 << j ) ){                x[free_x[j]] = 1;                cnt++;            }            else{                x[free_x[j]] = 0;            }        }        for( int j = n*m - t - 1; j >= 0; j-- ){//自下而上回带求解所有未知量            int idx;            for( idx = j; idx < n*m; idx++ ){                if( a[j][idx] ){                    break;                }            }            x[idx] = a[j][n*m];            for( int l = idx + 1; l < n*m; l++ ){                if( a[j][l] ){                    x[idx] ^= x[l];                }            }            cnt += x[idx];        }        ans = min( ans, cnt);    }    return ans; }int main(){    //freopen( "in.txt","r",stdin);    int T;    scanf("%d",&T);    int cas = 1;    while( T-- )    {        int n,m,ret = 0;        scanf("%d %d",&n,&m);        for( int i = 0; i < n; i++ ){            scanf("%s",mat[i]);            for( int j = 0; j < m; j++ ){                Id[i][j] = ret++;            }        }        int ans = solve( n ,m );        if( ans == -1 ){            printf("Case %d: impossible\n",cas++);            continue;        }        printf("Case %d: %d\n",cas++,ans);    }    return 0;}
0 0