la 3029 City Game (扫描法)

题目链接

思路:对每一个格子进行处理:计算包含它的最大矩形。

up[i][j]:   存储矩形的高;

left[i][j]:  存储矩形的左边界的列号;

right[i][j]:存储矩形的右边界的列号。

面积 = up[i][j] * ( right[i][j] - left[i][j] +1 )。

心得:即便是有了思路,写代码前也要在纸上画画,模拟模拟。否则就像代码中提示注意的地方,写成了0,估计debug半天也不知道错在哪了。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
typedef long long ll;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e3+10;

int mat[maxn][maxn],up[maxn][maxn];
int myleft[maxn][maxn],myright[maxn][maxn];
int m,n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d %d",&m,&n);
		char ch;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				ch=getchar();
				while(ch!=‘R‘ && ch!=‘F‘)ch=getchar();
				if(ch==‘R‘)mat[i][j]=1;
				else mat[i][j]=0;
			}
		}
		/*
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				printf("%d ",mat[i][j] );
			}
			printf("\n");
		}*/
		int ans=0,i,j;
		for(i=0;i<m;i++){//从上到下扫描
			int lo=-1,ro=n;//lo表示左边障碍物的列号,ro同理。
			for(j=0;j<n;j++){//从左到右扫描,处理up[][]和left[][]。
				if(mat[i][j]==1){
					lo=j;
					up[i][j]=0;
					myleft [i][j]=0;

				}else{
					up[i][j]=(i==0?1:up[i-1][j]+1);
					myleft[i][j]=(i==0?lo+1:max(myleft[i-1][j],lo+1));// !!!
				}
			}

			for(int j=n-1;j>=0;j--){//从右往左扫描,处理right[][]。
				if(mat[i][j]==1){
					ro=j;
					myright[i][j]=n;//注意细节
				}else{
					myright[i][j]=(i==0?ro-1:min(myright[i-1][j],ro-1));// !!!
					ans=max(ans,up[i][j]*(myright[i][j]-myleft[i][j]+1));
				}
			}
		}
		printf("%d\n",ans*3 );
	}
	return 0;
}

  

时间: 2024-10-11 00:26:25

la 3029 City Game (扫描法)的相关文章

LA 3029 - City Game (简单扫描线)

题目链接 题意:给一个m*n的矩阵, 其中一些格子是空地(F), 其他是障碍(R).找一个全部由F 组成的面积最大的子矩阵, 输出其面积乘以3的结果. 思路:如果用枚举的方法,时间复杂度是O(m^2 n^2); 因为不但要枚举每一个点,而且矩阵的大小不知道,所以还要枚举长和宽. 可以通过枚举每一个点,求该点所能构成的最大矩形的边界. 分别用le[], rig[] 和 up[] 表示左边界,右边界和 上边界. 1 #include <iostream> 2 #include <cstrin

UVALive 3029 City Game 悬线法求最大子矩阵面积 dp

题目链接:点击打开链接 Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees, factories and buildings. There is still some space in the a

F - City Game

F - City Game Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Bob is a strategy game programming specialist. In his new city building game the gaming environmentis as follows: a city is built up by areas, in which there are str

UVA之1330 - City Game

[题目] Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees, factories and buildings. There is still some space in the area tha

LA 2038

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to

LA 4327

Panagola, The Lord of city F likes to parade very much. He always inspects his city in his car and enjoys the welcome of his citizens. City F has a regular road system. It looks like a matrix with n + 1 <tex2html_verbatim_mark>west-east roads and m 

bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)

P2061 [USACO07OPEN]城市的地平线City Horizon 扫描线 扫描线简化版 流程(本题为例): 把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序 $upd:$本题的底端点坐标简化为$(x,0)$ 蓝后对纵坐标建一棵线段树(本题需要对高度进行离散化). 每次对线段树进行覆盖$or$删除区间操作,顺便统计一下$k=$有多少点被覆盖到 而两次(线段)操作之间的长度为$r=x_{i}-x_{i-1}$ 于是两条线段之间被覆盖的面积即为$k*r$ (

City Upgrades

City Upgrades Time limit: 1000 msMemory limit: 128 MB There are N cities placed in a line. For each city ii you know its coodinate x??. You can upgrade exactly K of these cities. Your goal is to choose what cities to upgrade in a way the minimizes th

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space