UVA - 11464 Even Parity 【暴力枚举】
题意
给出一个 01 二维方阵
可以将里面的 0 改成1 但是 不能够 将 1 改成 0
然后这个方阵 会对应另外一个 方阵 另外一个方阵当中的元素 为 上 下 左 右 四个元素(如果存在)的和 要求另外一个方阵当中的元素 全都是偶数
求 最少的 改变次数 使得 满足这种状态
思路
刚开始 想要暴力枚举 所有状态
但是 2^225 太大了。。。
后来想想 如果 第一行确定了 那么 接下来的每一行 都是能够确定的
比如说
3
0 0 0
1 0 0
0 0 0
这组 样例 来说
假如 我第一行为
0 1 0
那么 第二行的状态
如果 原始元素 为 1 我们就要判断 是否满足 不满足 就return
如果 原始元素 为 0 我们就判断 其 左上 右上 和 上上 的元素 相加 是否为 奇数 如果是 ans++ 并且 将该元素 改为 1
然后 最后 其实是要多判断一行的
因为我们每一次判断的 其实是上一行的状态 比如说
当前的状态是 g[5][5] 那么 我们就要求
sum = g[4][4] + g[4][6] + g[3][5] 易知 这三个点 都是已经 判断并且更新过的
如果 sum 为 奇数
并且 g[5][5] = 0 的话 我们就将 g[5][5] 改为1 ans++
因为只有这样 g[4][5] 这个点 的sum 才是偶数
如果 g[5][5] = 1 那么 这种状态就是不行的 直接 return 一个不行的标记的就可
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;
const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30;
const int INF = 0x3f3f3f3f;
const int maxn = 4e5 + 5;
const int MOD = 1e9 + 7;
int G[15][15];
int n;
int Move[4][2]
{
0, 0,
-1, -1,
-1, 1,
-2, 0,
};
bool ok(int x, int y)
{
if (x < 0 || x >= n || y < 0 || y >= n)
return false;
return true;
}
int check(int cur)
{
int g[16][16];
CLR(g, 0);
int ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
g[i][j] = G[i][j];
}
}
for (int i = n - 1; i >= 0; i--)
{
if (g[0][i] == 0)
{
if (cur & 1)
{
ans++;
g[0][i] = 1;
}
cur >>= 1;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < 4; k++)
{
int x = i + Move[k][0];
int y = j + Move[k][1];
if (ok(x, y))
sum += g[x][y];
}
if (sum & 1)
{
if (g[i][j] == 0)
{
g[i][j] = 1;
ans++;
}
else
{
return INF;
}
}
}
}
return ans;
}
int main()
{
int t;
cin >> t;
int count = 1;
while (t--)
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &G[i][j]);
int ans = INF;
int len = 0;
for (int i = 0; i < n; i++)
{
if (G[0][i] == 0)
len++;
}
len = 1 << len;
for (int i = 0; i <= len; i++)
ans = min(check(i),ans);
printf("Case %d: ", count++);
if (ans == INF)
printf("-1\n");
else
cout << ans << endl;
}
}
UVA - 11464 Even Parity 【暴力枚举】的更多相关文章
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- UVA.11464 Even Parity (思维题 开关问题)
UVA.11464 Even Parity (思维题 开关问题) 题目大意 给出一个n*n的01方格,现在要求将其中的一些0转换为1,使得每个方格的上下左右格子的数字和为偶数(如果存在的话),求使得最 ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- UVA 11464 Even Parity(递归枚举)
11464 - Even Parity Time limit: 3.000 seconds We have a grid of size N x N. Each cell of the grid in ...
- UVA 11464 Even Parity(部分枚举 递推)
Even Parity We have a grid of size N x N. Each cell of the grid initially contains a zero(0) or a on ...
- UVa 11464 Even Parity (二进制法枚举)
题意:给你一个n*n的01矩阵,让你把最少的0变成1,使得每个元素的上,下,左,右的元素(如果有的话)之和均为偶数. 析:最好想的的办法就是暴力,就是枚举每个数字是变还是不变,但是...时间复杂度也太 ...
- UVA 11464 - Even Parity(枚举方法)
D Even Parity Input: Standard Input Output: Standard Output We have a grid of size N x N. Each cell ...
- 【UVA】11464 Even Parity(枚举子集)
题目 传送门:QWQ 分析 标准的套路题. 枚举第一行,接着根据第一行递推下面的行. 时间复杂度$ O(2^n \times n^2) $ 代码 #include <bits/stdc++.h& ...
- 【转载】UVa 11464 Even Parity 偶数矩阵
题意:给你一个n*n的01矩阵,让你把这个矩阵中尽量少的0转换成1,使得矩阵每个位置的上下左右四个相邻的数加起来能被2整除,求最少的转换数 首先,n 的规模并不大,最大只有15.但是完全枚举整个矩阵显 ...
随机推荐
- dedecms 调用父栏目下的所有子栏目
效果如下: 代码如下: <div class="productxilie"> <ul> {dede:channelartlist row=6 typeid ...
- mac使用xampp中自带phpmyadmin连接单独安装mysql
1 在xampp安装目录中找到phpadmin目录,编辑config.inc.php权限,赋予读写权限 2 打开config.inc.php $cfg['Servers'][$i]['user'] = ...
- PHP执行linux系统命令
本文是第一篇,讲述如何在PHP中执行系统命令从而实现一些特殊的目的,比如监控服务器负载,重启MySQL.更新SVN.重启Apache等.第二篇<PHP监控linux服务器负载>:http: ...
- OpenCV生成点集的Delaunay剖分和Voronoi图
实现内容: 设置一副图像大小为600*600.图像像素值全为0,为黑色. 在图像中Rect(100,100,400,400)的区域随机产生20个点.并画出. 产生这些点集的Delaunay剖分和Vor ...
- Struts2中Action自己主动接收參数
Struts2中Action接收參数的方法主要有下面三种: 1.使用Action的属性接收參数:(通过属性驱动式) a.定义:在Action类中定义属性,创建get和set方法. b. ...
- leetcode-Symmetric Tree 对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 导出txt格式的说明书
/// <summary> /// 说明书 /// </summary> /// <returns></returns> public FileResu ...
- Java储存过程
存储过程:是指保存在数据库并在数据库端执行的程序. CallableStatement 对象为所有的 DBMS 提供了一种以标准形式调用已储存过程的方法.已储存过程储存在数据库中. 对已储存过程的调用 ...
- c++实现二叉搜索树
自己实现了一下二叉搜索树的数据结构.记录一下: #include <iostream> using namespace std; struct TreeNode{ int val; Tre ...
- 优志愿前端数据加密破解-python
# coding=utf-8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "etiaky.sett ...