Hdu 1505 City Game (DP求最大面積)

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1505    

思路: 跟1506 很相似,只是需要對每行都掃描,dp是O(n^2)的。

注意:用%s輸入或者cin 能夠濾掉多餘空格。

代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1000+5
int map[maxn][maxn];
int up[maxn],l[maxn],r[maxn];
int main(){
	int t ;
	int n,m;
	char tmp[2];
	scanf("%d",&t);
	while(t--){
		memset(up,0,sizeof(up));
		scanf("%d%d%*c",&n,&m);
		int ans = 0;
		for (int i = 1; i <= n; i++)
		{
			up[0] = up[m+1] = -1;
			for (int j = 1; j <= m; j++)
			{
				l[j] = r[j] = j;
				scanf("%s",tmp);
				if(tmp[0] == 'F'){
					up[j]++;
				}else up[j] = 0;
				while(up[ l[j]-1 ] >= up[j]){
					l[j] = l[ l[j]-1 ];
				}
			}
			for (int j = m; j > 0; j--)
			{
				while(up[ r[j]+1 ] >= up[j]){
					r[j] = r[ r[j]+1 ];
				}
				ans = max(ans,(r[j]-l[j]+1)*(up[j]));
			}
		}
		printf("%d\n",ans*3);
	}
	return 0;
}

發佈了119 篇原創文章 · 獲贊 10 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章