Problem 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
 #include <cstdio>
#include <cstring>
#include <cstdlib> const int maxn = ;
const int inf = <<;
int mat[maxn][maxn];
int sum[maxn][maxn];
int cnt,n,m; bool check(int x1, int y1, int x2, int y2)
{
int a = sum[x2][y2] - sum[x1-][y2] - sum[x2][y1-] + sum[x1-][y1-];
int b = sum[x2-][y2-] - sum[x1][y2-] - sum[x2-][y1] + sum[x1][y1];
int tmp = (y2-y1+)* + (x2-x1+)* - ;
if(a-b != tmp) return false;
int s = (y2-y1-) * (x2-x1-);
if(abs(s-*b) > ) return false;
return true;
}
void dfs(int x1, int y1, int l)
{
int x2 = x1+l, y2 = y1+l;
if(x2>n || y2>m) return;
if(check(x1, y1, x2, y2)) cnt++;
dfs(x1, y1, l+);
} int main()
{
int T,i,j;
scanf("%d", &T);
while(T--)
{
cnt = ;
scanf("%d%d", &n, &m);
memset(sum,,sizeof(sum));
for(i=; i<=n; i++)
for(j=; j<=m; j++)
{
scanf("%d", &mat[i][j]);
sum[i][j] = sum[i-][j] + sum[i][j-] - sum[i-][j-] + mat[i][j];
}
for(i=; i<n; i++)
for(j=; j<m; j++)
if(mat[i][j]) dfs(i, j, );
printf("%d\n", cnt);
}
return ;
}

递归遍历(约束条件)

Counting square的更多相关文章

  1. SPOJ 20713 DIVCNT2 - Counting Divisors (square)

    DIVCNT2 - Counting Divisors (square) #sub-linear #dirichlet-generating-function Let \sigma_0(n)σ​0​​ ...

  2. [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)

    题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0​(n) be the number of positive diviso ...

  3. SPOJ : DIVCNT2 - Counting Divisors (square)

    设 \[f(n)=\sum_{d|n}\mu^2(d)\] 则 \[\begin{eqnarray*}\sigma_0(n^2)&=&\sum_{d|n}f(d)\\ans&= ...

  4. SP20173 DIVCNT2 - Counting Divisors (square)

    Refer 主要思路参考了 Command_block 的题解. Description 给定 \(n\)(\(n\le 10^{10}\)),求 \[\sum_{i=1}^n\sigma_0(i^2 ...

  5. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  6. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  7. HDU 1264 Counting Squares(线段树求面积的并)

    Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  9. XidianOJ 1177 Counting Stars

    题目描述 "But baby, I've been, I've been praying hard,     Said, no more counting dollars     We'll ...

随机推荐

  1. KeyTool

    http://ln-ydc.iteye.com/blog/1335213 http://lukejin.iteye.com/blog/605634

  2. 线程初步了解 - <第一篇>

    操作系统通过线程对程序的执行进行管理,当操作系统运行一个程序的时候,首先,操作系统将为这个准备运行的程序分配一个进程,以管理这个程序所需要的各种资源.在这些资源之中,会包含一个称为主线程的线程数据结构 ...

  3. UESTC_Sea Base Exploration CDOJ 409

    When the scientists explore the sea base, they use a kind of auto mobile robot, which has the missio ...

  4. MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored

    MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored

  5. 使用Canvas实现动画效果 | DKlogs -- 设计 | 生活

    使用Canvas实现动画效果 | DKlogs -- 设计 | 生活 使用Canvas实现动画效果

  6. Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8010977 前面我们从Android应用程序与 ...

  7. 【Samza系列】实时计算Samza中文教程(二)——概念

    希望上一篇背景篇让大家对流式计算有了宏观的认识,本篇依据官网是介绍概念,先让我们看看有哪些东西呢?     概念一:Streams     Samza是处理流的.流则是由一系列不可变的一种相似类型的消 ...

  8. 什么是目标、度量、KPI、维度和细分

    今天看到了Avinash的一篇文章:Web Analytics 101: Definitions: Goals, Metrics, KPIs, Dimensions, Targets,正是我想在影响网 ...

  9. JQ动画,背景透明度

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. windows驱动编程(目录)

    目录 第一章 入门 配置开发环境 第一个程序 应用程序调用内核函数的流程