标签:
Description
Input
Output
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
#include <iostream>
#include <cstdio>
#include <cstring>
const int maxn=100+5;
char s[maxn][maxn];
int bi_ji[maxn][maxn];
int m,n;
using namespace std;
void dfs(int r,int c,int d)
{
if(r<0||r>=m||c<0||c>=n)
return;
if(bi_ji[r][c]>0||s[r][c]!=‘@‘)
return;
bi_ji[r][c]=d;
for(int dr=-1;dr<=1;dr++)
for(int dc=-1;dc<=1;dc++)
if(dr!=0||dc!=0)
dfs(r+dr,c+dc,d);
}
int main()
{
int qu;
while(scanf("%d%d",&m,&n)==2&&m&&n)
{
memset(bi_ji,0,sizeof(bi_ji));
qu=0;
for(int i=0;i<m;i++)
scanf("%s",s[i]);
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(!bi_ji[i][j]&&s[i][j]==‘@‘)
dfs(i,j,++qu);
printf("%d\n",qu);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/xl1164191281/p/4696710.html