poj_1681_高斯消元

來源:互聯網
上載者:User

標籤:數學   高斯消元   

這道題和之前的把那一道1222非常相似,只不過一定要注意一下對於無解的判斷。

/*########################################################################## File Name: poj_1681.cpp# Author: CaoLei# Created Time: 2015/7/20 18:33:08#########################################################################*/#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <set>#include <queue>#include <map>using namespace std;#define MAX(x,y) (((x)>(y))?(x):(y))#define MIN(x,y) (((x)<(y))?(x):(y))#define N 500010#define pi acos(-1.0)#define inf 100000000typedef long long ll;typedef unsigned long long ull;int a[300][300];char str[20];int n;bool f;void gauss(){    for (int i = 0; i<n*n; i++){        int p=i;        for (int j = i; j<n*n; j++){          //這個地方j的初始值是i不        //是0,錯了一次切記。            if (a[p][i]<a[j][i]){                p=j;            }        }        for (int j = 0; j <= n*n; j++) swap(a[i][j], a[p][j]);        int count=0;        for(int k=i;k<n*n;k++)       //判斷是否有解,關鍵          if(a[i][k]) count++;        if(!count&&a[i][n*n]){            f=false;            return ;        }        for (int j = 0; j<n*n; j++)          if (i != j&&a[j][i])            for (int k = i + 1; k <= n*n; k++)              a[j][k] ^= a[i][k];    }}int main(){    freopen("in.txt", "r", stdin);    int t;    scanf("%d", &t);    while (t--){        f = true;        memset(a, 0, sizeof(a));        scanf("%d", &n);        for (int i = 0; i<n; i++){            scanf("%s",str);            for (int j = 0; j<n; j++){                if (str[j] == ‘w‘){                    a[i*n + j][n*n] = 1;                }            }        }        for (int j = 0; j<n*n; j++){            a[j][j] = 1;            if (j%n != 0) a[j - 1][j] = 1;            if ((j + 1) % n != 0) a[j + 1][j] = 1;            if (j >= n) a[j - n][j] = 1;            if (j<(n - 1)*n) a[j + n][j] = 1;        }        gauss();        if (!f){            printf("inf\n");            continue;        }        int count = 0;        for (int i = 0; i<n*n; i++){            if(a[i][n*n]) count++;        }        printf("%d\n",count);    }    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

poj_1681_高斯消元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.