CE code
#include <bits/stdc++.h>
using namespace std;
struct node{
int x, y;
};
int sx, sy, a[501][501];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool vis[501][501];
vector<node> v;
set<vector<node> > s;
void dfs(int x, int y, int color){
if (vis[x])
return ;
vis[x][y] = true;
v.push_back({x - sx, y - sx});
for (int i = 0; i < 4; i++){
int tx = x + dx[i];
int ty = y + dy[i];
if (a[tx][ty] == color)
dfs(tx, ty, color);
}
}
int main(){
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] != 0){
v.clear();
sx = i, sy = j;
dfs(i, j, a[i][j]);
s.insert(v);
}
cout << s.size();
return 0;
}
第 36 行