PIPIOJ 1168: PIPI的方格

1168: PIPI的方格

题目描述

PIPI有一个N*N的方格,每个格子中有一个数字(0或者1),PIPI可以使任意格子中的0变成1,现在它想让每个格子的上下左右相邻格子中数字之和为偶数。
请你告诉PIPI,最少使用几次操作(将0变成1),才能使每个格子的上下左右(如果存在)相邻数字和为偶数。

输入

多组数据
第一行为一个正整数n,n<=15
接下来n行,每行n个数字(0或者1)。

输出

对于每组数据,输出一个整数,代表最少需要多少次操作。
若无解,输出-1.

样例输入

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

样例输出

0
3
-1

思路 1

此题关键在于确定第一行后,整个方格都会被确定(第 i 行由第 i - 1 行确定)。故使用二进制枚举法枚举第一行所有可能的状态,填充方格,判断合法性,统计变换次数即可。

#include<bits/stdc++.h>
using namespace std;
int bef[20][20], aft[20][20], n;
int main()
{
	while(scanf("%d", &n) != EOF)
	{	
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				scanf("%d", &bef[i][j]);
				
		int t = 1 << n, ans = -1;
		
		// 遍历第一行的 2^n - 1 种情况 
		for(int i = 0; i < t; i++)
		{
			for(int j = 1; j <= n; j++)
				for(int k = 1; k <= n; k++)
					aft[j][k] = bef[j][k];

			int cnt = 0;	
			bool flag = true;
			// 获取第一行 
			for(int j = 0; j < n; j++)
			{
				aft[1][n - j] = (i >> j) & 1;
				if(bef[1][n - j] != aft[1][n - j])
				{
					if(bef[1][n - j] == 0)
						cnt++;
					else
					{
						flag = false;
						break;
					}
				}
					
			}
			if(!flag)
				continue;
			// 填充剩下的行 
			for(int j = 1; j < n; j++)
			{
				for(int k = 1; k <= n; k++)
				{
					if((aft[j - 1][k] + aft[j][k - 1] + aft[j][k + 1] + aft[j + 1][k]) & 1 == 1)
					{
						if(aft[j + 1][k] == 0)
						{
							aft[j + 1][k] = 1;
							cnt++;	
						}
						else
						{
							flag = false;
							break;
						}
					}
				}
				if(!flag)
					break;
			}
			if(!flag)
				continue;
			if(ans == -1 || cnt < ans)
				ans = cnt;
		}
		printf("%d\n", ans);
	}
	return 0;
}

思路 2

回溯法:采用回溯法枚举第一行的状态,对于每一种第一行可能的状态再填充剩余行,回溯过程中有剪枝,效率更高。

#include<bits/stdc++.h>
using namespace std;
const int N = 20;
int n, a[N][N], cnt, ans, b[N][N];
void dfs(int x)
{
	if(x == n + 1)
	{
		// 第一行已经生成,继续填充剩余行
		for(int i = 1; i < n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				if((b[i - 1][j] + b[i][j - 1] + b[i][j + 1] + a[i + 1][j]) & 1 == 1)
				{
					if(a[i + 1][j] == 0)
					{
						cnt++;
						b[i + 1][j] = 1;
					}
					else
					{
						cnt = 0;
						return;
					}	
				}
			}
		}
		ans = min(cnt, ans);
		cnt = 0;
		return;
	}
    // 剪枝
	if(a[1][x] != 1)	
	{
		b[1][x] = 0;
		dfs(x + 1);
	}
	if(a[1][x] == 0)
		cnt++;
	b[1][x] = 1;
	dfs(x + 1);
}
int main()
{
	while(scanf("%d", &n) != EOF)
	{
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				scanf("%d", &a[i][j]);
		cnt = 0;
		ans = (n * n * 2);
		dfs(1);
		printf("%d\n", ans == (n * n * 2) ? -1 : ans);
	}
	return 0; 
}
### PIPI 数学问题解决方案 针对与 PIPI 相关的数学问题,通常可以通过设计特定的算法来解决。以下是基于常见数学问题类型的通用方法及其代码实现。 #### 字符串模拟类问题 如果问题是关于字符串操作(如引用中的第一题),则可以利用 Python 的字符串处理功能快速解决问题。例如: ```python def string_simulation(s, operation): result = [] for char in s: if operation == 'reverse': result.insert(0, char) elif operation == 'uppercase': result.append(char.upper()) return ''.join(result) # 测试用例 print(string_simulation("pipi", "reverse")) # 输出:ipip print(string_simulation("pipi", "uppercase")) # 输出:PIPI ``` 此函数支持两种基本操作:“反转”和“大写转换”,可以根据具体需求扩展更多逻辑[^2]。 #### 复杂数学计算问题 对于更复杂的数学问题,可能涉及到矩阵运算或数值优化等问题。这类问题往往需要借助 NumPy 库或其他科学计算工具包完成高效计算。以下是一个简单的矩阵乘法示例: ```python import numpy as np def matrix_multiplication(matrix_a, matrix_b): try: result_matrix = np.dot(matrix_a, matrix_b) return result_matrix.tolist() except ValueError: return "Matrix dimensions do not match." # 测试用例 matrix_a = [[1, 2], [3, 4]] matrix_b = [[5, 6], [7, 8]] print(matrix_multiplication(matrix_a, matrix_b)) # 输出:[[19, 22], [43, 50]] ``` 该函数实现了两个二维数组之间的矩阵乘法,并返回结果作为列表形式输出[^1]。 #### 推理与预测模型应用 当遇到涉及机器学习推理的任务时,应优先考虑简化模型架构以降低延迟时间。比如加载预训练好的 TensorFlow Lite 模型来进行实时分类判断: ```python import tensorflow as tf def load_and_predict(model_path, input_data): interpreter = tf.lite.Interpreter(model_path=model_path) interpreter.allocate_tensors() input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() prediction_result = interpreter.get_tensor(output_details[0]['index']) return prediction_result # 假设 model.tflite 已存在且输入维度已知 input_sample = np.random.rand(1, 28, 28).astype(np.float32) result = load_and_predict('model.tflite', input_sample) print(result.shape) # 打印输出张量形状 ``` 上述脚本展示了如何通过 TensorFlow Lite 加载轻量化模型执行推断过程。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值