题目链接:

http://codeforces.com/contest/435/problem/D

D. Special Grid

time limit per test:4 seconds
memory limit per test:256 megabytes
#### 问题描述
> You are given an n × m grid, some of its nodes are black, the others are white. Moreover, it's not an ordinary grid — each unit square of the grid has painted diagonals.
>
> The figure below is an example of such grid of size 3 × 5. Four nodes of this grid are black, the other 11 nodes are white.
>
>
> Your task is to count the number of such triangles on the given grid that:
>
> the corners match the white nodes, and the area is positive;
> all sides go along the grid lines (horizontal, vertical or diagonal);
> no side contains black nodes.
#### 输入
> The first line contains two integers n and m (2 ≤ n, m ≤ 400). Each of the following n lines contain m characters (zeros and ones) — the description of the grid. If the j-th character in the i-th line equals zero, then the node on the i-th horizontal line and on the j-th vertical line is painted white. Otherwise, the node is painted black.
>
> The horizontal lines are numbered starting from one from top to bottom, the vertical lines are numbered starting from one from left to right.

输出

Print a single integer — the number of required triangles.

样例

sample input

3 5

10000

10010

00001

sample output

20

题意

给你一个网格(网格有对角线),问由网格的边和对角线构成的三角形(三条边上都不能有任何黑点)个数。

题解

其实满足要求的三角形只有等腰直角三角形

对每个点,我们先预处理出八个方向能延伸出去的最大长度。(预处理出来这些,我们就可以判断一个直角三角形的三条边上是否有黑点了。比如对于三角形0 + 3 的斜边,你只要考虑0的左上角能延伸的最大长度或者3的右下角能延伸的最大长度就可以了)

然后枚举每个点,统计以这个点为直角的所有三角形的个数

对8个方向做一个说明:
7 3 4
\ | /
\ | /
\|/
2---+---0
/|\
/ | \
/ | \
6 1 5 八个方向组成的以'+'为原点,长度为1的直角:
0 -> + -> 1
+-
| 1 -> + -> 2
-+
| 2 -> + -> 3
|
-+ 3 -> + -> 0
|
+- 4 -> + -> 5
/
+
\ 5 -> + -> 6
+
/ \ 6 -> + -> 7
\
+
/ 7 -> + -> 4
\ /
+

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 444; char str[maxn][maxn]; //dp[i][j][d]记录(i,j)顶点从八个方向扩散出去的长度(这里的长度是按顶点数来度量的)。
int dp[maxn][maxn][8]; typedef __int64 LL; int n, m; int main() {
scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", str[i] + 1); for (int i = 0; i < maxn; i++) str[i][0] = str[0][i] = str[n + 1][i] = str[i][m + 1] = '1'; for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
//预处理出每个顶点八个方向能延伸出去的最大长度
for (int k = 0;; k++) if (str[i][j + k] == '1') {
dp[i][j][0] = k - 1; break;
}
for (int k = 0;; k++) if (str[i + k][j] == '1') {
dp[i][j][1] = k - 1; break;
}
for (int k = 0;; k++) if (str[i][j - k] == '1') {
dp[i][j][2] = k - 1; break;
}
for (int k = 0;; k++) if (str[i - k][j] == '1') {
dp[i][j][3] = k - 1; break;
}
for (int k = 0;; k++) if (str[i - k][j + k] == '1') {
dp[i][j][4] = k - 1; break;
}
for (int k = 0;; k++) if (str[i + k][j + k] == '1') {
dp[i][j][5] = k - 1; break;
}
for (int k = 0;; k++) if (str[i + k][j - k] == '1') {
dp[i][j][6] = k - 1; break;
}
for (int k = 0;; k++) if (str[i - k][j - k] == '1') {
dp[i][j][7] = k - 1; break;
}
}
} int ma = max(n, m);
LL ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int cnt = ans;
//这里的k可以理解为两直角边的长度。
for (int k = 1; k <= ma; k++) {
//计算以(i,j)这个顶点为直角的等腰直角三角形(总共有八个,看几个是合法的) //这四种情况中直角由网格的两条邻边构成
//这四种情况等腰直角三角形三边跨越的顶点数刚好都是相等的
if (k <= dp[i][j][0] && k <= dp[i][j][1] && k <= dp[i][j + k][6]) {
ans++;
}
if (k <= dp[i][j][1] && k <= dp[i][j][2] && k <= dp[i + k][j][7]) {
ans++;
}
if (k <= dp[i][j][2] && k <= dp[i][j][3] && k <= dp[i][j - k][4]) {
ans++;
}
if (k <= dp[i][j][3] && k <= dp[i][j][0] && k <= dp[i - k][j][5]) {
ans++;
} //这四种情况中直角由网格的两条相邻对角线构成
//这里开始斜边跨越的节点数刚好是直角边的两倍,所以斜边是2*k。
if (k <= dp[i][j][4] && k <= dp[i][j][5] && 2*k <= dp[i - k][j + k][1]) {
ans++;
}
if (k <= dp[i][j][5] && k <= dp[i][j][6] && 2*k <= dp[i + k][j + k][2]) {
ans++;
}
if (k <= dp[i][j][6] && k <= dp[i][j][7] && 2*k <= dp[i + k][j - k][3]) {
ans++;
}
if (k <= dp[i][j][7] && k <= dp[i][j][4] && 2*k <= dp[i - k][j - k][0]) {
ans++;
}
}
}
}
printf("%I64d\n", ans);
return 0;
} /*
3 3
001
010
000 3 4
0010
0100
0010 5 5
00000
01010
00000
01010
00000 4 4
0000
0111
0011
0111
*/

