题目链接:

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. linux下vi的复制,黏贴,删除,撤销,跳转等命令

    前言     在嵌入式linux开发中,进行需要修改一下配置文件之类的,必须使用vi,因此,熟悉 vi 的一些基本操作,有助于提高工作效率. 一,模式vi编辑器有3种模式:命令模式.输入模式.末行模式 ...

  2. Regarding the %EDIT table

    %EDITTABLE is field in the work record DERIVED. This field is generally used a prompt table for vari ...

  3. Centos 7.1+CDH5.7.2全部署流程

    前期准备: JDK环境 版本:jdk-8u101-linux-x64.rpm 下载地址:oracle官网 mysql rpm包:http://dev.mysql.com/get/Downloads/M ...

  4. (转)浅谈HTML5与css3画饼图!

    神马系饼图? 饼图,大家都应该熟知,在统计数据对比方面,几乎处处用到.如cnzz的统计饼图 从饼图中,很形象地展示了访问者地区的分布,以扇形为块的方式拼成一个大圆. 都使用什么方法实现 目前众多站点制 ...

  5. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  6. asp.net中两款文本编辑器NicEdit和Kindeditor

    过Web开发的朋友相信都使用过富文本编辑器,比较出名的CuteEditor和CKEditor很多人应该已经使用过,在功能强大的同时需要加载的东西也变得很多.下面要推荐的两款富文本编辑器都是使用JS编写 ...

  7. ok6410的DMA裸机总结

    1.为何使用DMA:为了提高CPU的工作效率,避免多余的等待时间 2.关于DMA控制器:(1)通道数:2440有4个通道,6410有4个DMA控制器(初始化的时候要选择),32个通道.210有两种DM ...

  8. 1.html5究竟是什么

    1.html5的起源,历史背景…… 按照一般的套路,我这里应该对html5的起源和发展历史,其优越性等大书特书一番.但既然你有意识地专门去找类似的文章,说明你早有相应的认识,就算没有,类似的东西网上也 ...

  9. python 函数的参数对应

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经接触过函数(function)的参数(arguments)传递.当时我们根 ...

  10. openshift云计算平台diy模式安装Python2.7+Flask

    主要翻译了链接1)的教程,加上一些个人研究,步骤如下: 1) 在openshift.redhat.com申请账号,安装git for windows,然后安装gem install rhc,这些比较容 ...