DFS deep first search深度优先搜索

Red and Black  HDU-1312

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles. 

Write a program to count the number of black tiles which he can reach by repeating the moves described above. 

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20. 

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows. 

'.' - a black tile 
'#' - a red tile 
'@' - a man on a black tile(appears exactly once in a data set) 

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself). 

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample Output

45
59
6
13
#include<bits/stdc++.h>
using namespace std;
int w,h,sum;//w列 h行
char mp[25][25];
int vis[25][25];
int dx[4]={0,0,1,-1};//(dx[i],dy[i])需要组成(1,0)(-1,0)(0,1)(0,-1)上下左右这四个方向
int dy[4]={1,-1,0,0};

void DFS(int x,int y)
{
	if(x<0||y<0||x>=h||y>=w||mp[x][y]=='#'||vis[x][y])//如果不能走
		return;//跳出本次递归函数
	else
    {
		sum++;
		int fx,fy;
		for(int i=0;i<4;i++){
			vis[x][y]=true;
			fx=x+dx[i];
			fy=y+dy[i];
			DFS(fx,fy);
		}
	}
}

int main(){
	while(scanf("%d%d",&w,&h)!=EOF&&(w||h))
    {
		memset(mp,0,sizeof(mp));
		memset(vis,0,sizeof(vis));
		sum=0;
		for(int i=0;i<h;i++)//h行 一行一行的输入
			scanf("%s",mp[i]);
		for(int i=0;i<h;i++)//w列 h行
		{
			for(int j=0;j<w;j++)
			{
				if(mp[i][j]=='@')//找到@的坐标开始
				{
					DFS(i,j);
					break;
				}
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

Oil Deposits  HDU-1241

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid. 

Input

扫描二维码关注公众号,回复: 2723871 查看本文章

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket. 

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets. 

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 
****@
*@@*@
*@**@
@@@*@
@@**@
0 0 

Sample Output

0
1
2
2
#include<bits/stdc++.h>
using namespace std;
int m,n;//m行n列
char mp[110][110];
int vis[110][110];
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};

void DFS(int x,int y)
{
    if(x<0||y<0||x>=m||y>=n||vis[x][y]==0)
        return ;

    else
    {
        vis[x][y]=0;//走过的
        for(int i=0;i<8;i++)
        {
            int fx=x+dx[i];
            int fy=y+dy[i];
            DFS(fx,fy);
        }
    }
}

int main()
{
    while(scanf("%d%d",&m,&n)!=EOF&&(m||n))
    {
        memset(mp,0,sizeof(mp));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
            scanf("%s",mp[i]);
        for(int i=0;i<m;i++)//先把这个图处理一下
        {
            for(int j=0;j<n;j++)
            {
                if(mp[i][j]=='*')
                    vis[i][j]=0;//0代表没有油矿
                else
                    vis[i][j]=1;
            }
        }
        int sum=0;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(vis[i][j]==1)//如果有油矿
                {
                    DFS(i,j);//把连通的油矿标为0
                    sum++;
                }
            }
        }
        printf("%d\n",sum);

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/VP_Death_Note/article/details/81328607