http://poj.org/problem?id=1964
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.
题目的意思就是找出一个最大的矩形, 这个矩形由F组成, 最后的答案就是max*3.
思路: 逐行的加入, 每次加入求能构成的最大矩形....
用数组height[i]表示: 当前行如果加入这第i列所能构成的矩形的最大高度.
数组left[i]表示: 向左延伸, 高度小于当前列的第一个列坐标
数组right[i]表示: 向右延伸, 高度小于当前列的第一个列坐标.
初始化:
left[0] = left[m+1] = -1;
right[0] = right[m+1] = -1; //其中m表示列数
这题要特别注意输入数据格式, 两个字符间可能有多个空格.....-_-||
Code from http://blog.csdn.net/blzorro/article/details/9972533
Read full article from poj 1964 City Game - SMCwwh - 博客频道 - CSDN.NET
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.
题目的意思就是找出一个最大的矩形, 这个矩形由F组成, 最后的答案就是max*3.
思路: 逐行的加入, 每次加入求能构成的最大矩形....
用数组height[i]表示: 当前行如果加入这第i列所能构成的矩形的最大高度.
数组left[i]表示: 向左延伸, 高度小于当前列的第一个列坐标
数组right[i]表示: 向右延伸, 高度小于当前列的第一个列坐标.
初始化:
left[0] = left[m+1] = -1;
right[0] = right[m+1] = -1; //其中m表示列数
这题要特别注意输入数据格式, 两个字符间可能有多个空格.....-_-||
Code from http://blog.csdn.net/blzorro/article/details/9972533
- int T,n,m,righ[MAXN][MAXN],up[MAXN][MAXN],lef[MAXN][MAXN];
- char map[MAXN][MAXN];
- int main()
- {
- scanf("%d",&T);
- while(T--)
- {
- int ans=0;
- scanf("%d%d",&m,&n); getchar();
- for(int i=0;i<m;++i)
- for(int j=0;j<n;++j)
- {
- char ch=getchar();
- while(ch!='F' && ch!='R') ch=getchar();//处理读入,注意用while,直到读入为F或者R为止
- map[i][j]=ch;
- }
- for(int j=0;j<n;++j)
- for(int i=0;i<m;++i)
- if(map[i][j]=='R') up[i][j]=0;
- else up[i][j]=i? up[i-1][j]+1:1;//递推up(i,j)
- for(int i=0;i<m;++i)
- for(int j=0,lo=-1;j<n;++j)
- if(map[i][j]=='R') lo=j,lef[i][j]=0;
- else lef[i][j]=i? max(lo+1,lef[i-1][j]):lo+1;//递推left(i,j)
- for(int i=0;i<m;++i)
- for(int j=n-1,ro=n;j>=0;--j)
- if(map[i][j]=='R') ro=j,righ[i][j]=n;
- else righ[i][j]=i? min(ro-1,righ[i-1][j]):ro-1;//递推right(i,j)
- for(int i=0;i<m;++i)
- for(int j=0;j<n;++j)
- if(map[i][j]=='F') ans=max(ans,(righ[i][j]-lef[i][j]+1)*up[i][j]);//计算答案取最大值
- printf("%d\n",ans*3);
- }
- return 0;
- }
- int height[MAXN]; //列高
- int left[MAXN], right[MAXN];
- int n, m;
- int dp() {
- int i, j;
- //计算左边节点
- left[0] = left[m+1] = -1;
- for(i=1; i<=m; i++) {
- if(height[i] == 0) {
- continue;
- }
- for(j=i-1; ; j=left[j]) {
- if(height[j] < height[i]) {
- left[i] = j;
- break;
- }
- }
- }
- //计算右边节点
- right[0] = right[m+1] = -1;
- for(i=m; i>=1; i--) {
- if(height[i] == 0) {
- continue;
- }
- for(j=i+1; ; j=right[j]) {
- if(height[j] < height[i]) {
- right[i] = j;
- break;
- }
- }
- }
- //计算当前能得到的最大矩形面积
- int cnt = 0;
- int max = 0;
- for(i=1; i<=m; i++) {
- if(height[i] != 0) {
- cnt = (right[i] - left[i] - 1) * height[i];
- if(cnt > max) {
- max = cnt;
- }
- }
- }
- return max;
- }
- int main() {
- //freopen("in.txt", "r", stdin);
- int t;
- char c[10];
- scanf("%d", &t);
- while(t--) {
- memset(height, 0, sizeof(height));
- memset(left, 0, sizeof(left));
- memset(right, 0, sizeof(right));
- scanf("%d%d", &n, &m);
- int i, j;
- int max = 0, tmp;
- for(i=1; i<=n; i++) {
- for(j=1; j<=m; j++) {
- //scanf("%*c%c", &c);
- scanf("%s", c);
- if(c[0] == 'F') {
- height[j]++;
- } else {
- height[j] = 0;
- }
- }
- tmp = dp();
- if(max < tmp) {
- max = tmp;
- }
- }
- printf("%d/n", max * 3);
- }
- return 0;
- }
Read full article from poj 1964 City Game - SMCwwh - 博客频道 - CSDN.NET