Determine the Photo Position —— 1D

时间:2021-08-12
本文章向大家介绍Determine the Photo Position —— 1D,主要包括Determine the Photo Position —— 1D使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Determine the Photo Position

题目描述

给定一个\(n\times n\)的01矩阵\(A\),再给定一个\(1 \times m\)的矩阵\(B\),把\(B\)贴到\(A\)中连续的1上,问有几种贴法。

范围

\(n,m \leq 2000\)

题解

模拟

#include <bits/stdc++.h>
using namespace std;

const int N = 2010;

char mp[N][N];
char t[N];
int cl[N][N];
int s[N][N];
bool ok (int l,int r,char *p) {
	for(int i = l;i <= r; ++i) {
		if(p[i] != 0) return 0;
	}
	return 1;
}

int main () {
	int n,m;
	cin >> n >> m;
	if(m > n) {
		puts("0");
	}
	else {
		int ans = 0;
		for(int i = 1;i <= n; ++i) scanf("%s",mp[i] + 1);scanf("%s",t + 1);
		for(int i = 1;i <= n; ++i) {
			for(int j = 1;j <= n; ++j) {
				cl[i][j] = mp[i][j] - '0';
			}
		}
		for(int i = 1;i <= n; ++i) {
			s[i][1] = cl[i][1];
			for(int j = 2;j <= n; ++j) {
				s[i][j] += s[i][j - 1] + cl[i][j];
			}
		}
		for(int i = 1;i <= n; ++i) {
			for(int j = 1;j <= n; ++j) {
				if(cl[i][j] == 0) {
					if(j + m - 1 <= n) {
						int tmp = s[i][j + m - 1] - s[i][j - 1];
						if(!tmp) {
							//cout << i << ' ' << j << endl;
							ans ++;
						}
					}
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

原文地址:https://www.cnblogs.com/akoasm/p/15132157.html