1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char date[110][110];
bool map[110][110];
int n,m;
int dfs(int a,int b)
{
if(map[a][b]==0) return 0;
else {
map[a][b]=0;
if(a>0){
if(b>0) dfs(a-1,b-1);
if(b<n-1) {
dfs(a-1,b+1);
}
dfs(a-1,b);
}
if(a<m-1)
{
if(b>0) dfs(a+1,b-1);
if(b<n-1)dfs(a+1,b+1);
dfs(a+1,b);
}
if(b>0) dfs(a,b-1);
if(b<n-1) dfs(a,b+1);
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
//printf("%d %d\n",m,n);
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++) cin>>date[i];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++)
{
map[i][j]=date[i][j]=='@'?true:false;
}//puts("");
}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char map[110][110];
int n,m;
queue<pair<int,int> > q;
int bfs(int a,int b)
{
if(map[a][b]=='*') return 0;
else {
q.push(make_pair(a,b));
while (!q.empty())
{
int x,y;
x=q.front().first;
y=q.front().second;
if(x>0){
if(y>0) if(map[x-1][y-1]=='@'){
map[x-1][y-1]='*';
q.push(make_pair(x-1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x-1][y+1]=='@')
{
map[x-1][y+1]='*';
q.push(make_pair(x-1,y+1));
}//bfs(a-1,b+1);
if(map[x-1][y]=='@')
{
map[x-1][y]='*';
q.push(make_pair(x-1,y));
}//bfs(a-1,b);
}
if(x<m-1)
{
if(y>0) if(map[x+1][y-1]=='@'){
map[x+1][y-1]='*';
q.push(make_pair(x+1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x+1][y+1]=='@')
{
map[x+1][y+1]='*';
q.push(make_pair(x+1,y+1));
}//bfs(a-1,b+1);
if(map[x+1][y]=='@')
{
map[x+1][y]='*';
q.push(make_pair(x+1,y));
}//bfs(a-1,b);
}
if(y>0)
if(map[x][y-1]=='@')
{
map[x][y-1]='*';
q.push(make_pair(x,y-1));
}//bfs(a,b-1);
if(y<n-1)
if(map[x][y+1]=='@')
{
map[x][y+1]='*';
q.push(make_pair(x,y+1));
}//bfs(a,b+1);
q.pop();
}
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++){ cin>>map[i];}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=bfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}hud 1241 Oil Deposits,布布扣,bubuko.com
原文:http://blog.csdn.net/alpc_paul/article/details/38120289