202206-2 寻宝!大冒险!
题目描述
题解
由于n<=1000,S<=50,L<10^9 如果直接模拟整个地图的话肯定是不太行,题目中说了,藏宝图的左下角必为一颗树,所以可以以每棵树为原点,生成一个与藏宝图大小相同的绿化树副本,然后判断每个点的值是否相等,这么做的话时间复杂度是N*S^2 ,大概是10^5,不会超时,两个二维数组也很小,所以直接开干
代码
import java.util.Scanner;
public class Main {
static int[][] trees = new int[1024][2];
static int[][] trasurea = new int[52][52];
static int[][] temp = new int[52][52];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int L = sc.nextInt();
int S = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt(), y = sc.nextInt();
trees[i] = new int[]{x,y};
}
for (int i = S; i >= 0; i--) {
for (int j = 0; j <= S; j++) {
trasurea[i][j] = sc.nextInt();
}
}
int res = 0;
for (int i = 0; i < n; i++) {
int startX = trees[i][0];
int startY = trees[i][1];
if (startX >= L - (S - 1) || startY>= L - (S - 1)) {
continue;
}
for (int j = 0; j <= S; j++) {
for (int k = 0; k <= S; k++) {
temp[j][k] = 0;
}
}
for (int j = 0; j < n; j++) {
int tempx = trees[j][0] - startX;
int tempy = trees[j][1] - startY;
if (tempx <= S && tempx >= 0 && tempy <= S && tempy >= 0) {
temp[tempx][tempy] = 1;
}
}
boolean flag = true;
for (int j = 0; j <= S; j++) {
for (int k = 0; k <= S; k++) {
if (trasurea[j][k] != temp[j][k]) {
flag = false;
break;
}
}
}
if (flag) {
res++;
}
}
System.out.println(res);
}
}