给出一个二维数组,每个字母表示一种颜色,问指定的颜色周围有几种不同的颜色,已知相同的颜色一定有公共边
直接暴力遍历就行,注意标记遇到过的颜色
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,vis[105][105],ans=0;
char map[105][105];
int dx[]={0,-1,0,1},dy[]={1,0,-1,0};
int s[105];
void dfs(int x,int y,char ch)
{
vis[x][y]=1;
if(map[x][y]!=ch)
{
if(!s[map[x][y]])
{
++ans;
}
s[map[x][y]]=1;
return;
}
for(int i=0;i<4;++i)
{
int tx=x+dx[i],ty=y+dy[i];
if(tx<0||tx>=n||ty<0||ty>=m||map[tx][ty]=='.'||s[map[tx][ty]])
{
continue;
}
if(!vis[tx][ty])
{
dfs(tx,ty,ch);
}
}
}
void slove(char x)
{
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(map[i][j]==x)
{
dfs(i,j,x);
return;
}
}
}
}
int main()
{
char x;int bx,by;
scanf("%d%d %c",&n,&m,&x);
for(int i=0;i<n;++i)
{
scanf("%s",map[i]);
}
slove(x);
printf("%d\n",ans);
return 0;
}