poj1222 高斯消元

/*
Time:2019.12.13
Author: Goven
type:高斯消元
ref:
[知识点]https://blog.csdn.net/lzyws739307453/article/details/89816311
[代码]https://blog.csdn.net/accelerator_/article/details/38024673
*/
注意:本题隐含有唯一解
法1:高斯消元

//高斯消元 
#include
#include
using namespace std;

int a[31][31];//表示30个方程组 
int d[5][2] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int res[5][6];//结果 

void back () {
    for (int i = 29; i >= 0; i--) {
        res[i / 6][i % 6] = a[i][30];
    }   
}

void gauss () {
    for (int i = 0; i < 30; i++) {
        //行交换 
        int k = i;
        for (; k < 30; k++) {
            if (a[k][i]) break;
        }
        for (int j = 0; j <= 30; j++) {//att1:根据题意存在唯一解,所以这里k不会>=30 不用再判断了
            swap(a[i][j], a[k][j]);
        }
        //消元--化成单位矩阵 
        for (int j = 0; j < 30; j++) {//att1:从0开始 
            if (i == j) continue;//att2:别漏了 
            if (a[j][i]) {
                for (int k = i; k <= 30; k++) {
                    a[j][k] ^= a[i][k];
                }
            }
        } 
    }
    back();
}

int main()
{
    int t, cnt = 0;
    cin >> t;
    while (t--) {
        memset(a, 0, sizeof(a));
        for (int i = 0; i < 30; i++) {
            cin >> a[i][30];
        }
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 6; j++) {
                int ti = i * 6 + j;
                for (int k = 0; k < 5; k++) {
                    int tx = i + d[k][0];
                    int ty = j + d[k][1];
                    if (tx < 0 || tx > 4 || ty < 0 || ty > 5) continue;
                    a[ti][tx * 6 + ty] = 1;
                }
            }
        }
        
        gauss();
        cout << "PUZZLE #" << ++cnt << endl;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                cout << res[i][j] << ' '; 
            }
            cout << res[i][5] << endl;
        } 
    }   
    return 0;
}

法2:枚举递推

//枚举递推 
#include
#include
using namespace std;

int d[5][2] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int res[5][6];//结果 
int g[5][6],tp[5][6];

bool judge (int n) {
    memset(res, 0, sizeof(res));
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 6; j++) 
            tp[i][j] = g[i][j];
    }   
    for (int i = 0; i < 6; i++) {
        if (n & (1 << i)) {
            res[0][i] = 1;
            for (int j = 0; j < 5; j++) {
                int tx = d[j][0];
                int ty = i + d[j][1];
                if (tx < 0 || tx > 4 || ty < 0 || ty > 5) continue;
                tp[tx][ty] = !tp[tx][ty];
            }
        }
    }
    
    for (int i = 1; i < 5; i++) {
        for (int j = 0; j < 6; j++) {
            if (tp[i - 1][j]) {
                res[i][j] = 1;
                for (int k = 0; k < 5; k++) {
                    int tx = i + d[k][0];
                    int ty = j + d[k][1];
                    if (tx < 0 || tx > 4 || ty < 0 || ty > 5) continue;
                    tp[tx][ty] = !tp[tx][ty];
                }
            }
        }
    }
    
    for (int i = 0; i < 6; i++) {
        if (tp[4][i]) return false;
    }   
    return true;
}

void solve () {
    for (int i = 0; i < (1 << 6); i++) {
        if (judge(i)) return;
    }
}
int main()
{
    int t, cnt = 0;
    cin >> t;
    while (t--) {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 6; j++) {
                cin >> g[i][j];
            }
        }
        
        solve();
        
        cout << "PUZZLE #" << ++cnt << endl;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                cout << res[i][j] << ' '; 
            }
            cout << res[i][5] << endl;
        } 
        
        
    }   
    return 0;
}

你可能感兴趣的:(poj1222 高斯消元)