【47.76%】【Round #380B】Spotlights
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Theater stage is a rectangular field of size n × m. The director gave you the stage’s plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.
You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight’s position is a cell it is placed to and a direction it shines.
A position is good if two conditions hold:
there is no actor in the cell the spotlight is placed to;
there is at least one actor in the direction the spotlight projects.
Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
Input
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan.
The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
Output
Print one integer — the number of good positions for placing the spotlight.
Examples
input
2 4
0 1 0 0
1 0 1 0
output
9
input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
output
20
Note
In the first example the following positions are good:
the (1, 1) cell and right direction;
the (1, 1) cell and down direction;
the (1, 3) cell and left direction;
the (1, 3) cell and down direction;
the (1, 4) cell and left direction;
the (2, 2) cell and left direction;
the (2, 2) cell and up direction;
the (2, 2) and right direction;
the (2, 4) cell and left direction.
Therefore, there are 9 good positions in this example.
【题目链接】:http://codeforces.com/contest/738/problem/B
【题解】
设f[i][j][4]表示从这个点往4个方向是不是有表演者(这个点是empty的情况下);
然后用记忆化搜索搞一波;
具体点;
从一个为empty的点开始往4个方向搜actor;如果搜到了actor,或者在途中遇到另外一个empty的点f[i][j][当前方向]为true;则也可以返回true;否则返回false;
统计答案就好;
直接用int即可不用LL;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
int n,m,ans = 0;
int f[MAXN][MAXN][4];
bool a[MAXN][MAXN];
int can(int x,int y,int p)
{
if (x > n || y>m)
return 0;
if (x < 1 || y<1)
return 0;
if (f[x][y][p]!=-1)
return f[x][y][p];
if (a[x][y])
return 1;
f[x][y][p] = can(x+dx[p],y+dy[p],p);
return f[x][y][p];
}
int main()
{
// freopen("F:\\rush.txt","r",stdin);
scanf("%d%d",&n,&m);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
scanf("%d",&a[i][j]);
memset(f,255,sizeof(f));
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (!a[i][j])
{
for (int k = 0;k <= 3;k++)
if (can(i,j,k))
ans++;
}
printf("%d\n",ans);
return 0;
}
【47.76%】【Round #380B】Spotlights的更多相关文章
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- POJ 1518 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ 1584 A Round Peg in a Ground Hole【计算几何=_=你值得一虐】
链接: http://poj.org/problem?id=1584 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- 【UOJ#76】【UR #6】懒癌(动态规划)
[UOJ#76][UR #6]懒癌(动态规划) 题面 UOJ 题解 神....神仙题. 先考虑如果是完全图怎么做... 因为是完全图,所以是对称的,所以我们只考虑一个有懒癌的人的心路历程. 如果只有一 ...
- 【codeforces】【比赛题解】#851 CF Round #432 (Div.2)
cf真的难…… 点我浏览丧题. [A]Arpa和她对墨西哥人浪的研究 Arpa正在对墨西哥人浪进行研究. 有n个人站成一排,从1到n编号,他们从时刻0开始墨西哥人浪. 在时刻1,第一个人站起来.在时刻 ...
- 【BestCoder】【Round#41】
枚举+组合数?+DP+数学问题 http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=582 QAQ许久没打过比赛,来一发BC,结果还是只 ...
- 【Mail.Ru Cup 2018 Round 2 A】 Metro
[链接] 我是链接,点我呀:) [题意] [题解] 1:一直往右走的情况. 2:中间某个地方中转 (不会出现超过1次的转弯. (如果超过了和1次是等价的 [代码] #include <bits/ ...
- 【Mail.Ru Cup 2018 Round 2 B】 Alice and Hairdresser
[链接] 我是链接,点我呀:) [题意] [题解] 因为只会增加. 所以. 一开始暴力算出来初始答案 每次改变一个点的话. 就只需要看看和他相邻的数字的值就好. 看看他们是不是大于l 分情况增加.减少 ...
- 【Mail.Ru Cup 2018 Round 2 C】 Lucky Days
[链接] 我是链接,点我呀:) [题意] [题解] 题解的作者: manish_joshi 对于任意一个k 因为那条直线(关于x,y的方程可以看出一条直线)的斜率>= 所以肯定会经过第一象限. ...
随机推荐
- Css盒模型有哪几部分,标准盒模型和IE盒模型有哪些区别
首先介绍Css的盒模型也就是标准盒模型(BOX Model) 包含了 内容(content) 内边距(padding) 边框(border) 外边框(margin) 这是大家经常用到的,也是现在的标准 ...
- 浏览器下管理Linux系统--记webmin的使用
本文介绍一款浏览器方式来管理linux的一种方式,这款软件就叫webmin,Webmin 让您能够在远程使用支持 HTTPS (SSL 上的 HTTP)协议的 Web 浏览器通过 Web 界面管理您的 ...
- 51Nod 飞行员配对(二分图最大匹配)(匈牙利算法模板题)
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
- 03007_JDBC概述
1.JDBC概述 (1)JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用J ...
- 为ImageView设置背景图片(代码中)
仅仅需三行代码: Resources resources = getBaseContext().getResources(); Drawable imageDrawable = resources.g ...
- 47.Android 自己定义PopupWindow技巧
47.Android 自己定义PopupWindow技巧 Android 自己定义PopupWindow技巧 前言 PopupWindow的宽高 PopupWindow定位在下左位置 PopupWin ...
- Android 上的 制表符(tab) —— 一个奇妙的字符 (二)
接到上回的说,主要是上回那个问题,我认为是android的bug,黎叔认为是cocos2dx的bug,叫我去提交bug.所以我又继续研究了下. 上回说到会调用java层的函数去创建一个image,然后 ...
- php课程 12-41 多态是什么
php课程 12-41 多态是什么 一.总结 一句话总结:一种请求,多种响应(根据参数类型和数量) 1.function useUsb(Usb $usb){}这句话是什么意思? 判断$usb参数是否实 ...
- js-轮播图的总结
/*两种播放行为:(一种自动播放,一种控制播放),一个定时器控制. *一个定时器控制两种播放状态. * 布局说明:装图片的盒子足够宽,让图片左浮,排成一排,最后一张重新放置第一张. * 定时器里执行自 ...
- 阅读笔记——Web应用程序
Web应用程序与DD文件 Web应用程序 web应用程序是一种可以通过Web访问的应用程序.Web应用程序最大的好处是永和很容易访问应用程序.用户只需要有浏览器即可,不需要安装其他任何软件.一个Web ...