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. 关于Keil C51中using关键字的使用心得

    刚才看到一位很牛的师兄写的一篇日志中提到了Keil C51中using这个关键字的用法,粗心的我本来一直都没有留意它是用来干嘛的(因为我一般看见它都是在中断服务函数的定义开头处,好像没有了它也可以中断 ...

  2. 直接地址跳转C实现

    <C缺陷和陷阱>讲过的一种方法: ( *( void (*)() ) 0 )(); //跳转到0地址执行 解析: 1.void (*p_fun)(void); //声明函数指针 2.voi ...

  3. html 知识

    <pre name="code" class="python"><pre name="code" class=" ...

  4. 转:Dynamic Binding Of RDLC To ReportViewer

    Introduction I was struggling to find the solution to bind rdlc dynamically to reportviewer .We had ...

  5. UESTC_温泉旅店 CDOJ 878

    天空飘下一朵一朵的雪花,这是一片纯白的世界. 在天空之下的温泉旅店里,雪菜已醉倒在一旁,冬马与春希看了看说着梦话的雪菜,决定找一点玩的来度过这愉快的晚上. 这家旅店提供一种特色游戏,游戏有n张牌,各写 ...

  6. sh_脚本语法

    介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编写脚 ...

  7. jQuery支持移动Mobile的DOM元素移动和缩放插件

    jQuery Panzoom是一款很有用的HTML DOM元素平移和缩放jQuery和CSS3插件. Panzoom利用CSS transforms 和 matrix函数来为浏览器进行硬件(GPU)加 ...

  8. UGUI实现的虚拟摇杆,可改变摇杆位置

    实现方式主要参考这篇文章:http://www.cnblogs.com/plateFace/p/4687896.html. 主要代码如下: using UnityEngine; using Syste ...

  9. Oracle SQL函数之转换函数

    chartorowid(c1) [功能]转换varchar2类型为rowid值 [参数]c1,字符串,长度为18的字符串,字符串必须符合rowid格式 [返回]返回rowid值 [示例] SQL> ...

  10. eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。

    今天遇到上面的情况.网上找了半天都没有找到解决的办法.后来,仔细比对了一下我的eclipse版本和svn版本.发现svn版本真的太老了.用上新的svn后,立马就可以用了 svn - http://su ...