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 
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;
}

相关