In the fig 01, there are several squares with an edge of 1. Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4331
# Include <cstdio> # include <iostream> # include <cstring> # include <cmath> # include <algorithm> # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 # define maxn 1010int t, n, grid [maxn] [maxn], d [maxn] [maxn], U [maxn] [maxn]; int R [maxn] [maxn], C [maxn] [maxn], sum [maxn <2]; using namespace STD; void build () {memset (sum, 0, sizeof (SUM);} void Update (INT POs, int l, int R, int RT) {++ sum [RT]; If (L = r) return; int M = (L + r)> 1; if (Pos <= m) Update (Pos, lson); else Update (Pos, rson);} int query (INT POs, int L, int R, int RT) {If (L = r) return sum [RT]; int M = (L + r)> 1; int ret = 0; If (Pos <= m) RET + = query (Pos, lson) + sum [RT <1 | 1]; else if (Pos> m) RET + = query (Pos, rson ); return ret;} struct data {int P, X;} G [maxn * 2]; bool CMP (Data A, data B) {return. P <B. p | (. P = B. P &. x <B. x) ;}for (INT I = N; I> 1; -- I) {ans + = solve (I, 1, n-I + 1 );} for (INT I = 1; I <= N; I ++) {ans + = solve (1, I, n-I + 1);} int solve (INT XX, int YY, int N) {int e = 0, m = 0, ret = 0; For (int K = 1, I = XX, j = YY; k <= N; k ++, I ++, J ++) {If (grid [I] [J]) {G [M]. P = k-U [I] [J], G [M ++]. X =-K; G [M]. P = K, G [M ++]. X = K ;}} sort (G, G + M, CMP); Build (); For (int K = 1, I = XX, j = YY; k <= N; k ++, I ++, J ++) {If (grid [I] [J]) {Update (K + d [I] [J]-1, 1, n, 1);} while (E <M & G [e]. P <= k) {If (G [e]. P = k) {If (G [e]. x <0) ret-= query (-G [e]. x, 1, n, 1); else RET + = query (G [e]. x, 1, n, 1) ;}++ e ;}return RET ;}int main () {scanf ("% d", & T ); for (int ca = 1; Ca <= T; CA ++) {scanf ("% d", & N); For (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {scanf ("% d ", & grid [I] [J]) ;}} memset (R, 0, sizeof (R); memset (C, 0, sizeof (c )); for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {If (grid [I] [J]) {R [I] [J] = R [I] [J-1] + 1; c [I] [J] = C [I-1] [J] + 1; U [I] [J] = min (R [I] [J], c [I] [J]) ;}} memset (R, 0, sizeof (R); memset (C, 0, sizeof (c )); for (INT I = N; I> 0; I --) {for (Int J = N; j> 0; j --) {If (grid [I] [J]) {R [I] [J] = R [I] [J + 1] + 1; c [I] [J] = C [I + 1] [J] + 1; d [I] [J] = min (R [I] [J], c [I] [J]) ;}} int ans = 0; For (INT I = N; I> 1; -- I) {ans + = solve (I, 1, n-I + 1) ;}for (INT I = 1; I <= N; I ++) {ans + = solve (1, I, n-I + 1);} printf ("case % d: % d \ n", CA, ANS);} return 0 ;}