要注意的地方就是并不是n*n的矩阵,列要单独求
dfs一下
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e6;
int n,m;
char mp[60][60];
int ans;
void dfs_R(int x , int y)
{
if(x >= 0 && x < n && y >= 0 && y < m && (mp[x][y] == ‘R‘ || mp[x][y] == ‘G‘))
{
if(mp[x][y] == ‘G‘) mp[x][y] = ‘B‘;
else mp[x][y] = ‘.‘;
dfs_R(x-1,y-1);
dfs_R(x+1,y+1);
}
return;
}
void dfs_B(int x, int y)
{
if(x >= 0 && x < n && y >= 0 && y < m && (mp[x][y] == ‘B‘ || mp[x][y] == ‘G‘))
{
if(mp[x][y] == ‘G‘) mp[x][y] = ‘R‘;
else mp[x][y] = ‘.‘;
dfs_B(x+1,y-1);
dfs_B(x-1,y+1);
}
return;
}
int main()
{
#ifdef xxz
//freopen("out.txt","w",stdout);
freopen("in.txt","r",stdin);
#endif // xxz
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%s",mp[i]);
int ans = 0;
m = strlen(mp[0]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(mp[i][j] == ‘R‘)
{
dfs_R(i,j);
ans++;
}
else if(mp[i][j] == ‘B‘)
{
dfs_B(i,j);
ans++;
}
else if(mp[i][j] == ‘G‘)
{
dfs_R(i,j);
mp[i][j] = ‘B‘;
dfs_B(i,j);
ans += 2;
}
}
}
printf("%d\n",ans);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-09 19:26:53