zoukankan      html  css  js  c++  java
  • POJ1222 EXTENDED LIGHTS OUT

    题意

    给出一个6*5的棋盘,每次按下一个按钮会使一个方格和它周边四联通的方格状态反转,问怎样操作可以使棋盘上所有方格全灭

    思路

    可以将每个位置是否进行操作视为未知数(x_i),设每个位置的初始状态为(a_i),要求解的问题就变成了

    [left{ egin{array}{**lr**} x_1 oplus x_2 oplus x_3 oplus dots oplus x_n=a_1oplus 0& \ x_1 oplus x_2 oplus x_3 oplus dots oplus x_n=a_2oplus 0& \ dots &\ x_1 oplus x_2 oplus x_3 oplus dots oplus x_n=a_noplus 0& \ end{array} ight. ]

    于是问题就转化成了解异或方程组的问题,由于异或也是一种线性运算,所以同样可以使用高斯消元法化成上三角矩阵求解
    由于数据范围较小,可以暴力的去做,复杂度是(30^3)

    代码

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    int _mat[50][50],_X[50];
    void Gauss(int n){
        for(int i=1;i<=n;i++){
            bool f=true;
            if(_mat[i][i]==0){
                f=false;
                for(int j=i+1;j<=n;j++){
                    if(_mat[j][i]==1){
                        for(int k=1;k<=n;k++)
                            swap(_mat[i][k],_mat[j][k]);
                        swap(_X[i],_X[j]);
                        f=true;
                        break;
                    }
                }
            }
            if(f){
                for(int j=1;j<=n;j++){
                    if(i!=j&&_mat[j][i]!=0){
                        for(int k=1;k<=n;k++)
                            _mat[j][k]^=_mat[i][k];
                        _X[j]^=_X[i];
                    }
                }
            }
        }
    }
    int mat[50][50];
    int getid(int x,int y){
        return (x-1)*6+y;
    }
    int cnt=0;
    void work(void){
        ++cnt;
        for(int i=1;i<=5;i++)
            for(int j=1;j<=6;j++){
                scanf("%d",&mat[i][j]);
                _X[getid(i,j)]=mat[i][j]^0;
            }
        for(int i=1;i<=5;i++){
            for(int j=1;j<=6;j++){
                if(i-1>=1)
                    _mat[getid(i,j)][getid(i-1,j)]=1;
                if(j-1>=1)
                    _mat[getid(i,j)][getid(i,j-1)]=1;
                _mat[getid(i,j)][getid(i,j)]=1;
                if(j+1<=6)
                    _mat[getid(i,j)][getid(i,j+1)]=1;
                if(i+1<=5)
                    _mat[getid(i,j)][getid(i+1,j)]=1;
            }
        }
        Gauss(5*6);
        printf("PUZZLE #%d
    ",cnt);
        for(int i=1;i<=5;i++){
            for(int j=1;j<=6;j++){
                printf("%d%c",_X[getid(i,j)],(j==6)?'
    ':' ');            
            }
        }
    }
    int main(){
        int T;
        scanf("%d",&T);
        while(T--){
            work();
        }
        return 0;
    }
    
  • 相关阅读:
    php 修改、增加xml结点属性的实现代码
    mysql rand随机查询记录效率
    分享:mysql 随机查询数据
    分享:perl 文件操作总结
    分享:Perl打开与读取文件的方法
    js日期相关函数总结分享
    php后台如何避免用户直接进入方法实例
    python 函数的进阶
    python 初识函数
    python 冒泡排序
  • 原文地址:https://www.cnblogs.com/dreagonm/p/14339290.html
Copyright © 2011-2022 走看看