Connected Blocks dfs搜索题

来源:互联网 发布:淘宝网店运营推广方案 编辑:程序博客网 时间:2024/06/08 16:14

Problem Description


We have a map consist of n * m points. In the map, there are two type points. One is ‘@’, another is ‘#’. 


Then you need to count the connected blocks in the map.


But we have two way to count the blocks,so you need to count twice.


The first way, if two points ‘@’ which are adjacent to each other, they are in the same block.

The second way, if two points ‘@’ which are adjacent or diagonal to each other, they are in the same block.

Input


The first line contains an integer T(T<=20), then T cases follow.


In each case:


The first line contains 2 integer: n (1<=n<=100) and m(1 <= m <= 100), represent the rows and columns.


Then n line contains m characters. Each character represent a point in the map.


Output

For each case,output one line contains two integers, means the number of blocks in two ways.

Sample Input

2
2 3
@##
#@@
4 4
##@#
###@
@#@#
@###

Sample Output

2 1
4 2

Hint


In the first example,By the first way, the first block is (1, 1), the second block is (2, 2) and (2, 3).By the


second way, (1, 1), (2, 2) and (2, 3) are in the same block.


So the answer is 2 1.


题的大意就是搜索连通的“@”,而根据题意有两种搜索方式,一种是上下左右相邻才算,而另一种则是在前一种基础上,增添对角线条件。
关键在于搜索、递归、条件判断。
第一种递归要用到4个方向,dx1[4] - {0,0,-1,1},dy1[4] = {1,-1,0,0}.同理,第二种递归用到了八个方向。
只有满足没被查找过,且为‘@’的点满足条件,使cnt++,并以此点展开,dfs搜索,把(按照两种不同方式)连通的点标记为vis[i][j] = 1。

// Copyright (c) 2017 , Shawbert. Shaw// All rights reserved.//// Filename:  数@//// Description: dfs//// Version:  1.0// Created:  2017/12/18/20点21分// Compiler:  g++//// Author:  孙毓霄 Shaw#include <stdio.h>#include <stdlib.h>#include <string.h>#define maxn 103int vis[maxn][maxn];char mat[maxn][maxn];int n,m;int dx1[4] = {0,0,-1,1}, dy1[4] = {1,-1,0,0};int dx2[8] = {1,1,-1,-1,0,0,-1,1},dy2[8] = {-1,1,1,-1,1,-1,0,0};void dfs1(int i,int j){    if(i < 0 || i >= n || j < 0 || j >= m) return ;    if(vis[i][j] || mat[i][j] == '#') return ;    vis[i][j] = 1;    int direct;    for(direct = 0; direct < 4; direct++)    {        dfs1(i + dx1[direct],j + dy1[direct]);    }}void dfs2(int i,int j){    if(i < 0 || i >= n || j < 0 || j >= m) return ;    if(vis[i][j] || mat[i][j] == '#') return ;    vis[i][j] = 1;    int direct;    for(direct = 0; direct < 8; direct++)    {        dfs2(i + dx2[direct],j + dy2[direct]);    }}int main(){    int i,j;    int cnt1,cnt2;    int t;    scanf("%d",&t);    while(t--)    {        cnt1 = 0,cnt2 = 0;        scanf("%d%d",&n,&m);        for(i = 0; i < n; i++)        {            scanf("%s",mat[i]);        }        memset(vis,0,sizeof(vis));        for(i = 0; i < n; i++)        {            for(j = 0; j < m; j++)            {                if(mat[i][j] == '@' && vis[i][j] == 0)                {                    cnt1 ++;                    dfs1(i,j);                }            }        }        memset(vis,0,sizeof(vis));        for(i = 0; i < n; i++)        {            for(j = 0; j < m; j++)            {                if(mat[i][j] == '@' && vis[i][j] == 0)                {                    cnt2 ++;                    dfs2(i,j);                }            }        }        printf("%d %d\n",cnt1,cnt2);    }    return 0;}

原创粉丝点击