LDU训练赛:钻石游戏 模拟

题目描述

在这里插入图片描述

分析

需要理解题意,每次交换的分值为通过这次交换后能够形成的最大矩形的面积,意思就是交换的两个方块形成的最大矩形面积
所以我们可以从两个交换的方块向四周进行扩展,因为两个方块颜色不一样,所以只有可能向一个方向扩展,然后我们去维护每一层最小的边长即可

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <unordered_map>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 600;
int n,m;
int g[N][N];
int q;

int dfs1(int x,int y){ //向上拓展
    int res = 1;
    int c = g[x][y];
    int l1 = 1;
    int l2 = INF;
    int l;
    for(int i = x;i;i--){
        if(g[i][y] != c) break;
        int p1 = y;
        while(g[i][p1 - 1] == c && p1) p1--;
        l1 = max(l1,p1);
        int p2 = y;
        while(g[i][p2 + 1] == c && p2 <= m) p2++;
        l2 = min(l2,p2);
        l = l2 - l1 + 1;
        res = max((x - i + 1) * l,res);
    }
    return res;
}

int dfs2(int x,int y){ //向下拓展
    int res = 1;
    int c = g[x][y];
    int l1 = 1;
    int l2 = INF;
    int l;
    for(int i = x;i <= n;i++){
        if(g[i][y] != c) break;
        int p1 = y;
        while(g[i][p1 - 1] == c && p1) p1--;
        l1 = max(l1,p1);
        int p2 = y;
        while(g[i][p2 + 1] == c && p2 <= m) p2++;
        l2 = min(l2,p2);
        l = l2 - l1 + 1;
        res = max((i - x + 1) * l,res);
    }
    return res;
}

int dfs3(int x,int y){ //向左拓展
    int res = 1;
    int c = g[x][y];
    int l1 = 1;
    int l2 = INF;
    int l;
    for(int i = y;i;i--){
        if(g[x][i] != c) break;
        int p1 = x;
        while(g[p1 - 1][i] == c && p1) p1--;
        l1 = max(l1,p1);
        int p2 = x;
        while(g[p2 + 1][i] == c && p2 <= n) p2++;
        l2 = min(l2,p2);
        l = l2 - l1 + 1;
        res = max((y - i + 1) * l,res);
    }
    return res;
}

int dfs4(int x,int y){ //向右拓展
    int res = 1;
    int c = g[x][y];
    int l1 = 1;
    int l2 = INF;
    int l;
    for(int i = y;i <= m;i++){
        if(g[x][i] != c) break;
        int p1 = x;
        while(g[p1 - 1][i] == c && p1) p1--;
        l1 = max(l1,p1);
        int p2 = x;
        while(g[p2 + 1][i] == c && p2 <= n) p2++;
        l2 = min(l2,p2);
        l = l2 - l1 + 1;
        res = max((i - y + 1) * l,res);
    }
    return res;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i++)
        for(int j = 1;j <= m;j++)
            scanf("%d",&g[i][j]);
    scanf("%d",&q);
    while(q--){
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        swap(g[x1][y1],g[x2][y2]);
        if(x1 == x2){
            if(y1 < y2) printf("%d\n",max(dfs3(x1,y1),dfs4(x2,y2)));
            else printf("%d\n",max(dfs4(x1,y1),dfs3(x2,y2)));
            // cout << dfs3(x1,y1) << ' ' << dfs4(x2,y2) << endl;
        }
        else{
            if(x1 < x2) printf("%d\n",max(dfs1(x1,y1),dfs2(x2,y2)));
            else printf("%d\n",max(dfs2(x1,y1),dfs1(x2,y2)));
        }
    }
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值