提交时间:2023-01-20 16:55:20

运行 ID: 67878

#include <bits/stdc++.h> using namespace std; const int N = 1505; typedef long long ll; int n; char ch; bool t[N][N]; // 第i行中第j列及之后第一个出现1的位置,第i列中第j行及之后第一个出现1的位置 int earliestOccurrencePositionX[N][N], earliestOccurrencePositionY[N][N]; // ll calc(int x0, int y0) { // int theNumberOfAllowedOnTheXAxis = // earliestOccurrencePositionY[y0][x0] - x0 - 3; // if (theNumberOfAllowedOnTheXAxis < 1) return 0; // } ll calc2(int x0, int y0) { ll ans = 0; for (int p = 2; p < min(earliestOccurrencePositionY[y0][x0], earliestOccurrencePositionX[x0][y0]) - 1; p++) { if (p >= earliestOccurrencePositionX[x0 + p][y0]) continue; ans += ll(earliestOccurrencePositionY[y0][x0 + p] - 1) * earliestOccurrencePositionX[x0][y0 + p]; } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf(" %c", &ch); t[i][j] = ch - '0'; } } for (int i = 1; i <= n; i++) { int sh = n + 1; for (int j = n; j >= 1; j--) { if (t[i][j]) sh = j; earliestOccurrencePositionX[i][j] = sh - j; } } for (int j = 1; j <= n; j++) { int sh = n + 1; for (int i = n; i >= 1; i--) { if (t[i][j]) sh = i; earliestOccurrencePositionY[j][i] = sh - i; } } ll ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ans += calc2(i, j); } } printf("%lld\n", ans); return 0; } /* 000010 010000 100000 000001 000001 001001 */