City Game UVA - 1330(最大子矩阵DP,很经典的一道题目)

City Game UVA - 1330

题意:

给你一个矩阵有F(空地)和R(障碍)。现要你求一个由F组成的最大的子矩阵。输出其乘以3的结果。

思路:

我们把每个格子向上延伸的连线空格看成一条悬线,并且用up(i,j),left(i,j),right(i,j) 表示格子(i,j)的悬线长度以及悬线向左、向右运动的“运动极限”。
之后更新过程具体看代码。

AC(2021-3-9)

/*
皮卡丘冲鸭!
へ     /|
  /\7    ∠_/
  / │   / /
 │ Z _,< /   /`ヽ
 │     ヽ   /  〉
  Y     `  /  /
 イ● 、 ●  ⊂⊃〈  /
 ()  へ    | \〈
  >ー 、_  ィ  │ //
  / へ   / ノ<| \\
  ヽ_ノ  (_/  │//
  7       |/
  >―r ̄ ̄`ー―_
*/
#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define ALL(x) x.begin(),x.end()
#define mp make_pair
#define fi first
#define se second
#define db double
#define endl '\n'
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
typedef pair<db,db> pdd;

const int N = 2e5+10;
const int M = 1e5;
const int maxn=2e5+10;
const db eps = 1e-8;
const db pi = acos(-1.0);

template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
  print(x), putchar(let);
}

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

char s[5010][5010];
int n, m;

//#define LOCAL
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
int tt;
cin>>tt;
while(tt--){
    cin>>n>>m;
    For(i,1,n)For(j,1,m)cin>>s[i][j];
    For(i,1,n)For(j,1,m) s[i][j] = s[i][j]=='F'?1:0;//s[i][j] -='0';
    int mxl[5100]{}, mxr[5100];
    int h[5100]{};
    int ans = 0;
    For(i,1,m) mxr[i] = mxl[i] = inf;
    For(i,1,n){
        int cnt = 0;
        For(j,1,m){
            if(s[i][j]){
                cnt++;
                ckmin(mxl[j],cnt);
                h[j]++;
                //ckmax(ans,h[i]*mx[i]);
                //ckmax(ans,cnt);
            }
            else cnt = 0, h[j] = 0, mxl[j] = inf;
        }
        cnt = 0;
        rep(j,m,1){
            if(s[i][j]){
                cnt++;
                ckmin(mxr[j], cnt);
                ckmax(ans,(mxr[j]+mxl[j]-1)*h[j]);
            }else cnt = 0, h[j] = 0, mxr[j] = inf;
        }
    }
    cout<<ans*3<<endl;
}
    return 0;
}

AC(2020-12-18)

#include <bits/stdc++.h>
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
using namespace std;
const int maxn =1000;
int mat[maxn][maxn], up[maxn][maxn], leftt[maxn][maxn], rightt[maxn][maxn];
int main()
{
    int tt; scanf("%d", &tt);
    while(tt--){
        int m, n;
        scanf("%d%d", &m, &n);
        fori(i,0,m)fori(j,0,n){
            int ch = getchar();
            while(!isalpha(ch))ch = getchar();
            mat[i][j] = ch == 'F' ? 0 : 1;
        }
//        fori(i,0,m){
//            fori(j,0,n)cout<<mat[i][j];
//            cout<<endl;
//        }
//        ///
//        cout<<"up right left the matrix"<<endl;
//        cout<<"--------------------------"<<endl;
        int ans = 0;
        fori(i,0,m){
            int lo = -1, ro = n;///the left object and the right object
            fori(j,0,n){
                if(mat[i][j] == 1){
                    up[i][j] = leftt[i][j] = 0;
                    lo = j;
                }else {
                    up[i][j] = i == 0 ? 1 : (up[i-1][j]+1);
                    leftt[i][j] = i == 0 ? lo+1 :max(leftt[i-1][j], lo+1);
                }
            }
            rep(j,n-1,0){
                if(mat[i][j] == 1){
                    rightt[i][j] = n;
                    ro = j;
                }else {
                    rightt[i][j] = i == 0 ? ro-1 : min(rightt[i-1][j], ro-1);
                    ans = max(ans, up[i][j]*(rightt[i][j]-leftt[i][j]+1));
                }
            }
        }
//        cout<<"up-------------"<<endl;
//        fori(i,0,m){
//            fori(j,0,n)cout<<up[i][j];
//            cout<<endl;
//        }
//        cout<<"left-------------"<<endl;
//        fori(i,0,m){
//            fori(j,0,n)cout<<leftt[i][j];
//            cout<<endl;
//        }
//        cout<<"right-------------"<<endl;
//        fori(i,0,m){
//            fori(j,0,n)cout<<rightt[i][j];
//            cout<<endl;
//        }
        printf("%d\n", ans*3);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值