UVA 1330 LA 3029 seerc 2004 - City Game

来源:互联网 发布:网络新词及意思 编辑:程序博客网 时间:2024/06/07 02:21


Bob is a strategy game programming specialist. In his new city building game thegaming 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 strategictask of his game is to win as much rent money from these free spaces. To win rent money youmust erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying tofind a way to build the biggest possible building in each area. But he comes across someproblems � he is not allowed to destroy already existing buildings, trees, factories and streets inthe 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. Eachone 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 markedwith the symbol F.

Input 

The first line of the input file contains an integer K � determining the number ofdatasets. Next lines contain the area descriptions. One description is defined in the followingway: The first line contains two integers-area length M<=1000 and width N<=1000, separated bya 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, theinteger that represents the profit obtained by erecting the largest building in the area encoded bythe data set.

Sample Input 

25 6R F F F F FF F F F F FR R R F F FF F F F F FF F F F F F5 5R R R R RR R R R RR R R R RR R R R RR R R R R

Sample Output 

450

题目大意:给定N*M的矩阵,其中一些格子是空地(F),其他是障碍(R)。求全部又空地组成的最大矩阵,结果×3.



用三个数组记录递推一个格子往上,往左,往右的障碍格位置。全部扫描一遍即可。


#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <string>#include <cstring>#include <map>#include <cmath>#include <string>#include <queue>#include <set>using namespace std;#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))const int INF = 0x3f3f3f3f;const int maxn = 1000 + 50;int gleft[maxn][maxn];int gright[maxn][maxn];int up[maxn][maxn];int G[maxn][maxn];int main() {int kase;scanf("%d", &kase);while(kase--) {int n, m;scanf("%d%d", &n, &m);for(int i=0; i<n; i++) {for(int j=0; j<m; j++) {char ch = getchar();while(ch!='F' && ch!='R') ch = getchar();G[i][j] = ch == 'F' ? 0 : 1;}}int ans = 0;int lo=-1;for(int i=0; i<m; i++) {up[0][i] = -1 + G[0][i];if(G[0][i]) lo = i;gleft[0][i] = lo;}int ro=m;for(int i=m-1; i>=0; i--) {if(G[0][i]) ro = i;gright[0][i] = ro;int w = gright[0][i] - gleft[0][i] - 1;int h = 0 - up[0][i];if(w > 0 && h > 0) {int t = w * h;if(t > ans) {ans = t;}}}for(int i=1; i<n; i++) {lo = -1;for(int j=0; j<m; j++) {up[i][j] = G[i][j] ? i : up[i-1][j];if(G[i][j]) lo = j;if(G[i-1][j]) {gleft[i][j] = lo;} else {gleft[i][j] = max(lo, gleft[i-1][j]);}}ro = m;for(int j=m-1; j>=0; j--) {if(G[i][j]) ro = j;if(G[i-1][j]) {gright[i][j] = ro;} else {gright[i][j] = min(ro, gright[i-1][j]);}int w = gright[i][j] - gleft[i][j] - 1;int h = i - up[i][j];if(w>0 && h>0) {int t = w * h;if(t > ans) {ans = t;}}}}printf("%d\n", ans*3);}return 0;}







0 0
原创粉丝点击