P4147 玉蟾宫

题目

P4147 玉蟾宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 

方法 

 悬线法

1.确定每行每个元素能够取到的左右边界

2.确定每行每个元素能够取到的上边界

代码

//悬线法
#include
#include
using namespace std;
const int N = 1010;
int n, m, i, j;
char a[N][N];
int l[N][N], r[N][N], h[N][N];
int main()
{
	cin >> n >> m;
	for (i = 1; i <= n; i++) {
		for (j = 1; j <= m; j++) {
			cin >> a[i][j];
			if (a[i][j] == 'F') {//初始化左右,上边界
				h[i][j] = 1;
				l[i][j] = r[i][j] = j;
			}
			
		}
		
	}
	for (i = 1; i <= n; i++) {
		for (j = 2; j <= m; j++) {
			if (a[i][j] == 'F' && a[i][j - 1] == 'F')
				l[i][j] = l[i][j - 1];//左边界
		}
		for (j = m - 1; j >= 1; j--) {
			if (a[i][j] == 'F' && a[i][j + 1] == 'F')
				r[i][j] = r[i][j + 1];//右边界
		}
	}
	int  s = 0;
	for (i = 1; i <= n; i++) {
		for (j = 1; j <= m; j++) {
			if (a[i][j] == 'F' && a[i - 1][j] == 'F'){
				h[i][j] = h[i - 1][j] + 1;//确定上边界
				l[i][j] = max(l[i][j], l[i - 1][j]);//确定每个元素能到达的左边界
				r[i][j] = min(r[i][j], r[i - 1][j]);//确定每个元素能到达的右边界
			}
			if(a[i][j]=='F')s = max(s, h[i][j] * (r[i][j] - l[i][j] + 1));//确定最大面积s
		}
	}
	cout << s * 3;
	return 0;
}

 

 

你可能感兴趣的:(c++,算法)