Counting square
Problem Description
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
Output
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的更多相关文章
- SPOJ 20713 DIVCNT2 - Counting Divisors (square)
DIVCNT2 - Counting Divisors (square) #sub-linear #dirichlet-generating-function Let \sigma_0(n)σ0 ...
- [SPOJ] DIVCNT2 - Counting Divisors (square) (平方的约数个数前缀和 容斥 卡常)
题目 vjudge URL:Counting Divisors (square) Let σ0(n)\sigma_0(n)σ0(n) be the number of positive diviso ...
- 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&= ...
- SP20173 DIVCNT2 - Counting Divisors (square)
Refer 主要思路参考了 Command_block 的题解. Description 给定 \(n\)(\(n\le 10^{10}\)),求 \[\sum_{i=1}^n\sigma_0(i^2 ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- HDU 1264 Counting Squares(线段树求面积的并)
Counting Squares Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HackerRank "Square Subsequences" !!!
Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...
- XidianOJ 1177 Counting Stars
题目描述 "But baby, I've been, I've been praying hard, Said, no more counting dollars We'll ...
随机推荐
- BZOJ 2876 骑行川藏
http://www.lydsy.com/JudgeOnline/problem.php?id=2876 拉格朗日乘数法:f'+入g'=0,f为函数的导数,g为限制条件的导数. 思路:E=Σki*si ...
- SWFUpload批量上传插件
SWFUpload是一个批量上传插件,在HTML4.1里面,估计也只有Flash+javascript配合才能够做到了.先复制个重要的网址,这个应该是官方的文档了,相当齐全. http://leeon ...
- javascript之window对象
window :window对象是BOM中所有对象的核心,除了是BOM中所有对象的父对象外,还包含一些窗口控制函数. 1.全局的window对象 JavaScript中的任何一个全局函数或变量都是wi ...
- python hook监听事件
python hook监听事件 作者:vpoet mail:vpoet_sir@163.com # -*- coding: utf-8 -*- # # by oldj http://oldj.net/ ...
- Bring Your Charts to Life with HTML5 Canvas and JavaScript
Bring Your Charts to Life with HTML5 Canvas and JavaScript Bring Your Charts to Life with HTML5 Canv ...
- java.lang.UnsatisfiedLinkError: no XXX in java.library.path
其中涉及的测试源码如下: For those who didn't install Javawith default settings, a systematic way for solving JN ...
- SQL 通配符
在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符.SQL 通配符必须与 LIKE 运算符一起使用,必须放在引号内. 在 SQL 中,可使用以下通配符: %:替代一个或多个字符. _:仅替代 ...
- svn local delete, incoming delete upon update 解决办法
经常有人会说,树冲突是很难解决的一类冲突,其实一旦了解了其原理,要解决也不难.先回顾下对于树冲突的定义. 树冲突:当一名开发人员移动.重命名.删除一个文件或文件夹,而另一名开发人员也对它们进行 ...
- Unity 角色复活和重新开始游戏
作者写游戏完成的时候,还需要从新想下如何把游戏设置重新开始,角色如何复活. 一般大多数都会采用这种方式来代替游戏重新开始 Application.LoadLevel("xxx场景" ...
- Entity Framework with MySQL
Get Entity Framework: http://msdn.microsoft.com/en-us/data/ee712906 Entity Framework 6 Tools for Vis ...