Codeforces Round #249 (Div. 2) D. Special Grid 枚举的更多相关文章

  1. 模拟 Codeforces Round #249 (Div. 2) C. Cardiogram

    题目地址:http://codeforces.com/contest/435/problem/C /* 题意:给一组公式,一组数据,计算得到一系列的坐标点,画出折线图:) 模拟题:蛮恶心的,不过也简单 ...

  2. 构造 Codeforces Round #135 (Div. 2) B. Special Offer! Super Price 999 Bourles!

    题目传送门 /* 构造:从大到小构造,每一次都把最后不是9的变为9,p - p MOD 10^k - 1,直到小于最小值. 另外,最多len-1次循环 */ #include <cstdio&g ...

  3. Codeforces Round #249 (Div. 2) C题,模拟画图 ----未解决!

    http://codeforces.com/contest/435/problem/C

  4. Codeforces Round #249 (Div. 2)B(贪心法)

    B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #249 (Div. 2) A题

    链接:http://codeforces.com/contest/435/problem/A   A. Queue on Bus Stop time limit per test 1 second m ...

  6. Codeforces Round #249 (Div. 2)

    A.水题. #include <cstdio> #include <iostream> #include <cstdlib> #include <cstrin ...

  7. Codeforces Round #249 (Div. 2) 总结

    D.E还是很难的.....C不想多说什么... A:提意:给出每一组人的个数,以及一次车载容量,求出最少需要多少次才能载走所有的人. water: http://codeforces.com/cont ...

  8. Codeforces Round #249 (Div. 2) (模拟)

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #249 (Div. 2) C. Cardiogram

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. VBA删除表格最后一行

    Sub 删除最后一行() If MsgBox("要为所有表格添加列吗?", vbYesNo + vbQuestion) = vbYes Then To ActiveDocument ...

  2. CentOS 6.4安装lnmp环境

    1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport ...

  3. 刚开始学IOS遇到的类和方法

    框架:Core FoundationCFGetRetainCount. 类:NSRunLoop.NSAutoreleasePool.NSStringFormClass.UIApplicationMai ...

  4. Android系统四层架构分享

    Android系统四层架构 个人网站:http://www.51pansou.com Android视频下载:Android视频 Android源码下载:Android源码 如果把Android系统看 ...

  5. GoldenGate中使用FILTER,COMPUTE 和SQLEXEC命令

    本文主要介绍OGG中一些过滤或计算函数的用法,以及sqlexec的基本用法 SQLPREDICATE 在使用OGG初始化时,可以添加此参数到extract中,用于选择符合条件的记录,下面是OGG官方文 ...

  6. Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:

    七月 17, 2014 4:56:01 下午 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service( ...

  7. input元素的padding border margin的区别

    padding内(不包含padding)的部分才是可输入部分,也是width和height标明的区域.padding的部分加上width和height部分是background的部分.padding的 ...

  8. spring mvc中的文件上传

    使用commons-fileupload上传文件所需要的架包有:commons-fileupload 和common-io两个架包支持,可以到Apache官网下砸. 在配置文件spring-mvc.x ...

  9. spring debug

    DispatcherServlet{ getHandler()}handlerMappings{ RequestMappingHandlerMapping BeanNameUrlHandlerMapp ...

  10. LN : leetcode 292 Nim Game

    lc 292 Nim Game 292 Nim Game You are playing the following Nim Game with your friend: There is a hea ...