首页 > 其他分享 >Security Camera 3

Security Camera 3

时间:2022-11-19 22:48:48浏览次数:49  
标签:square int obstacle surveillance Camera 监控 Security left

Problem Statement

There is a grid with $H$ rows from top to bottom and $W$ columns from left to right. Let $(i, j)$ denote the square at the $i$-th row from the top and $j$-th column from the left.
Square $(i, j)$ is occupied by an obstacle if $S_{i,j}=$ #, and is empty if $S_{i,j}=$ ..

Takahashi will install some surveillance cameras in the grid.

A surveillance camera can be placed at a square without an obstacle, in one of the four directions: up, down, left, or right.
Multiple surveillance cameras may be placed at the same square.

Each surveillance camera monitors the square it is placed at, and the squares in the direction it is placed in, as far as there is no obstacle in between.

At least how many surveillance cameras are needed to monitor all squares without an obstacle?

Constraints

  • $1 \leq H,W \leq 300$
  • $S_{i,j}$ is . or #.

Input

The input is given from Standard Input in the following format:

$H$ $W$
$S_{1,1}\ldots S_{1,W}$
$\vdots$
$S_{H,1}\ldots S_{H,W}$

Output

Print the answer.


Sample Input 1

3 3
...
.#.
...

Sample Output 1

4

For example, if we install two surveillance cameras at square $(1, 1)$ facing right and down, and another two at square $(3, 3)$ facing up and left, all squares without an obstacle will be monitored.


Sample Input 2

3 5
...##
.#...
...#.

Sample Output 2

5

For example, if we install two surveillance cameras at square $(1, 1)$ facing right and down, another at square $(3, 3)$ facing left, and another two at square $(2, 5)$ facing left and down, all squares without an obstacle will be monitored.

Note that the camera at square $(2, 5)$ facing left cannot monitor square $(2, 1)$, since there is an obstacle in between at square $(2, 2)$.


Sample Input 3

14 107
...........................................................................................................
...........................................................................................................
..#########..###....###########..###.......###...###########..####.......###...###########...###########...
..########..###....###########....###.....###...###########...#####......###..###########...###########....
..#######..###.....###.............###...###....###...........######.....###..###...........###............
..######..###......###..............###.###.....###...........###.###....###..###...........###............
..#####..###.......############......#####......############..###..###...###..###...........############...
..####....###......############.......###.......############..###...###..###..###...........############...
..###......###.....###................###.......###...........###....###.###..###...........###............
..##........###....###................###.......###...........###.....######..###...........###............
..#..........###...############.......###.......############..###......#####..############..############...
..............###...###########.......###........###########..###.......####...###########...###########...
...........................................................................................................
...........................................................................................................

Sample Output 3

91

最优化问题,在尝试了贪心,dp后,走向网络流。

首先一个监控有4个方向,但是只有两个方向有用。因为你在一个地方放置向右的监控,效果一定不差于在这个地方往右遇到的第一个障碍的前面或边界放置一个往左的监控。之后不妨设只有向下和向右的两种方向的监控。

另一个结论是,一个点会放向下的监控,当且仅当他位于上边界或者他上面是障碍,不然他的效果1肯定不如再往上一格去放。向右的监控同理。

最大流很难建图,考虑去求最小割。不妨把一个点拆成两个点,一个代表他是否放置往右的监控,一个代表他是否放置往下的监控。源点连向每一个代表是否放置往右监控的店,每一个代表是否放置往下的监控的点连向汇点,流量均为1。对于一个点,处理出他往上最近的一个可以放置的店,他往左最近的一个可以放置的店。这两个点至少要选一个,所以给这两个点分别代表的点中间连一条流量为正无穷的边。那么此时跑最大流,一条边断了,表示他放置往下/往右的监控。那么此时每个点代表的每条边,他的左右都至少1有一个点选了,也就代表这个点被覆盖了。

发现图是二分图,复杂度 \(O((HW)^{1.5})\)

