Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

简单dfs(附代码)

Posted by Ink213 at 2013-04-04 15:18:58 on Problem 1562
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=105;
bool map[MAXN][MAXN];
int dx[10]={0,0,1,-1,-1,-1,1,1};
int dy[10]={1,-1,0,0,-1,1,-1,1};
int n, m;
void dfs(int x,int y)
{
    int i, nx, ny;
    map[x][y]=false;
    for(i=0;i<8;i++)
    {
        nx=x+dx[i], ny=y+dy[i];
        if(nx<1||ny<1||nx>n||ny>m) continue;
        if(map[nx][ny]) dfs(nx,ny);
    }
}
int main()
{
    int i, j, num;
    char a;
    while(scanf("%d%d",&n,&m)&&(n||m))
    {
        num=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf(" %c",&a);
                if(a=='@')
                {
                    map[i][j]=true;
                }
                else
                {
                    map[i][j]=false;
                }
            }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(map[i][j])
                {
                    dfs(i,j);
                    num++;
                }
            }
        }
        printf("%d\n",num);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator