poj 1222EXTENDED LIGHTS OUT

高斯消元的题本质思想一样。

学习网址:http://www.cnblogs.com/rainydays/archive/2011/08/31/2160748.html

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
short g[35][35],f[35][35],m[][2]={{0,0},{0,1},{0,-1},{1,0},{-1,0}},x[35];
short swap(short &a,short &b){
    a=a^b;
    b=a^b;
    a=a^b;
}
void work(){
    memset(x,0,sizeof(x));//无关项默认为0
    short i,j,k,l;
    for(i=0,j=0;j<30;){
        for(k=i;k<30;k++){
            if(g[k][j]!=0)
                break;
        }
        if(k==30){
            j++;
            continue;
        }
        if(k!=i){
            for(l=j;l<=30;l++)
            swap(g[k][l],g[i][l]);
        }
        for(k=i+1;k<30;k++){
            if(g[k][j])
            for(l=j;l<=30;l++){
                g[k][l]=g[i][l]^g[k][l];
            }
        }
            i++;j++;
    }//j==30
    for(k=i-1;k>=0;k--){
        x[k]=g[k][30];
        for(l=29;l>k;l--){
            x[k]^=g[k][l]&&x[l];
        }
    }//题中已经保证有解,无需再进行有无解的判断
}
void print(){
    short i=0,j=0;
    for(;i<5;i++){
        cout<<x[i*6];
        for(j=1;j<6;j++){
            cout<<' '<<x[i*6+j];
        }
    cout<<endl;
    }
}
int main(){
    //freopen("D:\\INPUT.txt","r", stdin);
    int n,t;
    cin>>n;
    t=1;
    int i,j,k,a,b;
    for(i=0;i<5;i++)
        for(j=0;j<6;j++)
            for(k=0;k<5;k++){
              a=i+m[k][0];
              b=j+m[k][1];
              if(a>=0&&b>=0&&a<5&&b<6){
                  f[i*6+j][a*6+b]=1;
              }
            }
    while(t<=n){
        cout<<"PUZZLE #"<<t++<<endl;
        memcpy(g,f,sizeof(f));
            for(i=0;i<30;i++){
                scanf("%d",&g[i][30]);
            }
           /* for(i=0;i<30;i++){
                cout<<g[i][0];
                for(j=1;j<=30;j++){
                    cout<<' '<<g[i][j];
                }
                cout<<endl;
            }*/
            //print();
            work();
            print();
    }
    return 0;
}
View Code

 

posted @ 2014-08-19 22:56  Deribs4  阅读(194)  评论(0编辑  收藏  举报