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 ...
随机推荐
- KeyTool
http://ln-ydc.iteye.com/blog/1335213 http://lukejin.iteye.com/blog/605634
- 线程初步了解 - <第一篇>
操作系统通过线程对程序的执行进行管理,当操作系统运行一个程序的时候,首先,操作系统将为这个准备运行的程序分配一个进程,以管理这个程序所需要的各种资源.在这些资源之中,会包含一个称为主线程的线程数据结构 ...
- 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 ...
- MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
MySQL无法重启问题解决Warning: World-writable config file ‘/etc/my.cnf’ is ignored
- 使用Canvas实现动画效果 | DKlogs -- 设计 | 生活
使用Canvas实现动画效果 | DKlogs -- 设计 | 生活 使用Canvas实现动画效果
- Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8010977 前面我们从Android应用程序与 ...
- 【Samza系列】实时计算Samza中文教程(二)——概念
希望上一篇背景篇让大家对流式计算有了宏观的认识,本篇依据官网是介绍概念,先让我们看看有哪些东西呢? 概念一:Streams Samza是处理流的.流则是由一系列不可变的一种相似类型的消 ...
- 什么是目标、度量、KPI、维度和细分
今天看到了Avinash的一篇文章:Web Analytics 101: Definitions: Goals, Metrics, KPIs, Dimensions, Targets,正是我想在影响网 ...
- JQ动画,背景透明度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- windows驱动编程(目录)
目录 第一章 入门 配置开发环境 第一个程序 应用程序调用内核函数的流程