Chinaunix首页 | 论坛 | 博客
  • 博客访问: 434902
  • 博文数量: 103
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 971
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-11 17:22
文章分类
文章存档

2008年(77)

2007年(26)

我的朋友

分类: C/C++

2008-03-17 18:34:52

#include <stdio.h>
#include <stdlib.h>

int visit[100][100];
char array[100][100];
int x[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int y[8] = {1, 0, -1, 1, -1, 1, 0, -1};

int m, n;

static void dfs(char array[][100], int i, int j);
int main(int argc, char **argv)
{
        int i, j, num = 0;

        scanf("%d %d", &m, &n);
        getchar();
        for(i = 0; i < m; i++){
                for(j = 0; j < n; j++){
                        scanf("%c", &array[i][j]);
                }
                getchar();
        }

        for(i = 0; i < m; i++){
                for(j = 0; j < n; j++){
                        if((array[i][j] == 'W') && (visit[i][j] == 0)){
                                dfs(array, i, j);
                                num++;
                        }
                }
        }

        printf("%d\n", num);
        exit(0);
}

static void dfs(char array[][100], int i, int j)
{
        int p, flag, newrow, newcol;

        visit[i][j] = 1;
        for(p = 0; p < 8; p++){
                newrow = i + x[p];
                newcol = j + y[p];
                flag = ((array[newrow][newcol] == 'W') && (visit[newrow][newcol] == 0)
                        && (newrow >= 0) && (newrow < m) && (newcol >= 0) && (newcol < n));
                if(flag){
                        dfs(array, newrow, newcol);
                }
        }
}

阅读(753) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~