UVA11464 Even Parity

知识点:递归,递推

和李煜东的那道例题大体思想上一样,指数枚举第一行的时间复杂度是可以接受的,所以枚举第一行,然后由第一行递推得出后面的行,在中间判断存结果就行,需要注意的是,本题只能0变成1,这个不要想当然我一开始做的时候直接默认01可以互换

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

const int Inf = 1000000000;

int n, mat[20][20], t[20][20], ans;
vi chosen;

void calc(int xx) {
    if (xx == n + 1) {
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) t[i][j] = mat[i][j];
        }
        for (int i = 0; i < sz(chosen); i++) {
            if (t[1][chosen[i]]) continue;
            t[1][chosen[i]] = 1;
            cnt++;
        }
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                int temp = 0;
                if (i - 1 > 1) temp += t[i - 2][j];
                if (i - 1 < n) temp += t[i][j];
                if (j > 1) temp += t[i - 1][j - 1];
                if (j < n) temp += t[i - 1][j + 1];
                if (temp % 2) {
                    if (t[i][j]) return;
                    cnt++; t[i][j] = 1;
                }
                if (cnt >= ans) return;
            }
        }
        int ok = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                int temp = 0;
                if (i > 1) temp += t[i - 1][j];
                if (i < n) temp += t[i + 1][j];
                if (j > 1) temp += t[i][j - 1];
                if (j < n) temp += t[i][j + 1];
                if (temp % 2) ok = 0;
            }
        }
        if (ok && cnt < ans) ans = cnt;
        return;
    }
    calc(xx + 1);
    chosen.pb(xx);
    calc(xx + 1);
    chosen.pop_back();
}

int main() {
    int t;
    cin >> t;
    for (int k = 1; k <= t; k++) {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) cin >> mat[i][j];
        }
        ans = Inf;
        calc(1);
        cout << "Case " << k << ": ";
        cout << (ans == Inf ? -1 : ans) << endl;
    }
    return 0;
}

见鬼了,上面是能过的,但是下面的写法就是错的,不知道哪里出问题了,因为这个数组的周围都是0,为什么这种写法会出现错误呢,不应该是等价的,有没有大手子来解答一下

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

const int Inf = 1000000000;

int n, mat[20][20], t[20][20], ans;
vi chosen;

void calc(int xx) {
    if (xx == n + 1) {
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) t[i][j] = mat[i][j];
        }
        for (int i = 0; i < sz(chosen); i++) {
            if (t[1][chosen[i]]) continue;
            t[1][chosen[i]] = 1;
            cnt++;
        }
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                int temp = t[i - 2][j] + t[i][j] + t[i - 1][j - 1] + t[i - 1][j + 1];
                //if (i - 1 > 1) temp += t[i - 2][j];
                //if (i - 1 < n) temp += t[i][j];
                //if (j > 1) temp += t[i - 1][j - 1];
                //if (j < n) temp += t[i - 1][j + 1];
                if (temp % 2) {
                    if (t[i][j]) return;
                    cnt++; t[i][j] = 1;
                }
                if (cnt >= ans) return;
            }
        }
        int ok = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                int temp = t[i - 1][j] + t[i + 1][j] + t[i][j - 1] + t[i][j + 1];
                //if (i > 1) temp += t[i - 1][j];
                //if (i < n) temp += t[i + 1][j];
                //if (j > 1) temp += t[i][j - 1];
                //if (j < n) temp += t[i][j + 1];
                if (temp % 2) ok = 0;
            }
        }
        if (ok && cnt < ans) ans = cnt;
        return;
    }
    calc(xx + 1);
    chosen.pb(xx);
    calc(xx + 1);
    chosen.pop_back();
}

int main() {
    int t;
    cin >> t;
    for (int k = 1; k <= t; k++) {
        memset(mat, 0, sizeof(mat));
        cin >> n;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) cin >> mat[i][j];
        }
        ans = Inf;
        calc(1);
        cout << "Case " << k << ": ";
        cout << (ans == Inf ? -1 : ans) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值