zoukankan      html  css  js  c++  java
  • poj 1222 EXTENDED LIGHTS OUT 夜

    高斯消元
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<map>
    #include<cmath>
    #define LL long long
    
    using namespace std;
    
    const int N=35;
    
    int lx[4]={1,-1,0,0};
    int ly[4]={0,0,1,-1};
    int a[N][N];
    int ans[N];
    int n;
    void Gauss()
    {
        int row=0;
        int col=0;
        while(row<n&&col<n)
        {
            int k=-1;
            for(int i=row;i<n;++i)
            {
                if(a[i][col]!=0)
                {
                    k=i;break;
                }
            }
            if(k!=row)
            {
                for(int j=col;j<=n;++j)
                {
                    swap(a[row][j],a[k][j]);
                }
            }
            int x=a[row][col];
            for(int i=row+1;i<n;++i)
            {
                int y=a[i][col];
                for(int j=col;j<=n;++j)
                {
                    a[i][j]=(a[i][j]*x-y*a[row][j])%2;
                }
            }
            ++row;++col;
        }
        //cout<<row<<" "<<col<<endl;
        for(int i=row-1;i>=0;--i)
        {
            int sum=0;
            for(int j=i+1;j<n;++j)
            sum=sum+(ans[j]*a[i][j]);
            ans[i]=(a[i][n]-sum)/a[i][i]%2;
        }
        for(int i=0;i<5;++i)
        {
            for(int j=0;j<6;++j)
            {
                printf("%d",(ans[i*6+j]+2)%2);
                if(j<5)
                printf(" ");
            }
            printf("\n");
        }
    }
    int main()
    {
        n=30;
        int T;
        scanf("%d",&T);
        for(int w=1;w<=T;++w)
        {
            memset(a,0,sizeof(a));
            for(int i=0;i<5;++i)
            {
                for(int j=0;j<6;++j)
                {
                    int x;
                    scanf("%d",&x);
                    if(x==1)
                    a[i*6+j][n]=1;
                    a[i*6+j][i*6+j]=1;
                    for(int l=0;l<4;++l)
                    {
                        int l1=i+lx[l];
                        int l2=j+ly[l];
                        if(l1>=0&&l1<5&&l2>=0&&l2<6)
                        {
                            a[l1*6+l2][i*6+j]=1;
                        }
                    }
                }
            }
            printf("PUZZLE #%d\n",w);
            Gauss();
        }
        return 0;
    }
    

      

  • 相关阅读:
    poj 2251 Dungeon Master-搜索进阶-暑假集训
    棋盘问题-POJ 1321
    Popular Cows
    The Factor
    整数解 (hdu 2092
    Strange fuction hdu 2899
    Factors and Multiples
    Trailing Zeroes (III) -;lightoj 1138
    Swap——hdu 2819
    Arithmetic Sequence
  • 原文地址:https://www.cnblogs.com/liulangye/p/2609470.html
Copyright © 2011-2022 走看看