給一個陣列,問最大同色面積
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<cmath>
using namespace std;
typedef long long int int64;
typedef pair<int, int> pii;
int arr[3010][3010];
bool vis[3010][3010];
pii q[9000010];
int main()
{
int n, m;scanf("%d %d", &n, &m);
for(int lx = 0;lx < n;lx++)
for(int ly = 0;ly < m;ly++)
scanf("%d", &arr[lx+1][ly+1]);
memset(vis, false, sizeof(vis));
for(int lx = 0;lx <= n+1;lx++)
arr[lx][0] = -1, arr[lx][m+1] = -1;
for(int ly = 0;ly <= m+1;ly++)
arr[0][ly] = -1, arr[n+1][ly] = -1;
int mao = 0;
for(int lx = 1;lx <= n;lx++){
for(int ly = 1;ly <= m;ly++){
if(vis[lx][ly]) continue;
int st = 0, ed = 1;
int typ = arr[lx][ly];
q[0] = pii(lx, ly);
vis[lx][ly] = true;
int cnt = 0;
while(st < ed){
int px = q[st].first, py = q[st].second; st++;
cnt++;
if(vis[px+1][py] == false and typ == arr[px+1][py])
vis[px+1][py] = true, q[ed++] = pii(px+1, py);
if(vis[px-1][py] == false and typ == arr[px-1][py])
vis[px-1][py] = true, q[ed++] = pii(px-1, py);
if(vis[px][py-1] == false and typ == arr[px][py-1])
vis[px][py-1] = true, q[ed++] = pii(px, py-1);
if(vis[px][py+1] == false and typ == arr[px][py+1])
vis[px][py+1] = true, q[ed++] = pii(px, py+1);
}
mao = max(mao, cnt);
}
}
printf("%d\n", mao);
return 0;
}
沒有留言:
張貼留言