uva 1330 City Game

点击打开链接uva 1330

思路:悬线法求解最大子矩阵
分析:
1 详细资料请见点击打开链接
2 有个地方需要注意的是输入格式,有可能输入字母后面会有多个空格,所以必须要过滤掉这些空格

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 1010;
int mat[MAXN][MAXN];
int up[MAXN][MAXN] , Left[MAXN][MAXN] , Right[MAXN][MAXN];
int n , m;

int solve(){
    int ans = 0;
    int leftNum , rightNum;
    for(int i = 1 ; i <= n ; i++){
       //从左往右求最大的左边列编号
       leftNum = 0;
       for(int j = 1 ; j <= m ; j++){
          if(!mat[i][j]){
             up[i][j] = 0;
             Left[i][j] = 0;
             leftNum = j;
          }
          else{
             up[i][j] = i == 1 ? 1 : up[i-1][j]+1;
             Left[i][j] = i == 1 ? leftNum+1 : max(Left[i-1][j] , leftNum+1);
          }
       }
       //从右往左求最小的右边列编号
       rightNum = m+1;
       for(int j = m ; j >= 1 ; j--){
          if(!mat[i][j]){
             Right[i][j] = m+1;
             rightNum = j;
          }
          else{
             Right[i][j] = i == 1 ? rightNum-1 : min(Right[i-1][j] , rightNum-1);
             ans = max(ans , up[i][j]*(Right[i][j]-Left[i][j]+1));
          }
       }
    }
    return 3*ans;
}

int main(){
    int Case;
    char c;
    scanf("%d" , &Case);
    while(Case--){
        scanf("%d%d" , &n , &m);
        for(int i = 1 ; i <= n ; i++){
           for(int j = 1 ; j <= m ; j++){
               c = getchar();
               while(c != 'F' && c != 'R')
                   c = getchar();
               mat[i][j] = c == 'F' ? 1 : 0;
           }
        }
        printf("%d\n" , solve());
    }
    return 0;
}
时间: 2024-10-29 04:41:13

uva 1330 City Game的相关文章

SEERC 2004 / UVa 1330 City Game (扫描)

1330 - City Game Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 Bob is a strategy game programming specialist. In his new city building game the ga

uva 1330 - City Game 模拟

    扫描线,记录l,r,u,答案就是(r+l-1)*u     实际上只要开个2维数组就可以了 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <

UVA之1330 - City Game

[题目] 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 tha

算法题:uva 1330

题目链接: http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 以前做过一道一维的,这题只是变成了二维的,其他方法都一样.HDU 1506  Largest Rectangle in a Histogram   题解 代码1: #include<cstdio> #include<cs

UVa 507:Jill Rides Again

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=448 类型: 最大连续和 原题: Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill oft

UVa 11054:Wine trading in Gergovia

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1995 [原题] As you may know from the comic "Asterix and the Chieftain's Shield", Gergovia consists of one street, and

UVa 10048:Audiophobia(Floyd, Kruskal)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=989 题目: Problem B: Audiophobia Consider yourself lucky! Consider yourself lucky to be still breathing and having fun participati

UVa 10099:The Tourist Guide(Floyd, 最大生成树)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1040 题目: Problem D The Tourist Guide Input: standard input Output: standard output Mr. G. works as a tourist guide. His current

UVa 10596:Morning Walk, 赤裸裸的欧拉回路

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1537 题目类型: 欧拉回路, 并查集, dfs 题目: Kamal is a Motashota guy. He has got a new job in Chittagong. So, he has moved to Chittagong fr