Sicily 1918. Counting square

本文介绍了一种算法,用于计算给定矩阵中符合特定条件的魔法方阵数量。魔法方阵要求边界元素均为1,内部0和1的数量相差不超过1,且大小至少为2x2。文章提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1918. Counting square

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

 There is a matrix of size R rows by C columns. Each element in the matrix is either `0' or `1'. A square is called magic square if it meets the following three conditions.

The elements on the four borders are all `1'.
Inside the square (excluding the elements on the borders), the number of 1's and the number of 0's are different at most by 1.
The size of the square is at least 2 by 2. Now given the matrix, please tell me how many magic square are there in the matrix.

Input

 The input begins with a line containing an integer T , the number of test cases. Each case begins with two integers R , C (1 ≤ R, C ≤ 300) , representing the size of the matrix. Then R lines follow. Each contains C integers, either `0' or `1'. The integers are separated by a single space.

Output

 For each case, output the number of magic square in a single line.

Sample Input

3 
4 4 
1 1 1 1 
1 0 1 1 
1 1 0 1 
1 1 1 1 
5 5 
1 0 1 1 1 
1 0 1 0 1 
1 1 0 1 1  
1 0 0 1 1 
1 1 1 1 1 
2 2 
1 1 
1 1

Sample Output

3 
2 

1

// Problem#: 1918
// Submission#: 3577951
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>

short G[305][305];
short row1[305][305], col1[305][305];
int sum[305][305];
int h, w;
int ans;

void init() {
    int last1;
    for (int i = 0; i < h; i++) {
        last1 = -1;
        for (int j = w - 1; j >= 0; j--) {
            if (G[i][j] == -1) last1 = -1;
            else if (last1 == -1) last1 = j;
            row1[i][j] = last1;
        }
    }
    for (int i = 0; i < w; i++) {
        last1 = -1;
        for (int j = h - 1; j >= 0; j--) {
            if (G[j][i] == -1) last1 = -1;
            else if (last1 == -1) last1 = j;
            col1[j][i] = last1;
        }
    }
    sum[0][0] = G[0][0];
    for (int i = 1; i < h; i++) sum[i][0] = sum[i - 1][0] + G[i][0];
    for (int i = 1; i < w; i++) sum[0][i] = sum[0][i - 1] + G[0][i];
    for (int i = 1; i < h; i++) 
        for (int j = 1; j < w; j++) 
            sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + G[i][j];
}

inline int abs(int a) {if (a < 0) return -a; return a;}

inline void cal(int si, int sj) {
    int li = si + 1, lj = sj + 1;
    while (li < h && lj < w && G[si][lj] == 1 && G[li][sj] == 1) {
        if (lj <= row1[li][sj] && li <= col1[si][lj])
            if (abs(sum[li - 1][lj - 1] - sum[si][lj - 1] - sum[li - 1][sj] + sum[si][sj]) <= 1) ans++;
        lj++;
        li++;
    }
}

int main() {
    int caseNum;
    scanf("%d", &caseNum);
    while (caseNum--) {
        scanf("%d%d\n", &h, &w);
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                scanf("%d", &G[i][j]);
                if (G[i][j] == 0) G[i][j] = -1;
            }
        }
        ans = 0;
        init();
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) if (G[i][j] == 1) cal(i, j);
        printf("%d\n", ans);
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值