poj1964 City Game(最大子矩阵)

By | 08月11日
Advertisement

City Game

Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 4783 Accepted: 1899

Description

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 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 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

这个题是lrj大白上的一个例题。

思路:

poj1964  City Game(最大子矩阵)

代码:

#include<stdio.h>
#include<string.h>
#define max(a, b) (a>b?a:b)
#define min(a, b) (a<b?a:b)
int map[1005][1005], up[1005][1005], left[1005][1005], right[1005][1005];//要开全局变量,在main里会超

int main()
{
    int T, n, m, i, j, lo, ro, ans;
    scanf("%d", &T);
    while(T--)
    {
        ans = 0;//注意初始化
        memset(map, 0, sizeof(map));
        memset(up, 0, sizeof(up));
        memset(left, 0, sizeof(left));
        memset(right, 0, sizeof(right));
        scanf("%d%d", &n, &m);//n行,m列
        for(i=0; i<n; i++)
           for(j=0; j<m; j++)
           {
               char ch = getchar();
               while(ch!='F' && ch!='R') ch = getchar();//两个字母之间可能有多个空格
               map[i][j] = ch == 'F'? 0:1;//用0表示F, 1表示R
           }

        for(i=0; i<n; i++)
        {
            lo = -1; ro = m;//每扫一行,初始化一下。
            for(j=0; j<m; j++)
            {
                if(map[i][j]==1) {up[i][j]=left[i][j]=0; lo=j;}
                else
                {
                    up[i][j] = i==0 ? 1 : up[i-1][j]+1;
                    left[i][j] = i==0 ? lo+1 : max(left[i-1][j], lo+1);
                }
            }
            for(j=m-1; j>=0; j--)
            {
                if(map[i][j]==1) {right[i][j]=m; ro=j;}
                else
                {
                    right[i][j] = i==0 ? ro-1 : min(right[i-1][j], ro-1);
                    ans = max(ans, up[i][j]*(right[i][j]-left[i][j]+1));//当某一行的up和left扫完后,每扫一个right点
                                                                         //都要计算一遍面积,和ans比较,取最大值。
                }
            }
        }
        printf("%d\n", ans*3);
    }
    return 0;
}

Similar Posts:

  • 【BZOJ3039】玉蟾宫/【HDU1505】【POJ1964】City Game 最大01子矩阵

    转载请注明出处:http://blog.csdn.net/vmurder/article/details/42884845 首先我们可以用单调栈每行扫一遍,维护一个点向上下左右各能延伸多长. 当然,这是可以做的,也是很恶心的. 我们可以每行扫一遍,维护每个点当前的[列高度]时能向左向右的最大延伸距离. 当然,某个点可能降低一点高度,就会更宽,结果更优,但是显然既然存在这种结果,那么其它列一定可以代劳. 呃.所以每行扫一遍,然后记录这行每个节点以最大高度能向左向右延伸的最大距离就好了. 这道题思想

  • hdu 1505 City Game(0和1 中的最大子矩阵)

    City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3834 Accepted Submission(s): 1588 Problem Description Bob is a strategy game programming specialist. In his new city building game the gam

  • ACM Regionals 2004 Europe - Southeastern 最大子矩阵(扫描法)

    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

  • Task 4.4二维环形数组求最大子矩阵之和

    任务: (1)输入一个二维整形数组,数组里有正数也有负数. (2)二维数组首尾相接,象个一条首尾相接带子一样. (3)数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. (4)求所有子数组的和的最大值. 1.设计思想:这个题目显然是结合了之前二维数组求最大子数组的和以及一维环形数组求最大连续子数组的和两个模块,所以只要将两个程序联合起来即可.首先将数组横向扩展一倍,将环转换为求一维连续的数组求最大子数组和.然后将二维数组转换为一维数组求和,即建立一个新的和数组,用来储存从数组的首个

  • 并查集 - UVALive 6889 City Park

    City Park Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=129725 Mean: 在网格中给你一些矩形,求最大连通块的面积. analyse: 由于题目保证了矩形不会相交,所以不用扫描线也可做. 先把所有的线段分为横向和纵向,然后排序,依次判断是否相邻,相邻就用并查集合并,最后再用并查集统计一下面积,取最大值即可. Time complexity: O(N) Source code

  • 面积最大的全1子矩阵

    题目来源:腾讯2012年暑期实习生招聘面试二面试题 题目描述: 在一个M * N的矩阵中,所有的元素只有0和1,从这个矩阵中找出一个面积最大的全1子矩阵,所谓最大是指元素1的个数最多. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行是两个整数m.n(1<=m.n<=1000):代表将要输入的矩阵的大小. 矩阵共有m行,每行有n个整数,分别是0或1,相邻两数之间严格用一个空格隔开. 输出: 对应每个测试案例,输出矩阵中面积最大的全1子矩阵的元素个数. 样例输入: 2 2 0

  • hdu 4849 Wow! Such City! (最短路spfa)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4849 Wow! Such City! Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 1486 Accepted Submission(s): 511 Problem Description Doge, tired of being a p

  • hihocoder #1094 : Lost in the City微软苏州校招笔试 12月27日 (建图不大【暴力枚举】 子图的4种形态 1Y )

    #1094 : Lost in the City 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is. He does not know which direction is north. Fortunately, Little Hi has a map of the city. The map can be considered as a gri

  • 返回一个二维循环数组中最大子矩阵的和

    设计思路 和一维循环数组思路相仿,循环N次,每次将第一列的数移到最后一列,新建一个相同规格的二维数组来存放新矩阵,将之前写的求二维数组最大子矩阵的和的算法稍作修改,添加一个循环即可满足要求. 源程序代码 #include<iostream> #include <cstdlib> #include <ctime> using namespace std; #define M 4 #define N 4 void main() { int a[M][N],aa[M][N],

  • HDU 3080 The plan of city rebuild(prim和kruskal)

    The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 616 Accepted Submission(s): 215 Problem Description News comes!~City W will be rebuilt with the expectation to become a cen

Tags: