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 ...
随机推荐
- Android EditText圆角的方法
一.在drawable下面添加xml文件rounded_editview.xml <?xml version="1.0" encoding="utf-8" ...
- MFC radio button 绑定变量用法
我们在对话框中拖动一个radio button控件,然后点击类向导,结果却发现在Member Variables里看不到Radio控件的ID.这是为什么? 2.1 三个Radio Button,ID分 ...
- HDOJ-1012 u Calculate e(水)
http://acm.hdu.edu.cn/showproblem.php?pid=1012 简单套公式 # include <stdio.h> double Factorial(doub ...
- nginx编译配置
1, 正向代理是一个位于内网客户端和外网原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标,然后由代理服务器向 原始服务器转交请求并将获得的内容返回给客户端.正向代理 ...
- 一起来学canvas (前言)
codeyy - 一依丫丫 一起来学canvas (前言)
- Cocos2d-X中实现菜单特效
Cocos2d-X中能够讲菜单和动作结合起来使用实现菜单特效 程序实例1:使用菜单和动作的组合实现菜单特效<一> #include "MenuItem.h" CCSce ...
- ssh登录命令(转)
转:http://blog.csdn.net/edward_qing_lee/article/details/23133331 常用格式:ssh [-l login_name] [-p port] [ ...
- linux系统如何限制远程登录ip
在Linux系统上限制远程登录的IP,使用系统自带的配置文件. /etc/hosts.allow /etc/hosts.deny 匹配原则 先allow 后deny. 要求: 只允许 192.168 ...
- onvif规范的实现:成功实现ONVIF协议RTSP-Video-Stream与OnvifDeviceManager的视频对接
有了前几篇的基础,现在可以正式开始onvif的实现工作,其中一项非常重要的部分就是视频流的对接,即能够在符合onvif标准的监控客户端软件里接收到设备端NVT发来的RTSP视频流.这里,我所用的客户端 ...
- MVC学习 (一)
在学习MVC之前对asp.net MVC已经有了一些了解,但是还是有很多的疑问,接下来我慢慢来看书学习并带着问题写博客以作记录. 1.MVC是什么? 2.Asp.net MVC和传统的Asp.net ...