poj-1222-EXTENDED LIGHTS OUT && poj-1681-Painter's Problem

60 篇文章 0 订阅

gauss消元
这两道题都和poj-1830类似
只不过把二维矩阵看成一维的,每一个开关会影响它周围的灯的状态。

poj-1222-EXTENDED LIGHTS OUT
一发ac

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int inf = 0x3fffffff;
const double eps = 1e-6;
const int N = 33;
int res, r;
int a[N][N];
void debug(){
    return ;
    cout << "---------beautiful delimiter start------------" << endl;
    for (int i = 0; i < 30; i++){
        for (int j = 0; j <= 30; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << "---------beautiful delimiter end------------" << endl;
}
int gauss(int a[][N]) {
    int res = 0, r = 0;
    debug();
    for (int i = 0; i < 30; i++){
        for (int j = r; j < 30; j++){
            if (a[j][i]){
                for (int k = i; k <= 30; k++){
                    swap(a[j][k], a[r][k]);
                }
                break;
            }
        }
        if (!a[r][i]){
            res++;
            continue;
        }
        for (int j = 0; j < 30; j++){
            if (j != r && a[j][i]){
                for (int k = i; k <= 30; k++){
                    a[j][k] ^= a[r][k];
                }
            }
        }
        r++;
//      debug();
    }
    debug();
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    int i, j, k, t;
    cin >> t;
    for (int cas = 1; cas <= t; cas++){
        memset(a, 0, sizeof(a));
        for (i = 0; i < 30; i++){
            cin >> a[i][30];
        }
        for (i = 0; i < 5; i++){
            for (j = 0; j < 6; j++){
                if (i != 0){
                    // up
                    a[(i-1)*6+j][i*6+j] = 1;
                }
                if (j != 0){
                    // left
                    a[i*6+j-1][i*6+j] = 1;
                }
                if (i != 4){
                    // down
                    a[(i+1)*6+j][i*6+j] = 1;
                }
                if (j != 5){
                    // right
                    a[i*6+j+1][i*6+j] = 1;
                }
            }
        }
        for (i = 0; i < 30; i++){
            a[i][i] = 1;
        }
        gauss(a);
        cout << "PUZZLE #" << cas << endl;
        for (i = 0; i < 5; i++){
            for (j = 0; j < 6; j++){
                cout << a[i*6+j][30] << " ";
            }
            cout << endl;
        }
    }
    return 0;
}

poj-1681-Painter’s Problem

wa了一次,忘记考虑inf的情况了

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
const int inf = 0x3fffffff;
const double eps = 1e-6;
const int N = 330;
int res, r;
int a[N][N];
void debug(){
    return ;
    cout << "---------beautiful delimiter start------------" << endl;
    for (int i = 0; i < 25; i++){
        for (int j = 0; j <= 25; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << "---------beautiful delimiter end------------" << endl;
}
int gauss(int a[][N], int n) {
    int res = 0, r = 0;
    n *= n;
    debug();
    for (int i = 0; i < n; i++){
        for (int j = r; j < n; j++){
            if (a[j][i]){
                for (int k = i; k <= n; k++){
                    swap(a[j][k], a[r][k]);
                }
                break;
            }
        }
        if (!a[r][i]){
            res++;
            continue;
        }
        for (int j = 0; j < n; j++){
            if (j != r && a[j][i]){
                for (int k = i; k <= n; k++){
                    a[j][k] ^= a[r][k];
                }
            }
        }
        r++;
//      debug();
    }
    debug();
    for (int i = r; i < n; i++){
        if (a[i][n] != 0){
            return 0;
        }
    }
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("1.txt", "r", stdin);
#endif
    int i, j, k, t, n;
    cin >> t;
    char c;
    int ans; 
    for (int cas = 1; cas <= t; cas++){
        memset(a, 0, sizeof(a));
        cin >> n;
        for (i = 0; i < n*n; i++){
            cin >> c;
            if (c == 'y'){
                a[i][n*n] = 0;
            }else{
                a[i][n*n] = 1;
            }
        }
        for (i = 0; i < n; i++){
            for (j = 0; j < n; j++){
                if (i != 0){
                    // up
                    a[(i-1)*n+j][i*n+j] = 1;
                }
                if (j != 0){
                    // left
                    a[i*n+j-1][i*n+j] = 1;
                }
                if (i != n-1){
                    // down
                    a[(i+1)*n+j][i*n+j] = 1;
                }
                if (j != n-1){
                    // right
                    a[i*n+j+1][i*n+j] = 1;
                }
            }
        }
        for (i = 0; i < n*n; i++){
            a[i][i] = 1;
        }
        if (!gauss(a, n)){
            cout << "inf" << endl;
            continue;
        }
        ans = 0;
        for (i = 0; i < n; i++){
            for (j = 0; j < n; j++){
                ans += a[i*n+j][n*n];
            }
        }
        cout << ans << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值