NC53676「土」秘法地震
题目地址:
基本思路:
非常裸的一道二维前缀和,我们做出这个矩阵的二维前缀和,每次判断是否会停止施法就是了。关于二维前缀和的求法为递推式:,我们可以大脑中想象一下,应该很容易想到这个递推式是合理的,然后二维前缀和的查询方式,假设我们要找,,到,这部分子矩阵的和那么答案就是 这个大家可以自己画图辅助理解。(我拿鼠标画图疯狂手抖,就不画了OAO)
参考代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define IO std::ios::sync_with_stdio(false) #define ll long long #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, _) memset(s, _, sizeof(s)) #define pb push_back #define pii pair <int, int> #define mp(a, b) make_pair(a, b) #define INF 0x3f3f3f3f inline int read() { int x = 0, neg = 1; char op = getchar(); while (!isdigit(op)) { if (op == '-') neg = -1; op = getchar(); } while (isdigit(op)) { x = 10 * x + op - '0'; op = getchar(); } return neg * x; } inline void print(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } const int maxn = 1010; char c; int n,m,k,sum[maxn][maxn]; signed main() { IO; cin >> n >> m >> k; mset(sum,0); rep(i,1,n){ rep(j,1,m) cin >> c,sum[i][j] = (c - '0'); } rep(i,1,n){ rep(j,1,m) sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]; } int ans = 0; rep(i,k,n){ rep(j,k,m){ int res = sum[i][j] - sum[j-k][j] - sum[i][j-k] + sum[i-k][j-k]; if(res > 0) ans++; } } cout << ans << '\n'; return 0; }