2020.02.03UPC联合训练赛第三场J题lamp(前缀和)

题目描述
There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it. The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right. In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings Si (1≤i≤H), each of length W. If the j-th character (1≤j≤W) of Si is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints
·1≤H≤2,000
·1≤W≤2,000
·Si is a string of length W consisting of # and ‘.’.
·’.’ occurs at least once in one of the strings Si (1≤i≤H).
输入
Input is given from Standard Input in the following format:

H W
S1
:
SH

输出
Print the maximum possible number of squares lighted by the lamp.
样例输入 Copy
【样例1】
4 6
#…#…
…#
…#.
#.#…
【样例2】
8 8
…#…#.
…#…
##…
…###…#
…#…#.
##…#.
#…#…
###.#…#
样例输出 Copy
【样例1】
8
【样例2】
13
提示
样例1解释:If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.
**

题目分析:

题目意思就是在矩阵中选一个点,这个点向上,下,左,右四个方向发出一束光线问能点亮多少个格子,遇到 “#”会停止,遇到边界会停止,问能点亮的最多的格子数目是多少,

要是两层for循环遍历每个点,然后每个点向上下左右四个方向遍历,求出每个点能点亮多个格子,然后求出最多的,这样的复杂度在O(10010004*1000)在1e9左右,必T,这样的做法只得70分。

正解是用前缀和,对于一个点a[i][j],能点亮的格子数等于它左边的点点亮的格子数+右边的点点亮的格子数+上面的点点亮的格子数+下面的点点亮的格子数

a[i][j]=left[i][j-1]+right[i][j+1]+up[i+1][j]+dowm[i-1][j]; 

用四个二维数组储存就好了。

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define  gold_m main
using namespace std;
typedef long long ll;
const int   N=1000006;
ll mod=1000000007;
void out(ll aa) {
    
    
	if(aa > 9) {
    
    
		out(aa/10);
	}
	putchar(aa%10 + '0');
}
inline int read() {
    
    
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
    
    
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
    
    
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
char a[2002][2002];
int  b[2002][3002];
int  cnt=1,n,m,u[2002][2002],d[2002][2002],r[2002][2002],l[2002][2002];
int gold_m() {
    
    
	n=read();
	m=read();
//0可以走  1不可以走
	for(int i=1 ; i<=n; i++)
		for(int j=1 ; j<=m; j++)
			cin>>a[i][j];
	for(int i=1 ; i<=n; i++)
		for(int j=1; j<=m; j++) {
    
    
			if(a[i][j]=='#')
				b[i][j]=1;
		}
	ll ans=-N*100;
// 该点到点亮左边的个数
	for(int i=1 ; i<=n; i++) {
    
    
		for(int j=1; j<=m; j++) {
    
    
			if(b[i][j]!=1)
				l[i][j]+=l[i][j-1]+1;
			else l[i][j]=0;
		}
	}
//该点点亮右边的个数
	for(int i=1; i<=n; i++) {
    
    
		for(int j=m; j>=1; j--) {
    
    
			if(b[i][j]!=1)
				r[i][j]+=r[i][j+1]+1;
			else r[i][j]=0;
		}
	}
//该点点亮上边的个数
	for(int i=1 ; i<=n; i++) {
    
    
		for(int j=1 ; j<=m; j++) {
    
    
			if(b[i][j]!=1)
				u[i][j]+=u[i-1][j]+1;
			else u[i][j]=0;
		}
	}
	//该点点亮下面的个数
	for(int i=n; i>=1; i--) {
    
    
		for(int j=1 ; j<=m; j++) {
    
    
			if(b[i][j]!=1) d[i][j]+=d[i+1][j]+1;
			else d[i][j]=0;
		}
	}
	for(int i=1; i<=n; i++) {
    
    
		for(int j=1 ; j<=m; j++) {
    
    
			if(b[i][j]!=1) {
    
    
				ll temp;
				temp=r[i][j]+l[i][j]+u[i][j]+d[i][j]-3;
				ans=max(ans,temp);
			}
		}
	}
	out(ans);
	return 0;
}

/**************************************************************
Problem: 14480
User: 2019LDU11
Language: C++
Result: 正确
Time:442 ms
Memory:92040 kb
****************************************************************/

猜你喜欢

转载自blog.csdn.net/wmy0536/article/details/104155001