首页 > 代码库 > POJ 1681 Painter's Problem 高斯消元
POJ 1681 Painter's Problem 高斯消元
利用高斯消元求解异或方程
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <climits>#include <iostream>#include <string>using namespace std; #define MP make_pair#define PB push_backtypedef long long LL;typedef unsigned long long ULL;typedef vector<int> VI;typedef pair<int, int> PII;typedef pair<double, double> PDD;const int INF = INT_MAX / 3;const double eps = 1e-8;const LL LINF = 1e17;const double DINF = 1e60;const int maxn = 250;const int dx[] = {0, 0, 1, -1};const int dy[] = {1, -1, 0, 0};int a[maxn][maxn], n;void Gauss() { for(int i = 0; i < n * n; i++) { int k = i; while(a[k][i] == 0 && k < n * n) k++; if(k >= n * n) break; for(int j = 0; j <= n * n; j++) swap(a[i][j], a[k][j]); for(int j = 0; j < n * n; j++) if(j != i && a[j][i] != 0) { for(int k = 0; k <= n * n; k++) { a[j][k] ^= a[i][k]; } } }}void pp() { for(int i = 0; i < n * n; i++) { for(int j = 0; j <= n * n; j++) { printf("%d ", a[i][j]); } puts(""); }}void solve() { for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { int u = i * n + j; a[u][u] = 1; for(int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k], nu = nx * n + ny; if(nx >= 0 && nx < n && ny >= 0 && ny < n) { a[u][nu] = a[nu][u] = 1; } } } } Gauss(); int ans = 0; bool bad = false; for(int i = 0; i < n * n; i++) { if(a[i][n * n]) ans++; int nsum = 0; for(int j = 0; j < n * n; j++) nsum += a[i][j]; if(nsum == 0 && a[i][n * n] == 1) bad = true; } if(bad) puts("inf"); else printf("%d\n", ans);}int main() { int T; scanf("%d", &T); while(T--) { memset(a, 0, sizeof(a)); scanf("%d", &n); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { char tmp; scanf(" %c", &tmp); if(tmp == ‘w‘) a[i * n + j][n * n] = 1; else a[i * n + j][n * n] = 0; } } solve(); } return 0;}
POJ 1681 Painter's Problem 高斯消元
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。