【蓝桥杯-Even Parity】

蓝桥杯-Even Parity


洛谷 UVA11464 Even Parity

在这里插入图片描述
暴力思路:
去遍历每个元素,如果不符合要求则翻转
时间复杂度大概在O(2^(n×n) × n×n)

改进思路:
先去枚举确定第一行(第一行得合法),然后为保证第一行每个元素为偶数和去确定第二行;为保证第二行每个元素为偶数和去确定第三行。以此逐行检索。
最后对最后一行进行偶数和验证,如果满足要求,则方案合理。

#include<bits/stdc++.h>

using namespace std;

const int N = 20;
int grid[N][N], tmp[N][N], n;

int check()
{
	for (int i = 2; i <= n; i++)//生成接下来的行数 
		for (int j = 1; j <= n; j++)
		{
			int sum = 0;
			if (i > 2)sum += tmp[i - 2][j];//上 
			if (i > 1 && j > 1)sum += tmp[i - 1][j - 1];//左 
			if (i > 1 && j < n)sum += tmp[i - 1][j + 1];//右 
			tmp[i][j] = sum & 1;//sum为偶tmp为偶,sum为奇tmp为奇 
			if (tmp[i][j] == 0 && grid[i][j] == 1)return n * n + 1;//1改0 非法 

		}
	for (int j = 1; j <= n; j++)//检查第n行是不是合法 
	{
		int sum = 0;
		if (n > 1)sum += tmp[n - 1][j];//上 
		if (j > 1)sum += tmp[n][j - 1];//左 
		if (j < n)sum += tmp[n][j + 1];//右
		if (sum & 1) return n * n + 1;//sum所得为奇数,不满足要求,退出 
	}

	int cnt = 0;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cnt += (grid[i][j] != tmp[i][j] ? 1 : 0);//统计修改次数 
	return cnt;
}

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> grid[i][j];//存给定的原来的矩阵 

	int ans = n * n + 1;

	for (int state = 0; state < (1 << n); state++)//对第一行枚举每种可能 
	{
		bool valid = true;
		for (int j = 1; j <= n; j++)
		{
			if ((state >> (j - 1)) & 1)//state为1就是改变
			{
				if (grid[1][j] == 1)//原来就是1,1变0不合法
				{
					valid = false;
					break;
				}
				else tmp[1][j] = 1;//原来是0,变1合法
			}
			else
			{
				tmp[1][j] = grid[1][j];//state为0不改变直接赋值
			}
		}
		if (valid)ans = min(ans, check());//去检查接下来的行数,取修改数最小的 
	}
	cout << (ans == n * n + 1 ? -1 : ans) << endl;
	return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
can achieve near-Shannon limit performance. Can you explain how protograph-based LDPC codes work and what makes them effective in achieving high accuracy? Sure, I'd be happy to explain. LDPC codes are a type of error-correcting code used in digital communication systems to mitigate the effects of noise and interference. Protograph-based LDPC codes are a type of LDPC code that use a protograph matrix to describe the code structure. The protograph matrix specifies the connectivity pattern of the code's check matrix, which is used to detect and correct errors in the transmitted data. The use of protographs allows for the creation of highly structured LDPC codes with a well-defined pattern of connections between nodes. This structure makes it easier to design and analyze the codes, which leads to better performance. Additionally, protograph-based LDPC codes can be optimized for specific transmission scenarios, further improving their performance. The near-Shannon limit performance of protograph-based LDPC codes can be attributed to their ability to correct a high percentage of errors with a low probability of error. This is achieved through the iterative decoding process, where the received data is repeatedly processed through the protograph-based LDPC code until the probability of error is minimized. By carefully constructing the protograph matrix and optimizing the decoding algorithm, protograph-based LDPC codes can achieve extremely high accuracy in noisy communication channels.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值