C/C++教程

CSP_202206-2_寻宝!大冒险!

本文主要是介绍CSP_202206-2_寻宝!大冒险!,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

CSP_202206-2_寻宝!大冒险

题目链接

思路

相当于判断两个有限集合AB之间是不是满射和单射,只需要保证以下两点

  1. A和B元素个数相等
  2. A中每个元素都能通过映射\(\psi\)到B中一个元素,且\(\psi(a_1)=\psi(a_2) \iff a_1 = a_2\)

输入的矩阵格式和我们平常看到的坐标系,xy轴是反过来的

#include<iostream>
using namespace std;
int main() {
	int n, L, S;
	scanf("%d%d%d", &n, &L, &S);
	int A[n][2];
	int B[S+1][S+1];
	int t_in_b = 0, ans = 0; // number of trees in B 
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &A[i][0], &A[i][1]);
	}

	for (int i = 0; i < S+1; i++) {
		for (int j = 0; j < S+1; j++) {
			scanf("%d", &B[S-i][j]);
			if (B[S-i][j] == 1) t_in_b++;
		}
	}

	for (int i = 0; i < n; i++) {
		// tree out of B
		if (A[i][0] + S > L || A[i][1] + S > L) {
			continue;
		}
		// 
		int t_in_a = 0;
		for (int j = 0; j < n; j++) {
			int x = A[j][0] - A[i][0];
			int y = A[j][1] - A[i][1];
			if (0 <= x && x <= S && 0 <= y && y <= S) t_in_a += 1;
		}
		if (t_in_a != t_in_b) continue; // tree num not same
	
		bool flag = true;
		for (int j = 0; j < n; j++) {
			int m = A[j][0] - A[i][0];
			int n = A[j][1] - A[i][1];
			if (0 <= m && m <= S && 0 <= n && n <= S){
				if (B[m][n] == 1) continue;
				else {
					flag = false;
					break;
				}
			}
		}
		if (flag) ans += 1;
		else continue;
	}

	printf("%d", ans);
	return 0;
}
这篇关于CSP_202206-2_寻宝!大冒险!的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!