• 【搜索入门专题练习1】hdu1241+hdu1312 C+D【DFS】


    C    Oil Deposits

    Problem Description
    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
    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
     

    题意:输出有多少个连续的'@',单独的'@'算作一个。水DFS。


    #include<stdio.h>
    #include<string.h>
    #define N 110
    
    char str[N][N];
    int n, m,count;
    void dfs(int x,int y)
    {
    	int k[8][2] = {0,1,-1,1,-1,0,-1,-1,0,-1,1,-1,1,0,1,1};
    	int i;
    	int tx,ty;
    	for(i = 0; i < 8; i ++)
    	{
    		tx = x+k[i][0];
    		ty = y+k[i][1];
    		if(tx<0||ty<0||tx>n-1||ty>m-1)
    			continue;
    		if(str[tx][ty] == '@')
    		{
    			str[tx][ty] = '*';
    			dfs(tx,ty);
    		}
    	}
    	return;
    }
    
    int main()
    {
    	int i,j;
    	
    	while(scanf("%d%d",&n,&m),m!=0)
    	{
    		count = 0;
    		memset(str,0,sizeof(str));
    		for(i = 0;i < n; i ++)
    			scanf("%s",str[i]);
    		for(i = 0; i < n; i ++)
    		{
    			for(j = 0; j < m; j ++)
    			{
    				if(str[i][j] == '@')
    				{
    					count++;
    					dfs(i,j);
    				}
    			}
    		}
    		printf("%d
    ",count);
    	}
    	return 0;
    }


    D   Red and Black


    Problem Description
    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
     
    题意:从'@'开始,最多能够走过多少个'.',输入0,0结束输入。水DFS。

    
    
    #include<stdio.h>
    #include<string.h>
    #define N 30
    
    char str[N][N];
    int book[N][N];
    int n,m,step;
    
    void dfs(int x,int y)
    {
    	int k[4][2] = {0,1,-1,0,0,-1,1,0};
    	int i;
    	int tx,ty;
    	for(i = 0; i < 4; i ++)
    	{
    		tx = x + k[i][0];
    		ty = y + k[i][1];
    		if(tx < 0||ty < 0||tx > n-1||ty>m-1)
    			continue;
    		if(str[tx][ty]!='#'&&book[tx][ty]!=1)
    		{
    			book[tx][ty] = 1;
    			step++;
    			dfs(tx,ty);
    		}
    	}
    	return;
    }
    
    
    int main()
    {
    	int i,j;
    	while(scanf("%d%d",&m,&n),(m+n)!=0)
    	{
    		step = 1;
    		memset(book,0,sizeof(book));
    		for(i = 0; i < n; i ++)
    			scanf("%s",str[i]);
    		for(i = 0; i < n; i++)
    		{
    			for(j = 0; j < m; j ++)
    			{
    				if(str[i][j] == '@')
    				{
    					book[i][j] = 1;
    					dfs(i,j);
    				}
    					
    			}
    		}
    		printf("%d
    ",step);
    	}
    	return 0;
    }



  • 相关阅读:
    rabbitmq channel参数详解
    rabbitmq direct、fanout、topic 三种Exchange java 代码比较
    spark 性能优化
    spark sql 窗口函数over partition by
    Python爬虫从入门到进阶(4)之xpath的使用
    Python爬虫从入门到进阶(3)之requests的使用
    Python爬虫从入门到进阶(2)之urllib库的使用
    Mac环境下Redis的安装
    Mac环境下Vagrant的安装
    Mac环境下Scrapy的安装
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350099.html
Copyright © 2020-2023  润新知