Source Code
Problem: 1562 User: lnmm
Memory: 96K Time: 0MS
Language: C++ Result: Accepted
Source Code
#include "stdio.h"
#include "iostream.h"
bool visited[101][101];
char map[101][101];
int m,n,i,j;
int d[8][2]={{1,0},{1,1},{1,-1},{0,-1},{0,1},{-1,0},{-1,1},{-1,-1}};
bool In(int x,int y)
{
return x>0&&y>0&&y<=n&&x<=m;
}
void Search(int x,int y)
{
visited[x][y]=true;
for(int i=0;i<8;i++)
if( In(x+d[i][0],y+d[i][1])&&map[x+d[i][0]][y+d[i][1]]=='@'&&!visited[x + d[i][0]][y + d[i][1]])
Search(x + d[i][0], y + d[i][1]);
}
int solves()
{
int count=0;
visited[i][j]=true;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(map[i][j]=='@'&&!visited[i][j])
{Search(i,j);
++count; }
return count;
}
void main()
{
while(cin >> m >> n && (m || n))
{
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
cin>>map[i][j];
visited[i][j]=false;
}
}
cout<<solves()<<endl;
}
}
很简单的深度优先搜索,纯属练手.
简单得就像是在完全套用模板...
开始用scanf来读map[i][j],缺老报错,百思不得其解???
后来改用万能的iostream ,还好测试数据比较弱,仍然0ms通过
iostream才是王道...