惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Martin Fowler
Martin Fowler
I
InfoQ
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
B
Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
V
Visual Studio Blog

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
Uva 11464.Even Parity|OhYee 博客
2016-08-26 · via OhYee 博客

这是一篇最后编辑于 8 年前 的文章,其内容可能与目前实际情况差异较大,请注意甄别

题目

We have a grid of size N × N.
Each cell of the grid initially contains a zero(0) or a one(1).
The parity of a cell is the number of 1s surrounding that cell.
A cell is surrounded by at most 4 cells (top, bottom, left, right).

Suppose we have a grid of size 4 × 4:


For this problem, you have to change some of the 0s to 1s so that the parity of every cell becomes even.
We are interested in the minimum number of transformations of 0 to 1 that is needed to achieve the desired requirement.

Input

The first line of input is an integer T (T < 30) that indicates the number of test cases.
Each case starts with a positive integer N (1 ≤ N ≤ 15).
Each of the next N lines contain N integers (0/1) each.
The integers are separated by a single space character.

Output

For each case, output the case number followed by the minimum number of transformations required.

If it’s impossible to achieve the desired result, then output ‘-1’ instead.

Sample Input

3
3
0 0 0
0 0 0
0 0 0
3
0 0 0
1 0 0
0 0 0
3
1 1 1
1 1 1
0 0 0

Sample Output

Case 1: 0
Case 2: 3
Case 3: -1

题解

本来想使用动态规划,仔细一想压根没法计算

枚举 第一行的所有可能
模拟 生成剩下的每一行,计算与原图最 相近

输出结果即可,没什么明显的大坑,就是交题的时候服务器抽了2个多小时

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

const int INF = 0x7FFFFFFF;
const double eps = 1e-10;

const int maxn = 16;
const int delta[3][2] = {{-1,-1},{-1,1},{-2,0}};


int state[maxn];
int Map[maxn];

int n;
int kase = 1;

//比较两种状态,返回需要变成1的0的数量,不能完成返回-1
int Get(int res,int e) {
    int ans = 0;
    for(int i = 0;i < n;i++) {
        int a = res & 1;
        int b = e & 1;

        res >>= 1;
        e >>= 1;

        if(a != b)
            if(a == 0)
                ans++;
            else
                return -1;
    }
    return ans;
}

inline int SetI(int num,int i,bool flag) {
    if(flag)
        return num | (1 << i);
    else
        return num & (~(1 << i));
}

inline bool GetI(int num,int i) {
    return num >> i & 1;
}

void Do() {
    cin >> n;
    int Min = INF;

    for(int i = 0;i < n;i++) {
        int m = 0;
        for(int j = 0;j < n;j++) {
            int t;
            cin >> t;
            m <<= 1;
            m = SetI(m,0,t);
        }
        Map[i] = m;
    }

    for(int k = 0;k < (1 << n);k++) {
        //枚举初始态
        int ans = 0;
        state[0] = k;

        int temp = Get(Map[0],state[0]);

        if(temp == -1)
            continue;
        else
            ans += temp;

        for(int i = 1;i < n;i++) {
            //处理第i行
            for(int j = 0;j < n;j++) {
                //处理第j个位置
                int sum = 0;
                for(int t = 0;t < 3;t++) {
                    int xx = i + delta[t][0];
                    int yy = j + delta[t][1];
                    if(xx >= 0 && yy >= 0 && xx < n && yy < n) {
                        sum += GetI(state[xx],yy);
                    }
                }
                if(sum & 1)
                    state[i] = SetI(state[i],j,1);
                else
                    state[i] = SetI(state[i],j,0);
            }

            temp = Get(Map[i],state[i]);
            if(temp == -1)
                break;
            else
                ans += temp;
        }

        if(temp == -1)
            continue;
        else 
            Min = min(Min,ans);
    }

    cout << "Case " << kase++ << ": ";
    if(Min == INF)
        cout << -1 << endl;
    else
        cout << Min << endl;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    int T;
    cin >> T;
    while(T--)
        Do();

    return 0;
}