题目描述
分析
需要理解题意,每次交换的分值为通过这次交换后能够形成的最大矩形的面积,意思就是交换的两个方块形成的最大矩形面积
所以我们可以从两个交换的方块向四周进行扩展,因为两个方块颜色不一样,所以只有可能向一个方向扩展,然后我们去维护每一层最小的边长即可
代码
#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
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/