当前位置 : 主页 > 编程语言 > java >

计蒜客 颜色 dfs + set

来源:互联网 收集:自由互联 发布时间:2022-07-04
​​传送门​​ 思路:和求连通块的问题差不多,用set去重。 /** * From: * Qingdao Agricultural University * Created by XiangwangAcmer * Date : 2019-10-26-13.00.50 * Talk is cheap.Show me your code. */ #includeiostream


​​传送门​​ 思路:和求连通块的问题差不多,用set去重。

/**
* From:
* Qingdao Agricultural University
* Created by XiangwangAcmer
* Date : 2019-10-26-13.00.50
* Talk is cheap.Show me your code.
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<cstdlib>
#define ll long long
using namespace std;
const ll maxn = 1e6 + 5;
const ll minn = 1e9 + 5;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LIMIT = 4294967295LL;
vector<int>v[maxn];
int dp[maxn];
vector<int>G[maxn];
bool row[maxn], col[maxn];
bool flag = 0;
queue<int>q;
int n, m;
int cnt = 0;
set<ll>s;
ll a[1005][1005];
ll vis[1005][1005];
void dfs(int x, int y)
{
if(a[x][y] == 0 || x <= 0 || x > n || y <= 0 || y > m)
return ;
s.insert(a[x][y]);
a[x][y] = 0;
dfs(x + 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
dfs(x - 1, y);
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
ll maxnn=0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
cin >> a[i][j];
if(a[i][j])
vis[i][j] = 1;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
if(vis[i][j])
{
dfs(i, j);
ll t =s.size();
maxnn = max(maxnn, t);
s.clear();
}
cout << maxnn << endl;
return 0;
}


上一篇:洛谷 P2142 高精度减法
下一篇:没有了
网友评论