#include<bits/stdc++.h>
using namespace std;
const int N=305,M=3*N*N+6;
int h,w,e_num(1),hd[M],s,t,l,r,q[M],v[M],k,vhd[M],x[N][N],y[N][N],ans;
struct edge{
	int v,nxt,f;
}e[N*N*10];
int get(int x,int y)
{
	return x*h+w;
}
void add_edge(int u,int v,int f)
{
//	printf("%d %d %d\n",u,v,f);
	e[++e_num]=(edge){v,hd[u],f};
	hd[u]=e_num;
}
char str[N][N];
int bfs()
{
	memset(v,0,sizeof(v));
	q[l=r=1]=s,v[s]=1;
	memcpy(hd,vhd,sizeof(hd));
	while(l<=r)
	{
		for(int i=hd[q[l]];i;i=e[i].nxt)
		{
			if(!v[e[i].v]&&e[i].f)
			{
//				printf("%d %d %d\n",q[l],e[i].v,e[i].f);
				v[e[i].v]=v[q[l]]+1;
				q[++r]=e[i].v;
			}
		}
		++l;
	}
//	for(int i=1;i<=t;i++)
//		printf("%d ",v[i]);
//	putchar('\n');
	return v[t];
}
int dfs(int x,int flow)
{
	if(x==t)
		return flow;
	int s=0;
	for(int&i=hd[x];i;i=e[i].nxt)
	{
		if(v[e[i].v]==v[x]+1&&e[i].f)
		{
			int t=dfs(e[i].v,min(flow,e[i].f));
			flow-=t,s+=t;
			e[i].f-=t,e[i^1].f+=t;
			if(!t)
				v[e[i].v]=0;
			if(!flow)
				break;
		}
	}
	return s;
}
int main()
{
	scanf("%d%d",&h,&w) ;
	s=2*h*w,t=2*h*w+1;
	for(int i=0;i<h;i++)
		scanf("%s",str[i]);
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<w;j++)
		{
			if(str[i][j]=='#')
				continue;
			k=i*w+j;
			if(!i||str[i-1][j]=='#')
			{
				x[i][j]=k;
				add_edge(s,k,1);
				add_edge(k,s,0) ;
			}
			else
				x[i][j]=x[i-1][j];
			if(!j||str[i][j-1]=='#')
			{
				y[i][j]=k;
				add_edge(k+h*w,t,1);
				add_edge(t,k+h*w,0);
			}
			else
				y[i][j]=y[i][j-1];
			add_edge(x[i][j],y[i][j]+h*w,1e9);
			add_edge(y[i][j]+h*w,x[i][j],0);
		}
	}
	memcpy(vhd,hd,sizeof(hd));
	while(bfs())
		while(k=dfs(s,2e9))
			ans+=k;
	printf("%d",ans);
}

标签:square,int,obstacle,surveillance,Camera,监控,Security,left
From: https://www.cnblogs.com/mekoszc/p/16907390.html

相关文章

  • 微服务篇之SpringSecurity。
    1.SpringSecurity目前主流的认证授权的框架流行程度 TRANSLATEwithxEnglishArabicHebrewPolishBulgarianHindiPortugueseCatalanHmongDaw......
  • Springboot 配置 Security流程
    装入依赖引入spring-sercurity<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependen......
  • spring-security 跨域配置
    跨域问题是实际应用开发中一个非常常见的需求,在Spring框架中对于跨域问题的处理方案有好几种,引入了SpringSecurity之后,跨域问题的处理方案又增加了。1.什么是CORSCORS(C......
  • 点云_pointcloud_to_camera_可视化
    点云和图像像高=EFL*tan(半FOV);EFL为焦距;FOV为视场角视场角:FieldofView以光学仪器的镜头为顶点,以被测目标的物像可通过镜头的最大范围的两条边缘构成的夹角,称为......
  • LayaBox引擎3D源码阅读(二、关于Camera渲染的研究)
    前言摄像机在3D引擎充当着眼睛的作用,能看到什么不能看到什么,都是Camera中的属性所决定。因此我打算首先研究Camera中的一部分代码,以此来研究Laya在3D渲染方面的知识。本......
  • Week9——Security: Web Security
    Week9——Security:WebSecurity1.Whichofthefollowingisfalseaboutthetwokeysusedinpublickeyencryption?Ifyouhavethepublickeyitiseasytoc......
  • Week8:Security : Encrypting and Signing
    Week8:Security:EncryptingandSigning1.Whichofthefollowingistrueofsecurity?Perfectsecurityisachievableandrequiresatrade-offwithcost2.What......
  • 2010 Principles on the Security of AES against First and Second-Order Differenti
    一、对于AES算法的DPA攻击准则(无防护措施下的AES实现)(1)在第3轮列混淆前任意中间值可以用于一阶DPA攻击,该攻击将明文的0,3或15比特固定(2)在第7轮轮......
  • 2010 Principles on the Security of AES against First and Second-Order Differenti
    一、对于AES算法的DPA攻击准则(无防护措施下的AES实现)(1)在第3轮列混淆前任意中间值可以用于一阶DPA攻击,该攻击将明文的0,3或15比特固定(2)在第7轮轮......
  • Pod Security Admission
    Kubernetes Pod安全性标准(SecurityStandards) 为Pod定义不同的隔离级别。这些标准能够让你以一种清晰、一致的方式定义如何限制Pod行为。作为一项Beta功能特性,Ku......