美文网首页
poj1681 高斯消元

poj1681 高斯消元

作者: 暖昼氤氲 | 来源:发表于2019-12-13 20:19 被阅读0次

/*
Time:2019.12.13
Author: Goven
type:
ref:https://www.cnblogs.com/geloutingyu/p/7571522.html
*/

法1:高斯消元


//高斯消元 
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;

int a[225][226];
int d[5][2] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int res;

void back (int n) {
    int len = n * n;
    int i, cnt = 0; //cnt表示随机变量的个数 

    for (i =  len - 1; i >= 0; i--) {
        if (a[i][len]) {
            if (a[i][i] == 0) {
                cout << "inf" << endl;
                return;
            }
            break;
        } 
        cnt++;
    }
    res = 300;  
    for (int j = 0; j < (1 << cnt); j++) {//随机变量遍历所有可能 
        int cnt = 0;
        for (int k = 0; k < cnt; k++) {
            if (j & (1 << k)) {
                cnt++;
                for (int t = i; t >= 0; t--) {
                    if (a[t][n - cnt]) a[t][len] = !a[t][len];
                }   
            }   
        }
        for (int k = i; k >= 0; k--) {
            if (a[k][len]) cnt++;
        }
        res = min(res, cnt);
    } 
    cout << res << endl;
}

void gauss (int n) {
    int len = n * n;
    for (int i = 0; i < len; i++) {
        int k = i;
        for (; k < len; k++) {
            if (a[k][i]) break;
        }
        //交换行 
        if (k < len) {//err1:要判断k,不然k =len时会出错 
            for (int j = 0; j <= len; j++) {//att1: 别忘了= 
                swap(a[i][j], a[k][j]);
            }
        }
        
        //消元 
        for (int j = 0; j < len; j++) {
            if (i == j) continue;
            if (a[j][i]) {
                for (int k = i; k <= len; k++) {
                    a[j][k] ^= a[i][k];
                }
            }
        } 
    }
    back(n);
    
}

int main()
{
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        memset(a, 0, sizeof(a));
        char c;
        int len = n * n;
        for (int i = 0; i < len; i++) {
            cin >> c;
            if (c == 'w') a[i][len] = 1;
        }

        //扩展矩阵 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int ti = i * n + j;
                for (int k = 0; k < 5; k++) {
                    int tx = i + d[k][0];
                    int ty = j + d[k][1];
                    if (tx < 0 || tx >= n || ty <0 || ty >= n) continue;
                    a[ti][tx * n + ty] = 1;
                }
            }
        }
        gauss(n);
        
    }
    return 0;
}

法2:枚举递推


//枚举递推 
#include<iostream>
#include<cstring>
using namespace std;

int g[15][15], tp[15][15];
int d[5][2] = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int res;

int judge (int s, int n) {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            tp[i][j] = g[i][j];
    }

    for (int i = 0; i < n; i++) {
        if (s & (1 << i)) {
            cnt++;
            for (int j = 0; j < 5; j++) {
                int tx = d[j][0];
                int ty = i + d[j][1];
                if (tx < 0 || tx >= n || ty < 0 || ty >= n) continue;
                tp[tx][ty] = !tp[tx][ty];
            }
        }
    }
    
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (tp[i - 1][j]) {
                cnt++;
                for (int k = 0; k < 5; k++) {
                    int tx = i +d[k][0];
                    int ty = j + d[k][1];
                    if (tx < 0 || tx >= n || ty < 0 || ty >= n) continue;
                    tp[tx][ty] = !tp[tx][ty];
                }
            }
        }
    }
    
    for (int i = 0; i < n; i++) {
        if (tp[n - 1][i]) {
            return 300;
        }
    }
    return cnt;
}


void solve (int n) {
    for (int i = 0; i < (1 << n); i++) {
        res = min(res, judge(i, n));
    }
}

int main()
{
    int t, n;
    cin >> t;
    while (t--) {
        cin >> n;
        memset(g, 0, sizeof(g));
        res = 300;
        char c;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cin >> c;
                if (c == 'w') g[i][j] = 1;
            }
        }
        solve(n); 
        if (res == 300) cout << "inf" << endl;
        else
            cout << res << endl;
    }
    return 0;
}

相关文章

  • poj1681 高斯消元

    /*Time:2019.12.13Author: Goventype:ref:https://www.cnblog...

  • 高斯消元(行列式)

    浮点高斯消元: 模意义下高斯消元 整数行列式求值: 异或消元:

  • 2019-05-19 线性方程组-高斯消元法

    高斯消元法:(列主元高斯消元法)1.正常的情况下,我们将矩阵化成上三角,然后化成行最简形式,这里算法略微不同;2....

  • 高斯_约旦消元法_线性代数_day30

    高斯-约旦消元法 继续高斯消元法得出每一个未知数的值 前向过程(从上到下)选择最上面的主元,化为1主元下面所有的行...

  • 解方程

    复习一下解方程 0X00 解线性方程 用「高斯消元法」解线性方程 面试题 16.03. 交点 883. 高斯消元解...

  • 【线性代数学习笔记(一)】矩阵表示法和矩阵乘法规则是怎么来的?

    目录 求解线性方程组:高斯消元法简化线性方程组的表示——得到原始的矩阵定义用矩阵的表示方法表示高斯消元法解线性方程...

  • poj1222 高斯消元

    /*Time:2019.12.13Author: Goventype:高斯消元ref:[知识点]https://b...

  • 高斯消元法(约当消元法)

    这个真的烦了我好久啊,可能是我太笨了,最后看了几个模板,和写了好久,终于让我弄懂了.具体看代码咯这个是直接判断是否...

  • 数值代数

    第一次数值代数上机作业 一. (1)不选主元的高斯消元法 (2)列主元 结论:列主元gauss消元法更加精确,且稳...

  • 4.1 顺序高斯消元法

    算法见课本P31直接法的一种,前提是矩阵非奇异,通过有限步运算可以得到精确解注意:Gauss消元法比cramer法...

网友评论

      本文标题:poj1681 高斯消元

      本文链接:https://www.haomeiwen.com/subject/kmdqnctx.html