Hdu1505 -- City game (scanning line + pitfall)

Source: Internet
Author: User
City game

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 4572 accepted submission (s): 1938


Problem descriptionbob 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 into SS 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.
 


 

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


 

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

The first question of the scanning line. Question: Given the matrix of M * n, r cannot be occupied, and F can be occupied. It costs 3 for every f occupied, the cost of occupying F to form the largest rectangle
Scan Line, from top to bottom scan, for each number storage, to this point of the maximum height of the rectangle, the left side of the rectangle, the right side, you can use this to calculate the size of the rectangle, if the point is R, the height is 0. Otherwise, the height + 1 is determined by the left side of the vertex and the left side of the upper layer. That is, P [I] [J]. L = max (P [I] [J]. l, P [I-1] [J]. l), which must be closest to this point.

After this operation, each vertex stores the largest rectangle at the end of the given vertex to find the maximum value of all vertices.

In the input, both F and R must be judged; otherwise, wa

The code at hangdian is A, but other OJ won't work.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{    int l , r , k , h ;} p[1100][1100] ;void init(int n,int m){    int i , j , l , r ;    char ch[10] ;    for(i = 1 ; i <= n ; i++)    {        for(j = 1 ; j <= m ; j++)        {            scanf("%s", ch);            if( ch[0] == 'R' )                p[i][j].k = 0 ;            else if( ch[0] == 'F' )                p[i][j].k = 1 ;        }        for(j = 1 , l = 1 ; j <= m ; j++)        {            if( p[i][j].k == 0 )            {                p[i][j].l = -1 ;                l = j+1 ;            }            else                p[i][j].l = l ;        }        for(j = m , r = m ; j >= 1 ; j--)        {            if( p[i][j].k == 0 )            {                p[i][j].r = 999999 ;                r = j - 1 ;            }            else                p[i][j].r = r ;        }    }}int main(){    int t , i , j , n , m , l , r , temp , ans ;    char ch ;    scanf("%d", &t);    while(t--)    {        ans = -1 ;        scanf("%d %d", &n, &m);        init(n,m);        for(i = 1 ; i <= m ; i++)        {            if( p[1][i].k == 1 )                p[1][i].h = 1 ;            else                 p[1][i].h = 0 ;            temp = p[1][i].h * ( p[1][i].r - p[1][i].l + 1);            if( temp > ans )                ans = temp ;        }        for(i = 2 ; i <= n ; i++)        {            for(j = 1 ; j <= n ; j++)            {                if( p[i][j].k == 0 )                {                    p[i][j].h = 0 ;                }                else                {                    p[i][j].h = p[i-1][j].h + 1 ;                    p[i][j].l = max( p[i][j].l,p[i-1][j].l );                    p[i][j].r = min( p[i][j].r,p[i-1][j].r );                }                temp = p[i][j].h * ( p[i][j].r - p[i][j].l + 1 );                if(temp > ans)                    ans = temp ;            }        }        printf("%d\n", ans*3);    }    return 0;}


 

Adds an additional m row to determine whether there are n rows ....

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct node{    int l , r , k , h ;} p[1100][1100] ;void init(int n,int m){    int i , j , l , r , ll , top ;    char str[10000] ;    memset(p,0,sizeof(p));    for(i = 1 ; i <= n ; i++)    {        gets(str);        if( str[0] == '\n' )            break;        ll = strlen(str);        top = 1 ;        for(j = 0 ; j < ll ; j++)        {            if( str[j] == 'F' )                p[i][top++].k = 1 ;            else if( str[j] == 'R' )                p[i][top++].k = 0 ;        }    }    for(i = 1 ; i <= n ; i++)    {        for(j = 1 , l = 1 ; j <= m ; j++)        {            if( p[i][j].k == 0 )            {                p[i][j].l = -1 ;                l = j+1 ;            }            else                p[i][j].l = l ;        }        for(j = m , r = m ; j >= 1 ; j--)        {            if( p[i][j].k == 0 )            {                p[i][j].r = 999999 ;                r = j - 1 ;            }            else                p[i][j].r = r ;        }    }}int main(){    int t , i , j , n , m , l , r , temp , ans ;    char ch ;    scanf("%d", &t);    while(t--)    {        ans = -1 ;        scanf("%d %d ", &n, &m);        init(n,m);        for(i = 1 ; i <= m ; i++)        {            if( p[1][i].k == 1 )                p[1][i].h = 1 ;            else                p[1][i].h = 0 ;            temp = p[1][i].h * ( p[1][i].r - p[1][i].l + 1);            if( temp > ans )                ans = temp ;        }        for(i = 2 ; i <= n ; i++)        {            for(j = 1 ; j <= n ; j++)            {                if( p[i][j].k == 0 )                {                    p[i][j].h = 0 ;                }                else                {                    p[i][j].h = p[i-1][j].h + 1 ;                    p[i][j].l = max( p[i][j].l,p[i-1][j].l );                    p[i][j].r = min( p[i][j].r,p[i-1][j].r );                }                temp = p[i][j].h * ( p[i][j].r - p[i][j].l + 1 );                if(temp > ans)                    ans = temp ;            }        }        printf("%d\n", ans*3);    }    return 0;}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.