| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
dfs 纯是自己做的,一次ac了,太爽了!!#include"iostream"
using namespace std;
int m,n;
char array[101][101];
int path[101][101];
void dfs(int i,int j)
{
path[i][j]=1; //对没走过的点进行置1 记录表示已经走过不能在走了。
if(i-1>=0&&array[i-1][j]=='@'&&path[i-1][j]==0)
dfs(i-1,j);
if(i+1<m&&array[i+1][j]=='@'&&path[i+1][j]==0)
dfs(i+1,j);
if(j-1>=0&&array[i][j-1]=='@'&&path[i][j-1]==0)
dfs(i,j-1);
if(j+1<n&&array[i][j+1]=='@'&&path[i][j+1]==0)
dfs(i,j+1);
if(i-1>=0&&j-1>=0&&array[i-1][j-1]=='@'&&path[i-1][j-1]==0)
dfs(i-1,j-1);
if(i-1>=0&&j+1<n&&array[i-1][j+1]=='@'&&path[i-1][j+1]==0)
dfs(i-1,j+1);
if(i+1<m&&j-1>=0&&array[i+1][j-1]=='@'&&path[i+1][j-1]==0)
dfs(i+1,j-1);
if(i+1<m&&j+1<n&&array[i+1][j+1]=='@'&&path[i+1][j+1]==0)
dfs(i+1,j+1);
}
int main()
{
while(cin>>m>>n,m)
{
int count=0; //用于对路径进行计算
memset(path,0,sizeof(path)); // 对path数组初始化,0表示没有走过
int i,j;
for( i=0;i<m;i++)
for( j=0;j<n;j++)
cin>>array[i][j];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(array[i][j]=='@'&&path[i][j]==0)
{
count++;
dfs(i,j); // 深度搜索
}
}
cout<<count<<endl;
}
return 1;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator