【Jason's_ACM_解题报告】City Game

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 that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems  he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.

Each area has its width and length. The area is divided into a grid of equal square units. The rent paid for each unit on which you're building stands is 3$. Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N. The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.


Input
The first line of the input file contains an integer K  determining the number of datasets. Next lines contain
the area descriptions. One description is defined in the following way: The first line contains two integers-area
length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that
mark the reserved or free grid units, separated by a blank space. The symbols used are:
R  reserved unit
F  free unit
In the end of each area description there is a separating line.


Output
For each data set in the input file print on a separate line, on the standard output, the integer that represents the
profit obtained by erecting the largest building in the area encoded by the data set.


Sample Input
2
5 6
R F F F F F
F F F F F F
R R R F F F
F F F F F F
F F F F F F
5 5
R R R R R
R R R R R
R R R R R
R R R R R
R R R R R


Sample Output
45
0


仍然是扫描法,对于这个题,我们假设对于每一个元素来说,它是合法的最大矩形的底边的一点,那么对于每个元素维护三个数组,up、left、right。

直接理解三个数组的正确性含义不太好想,我们割裂开看:

1.首先是up数组,假设我们已经知道了底边的边长,不管底边具体是什么情况,那么,矩形的高越高越好。所以,若该元素为障碍,则up[i][j]=0,就是没有以此元素为底边的一点的矩形;若元素不为障碍,up[i][j]=up[i-1][j]+1,就是说新矩形的高是上面那个矩形的高+下面这个元素所在的小长条的高。

2.其次是left数组,同样的,不考虑该元素的所在底边的矩形的高,当然是底边向左越长越好,但是又要保证整个左边界往右不包括任何障碍,所以应该是,该元素上面矩形的左边界和下面元素所在小长条的左边界的最靠右的一个,即为left[i][j]=max(left[i-1][j],lo+1);lo是该元素所在小长条的最靠右的障碍的位置,同时,若该元素上有障碍,应给left[i][j]赋值为-1,即保证最靠右的障碍的位置的状态转移。

3.最后right数组和left相似,无非就是状态转移从右向左。

最后答案就是ans=MAX( ans , up[i][j] * ( right[i][j] - left[i][j] + 1) *3);


附代码如下:

#include<cstdio> 
#include<cstring>
#include<algorithm>

using namespace std;

#define MAXN (1000+5)
#define clr(x) (memset(x,0,sizeof(x))) 

int grid[MAXN][MAXN];
int up[MAXN][MAXN],left[MAXN][MAXN],right[MAXN][MAXN];

bool readbool(){
	char ch;
	for(;;){
		ch=getchar();
		if(ch=='F')return true;
		if(ch=='R')return false;
	}
}

int main(){
//freopen("in.txt","r",stdin);
	int total;
	scanf("%d",&total);
	while(total--){
		int m,n;clr(up);clr(left);clr(right);
		scanf("%d%d",&m,&n);
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				grid[i][j]=readbool();
			}
		}
		
		for(int i=0;i<m;i++){
			int lo=-1,ro=n;
			for(int j=0;j<n;j++){
				if(grid[i][j]){
					if(i>0)up[i][j]=up[i-1][j]+1;
						else up[i][j]=1;
				};
				
				if(grid[i][j]){
					if(i>0)left[i][j]=max(left[i-1][j],lo+1);
						else left[i][j]=lo+1;
				}else lo=j;
			}
			
			for(int j=n-1;j>=0;j--){
				if(grid[i][j]){
					if(i>0)right[i][j]=min(right[i-1][j],ro-1);
						else right[i][j]=ro-1;
				}else {
					ro=j;
					right[i][j]=n;//此处必不可少,由于 min(right[i-1][j],ro-1)
					// 所以right需要在有障碍的时候足够大,以保证状态的转移 
				}
			}
		}/*
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				printf("%d ",right[i][j]);
			}
			printf("\n");
		}
		printf("\n");
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				printf("%d ",left[i][j]);
			}
			printf("\n");
		}*/
		int ans=0;
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				ans=max(ans,up[i][j]*(right[i][j]-left[i][j]+1));
			}
		}
		
		printf("%d\n",ans*3);
	}
//fclose(stdin);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值