• poj 1979 Red and Black(dfs)


    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) 
    The end of the input is indicated by a line consisting of two zeros. 

    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

    题意:找出@能够到达的所有点的个数
    思路:dfs和递归都能做
    dfs代码:
     1 #include <iostream>
     2 #include<cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 using namespace std;
     7 char s[25][25];
     8 int a[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
     9 int ans,n,m;
    10 int dfs(int x,int y)
    11 {
    12     for(int i=0;i<4;i++)
    13     {
    14         int tx=x+a[i][0];
    15         int ty=y+a[i][1];
    16         if(tx>=m||tx<0||ty<0||ty>=n)
    17             continue;
    18         if(s[tx][ty]=='.')
    19         {
    20             s[tx][ty]='#';
    21             dfs(tx,ty);
    22             ans++;
    23         }
    24     }
    25     return 0;
    26 }
    27 int main()
    28 {
    29     while(~scanf("%d%d",&n,&m))
    30     {
    31         if(n==0||m==0)
    32             break;
    33         int flag=1;
    34         int tx,ty;
    35         for(int i=0;i<m;i++)
    36         {
    37             scanf("%s",s[i]);
    38             if(flag)
    39             {
    40                 for(int j=0;j<n;j++)
    41                 {
    42                     if(s[i][j]=='@')
    43                     {
    44                         tx=i,ty=j;
    45                         flag=0;
    46                         break;
    47                     }
    48                 }
    49             }
    50         }
    51         ans= 1;
    52         dfs(tx,ty);
    53         printf("%d
    ",ans);
    54     }
    55     return 0;
    56 }
    View Code

    递归代码:

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <stdio.h>
     5 int n,m;
     6 char data[30][30];
     7 using namespace std;
     8 int f(int i,int j)
     9 {
    10     if(i<0||j>n-1||i>m-1||j<0)
    11     return 0;
    12     if(data[i][j]=='#')
    13     return 0;
    14     data[i][j]='#';
    15     return 1+f(i-1,j)+f(i+1,j)+f(i,j-1)+f(i,j+1);
    16 }
    17 int main()
    18 {
    19     int x,y;
    20     while(~scanf("%d%d",&n,&m)&&(n!=0||m!=0))
    21     {
    22         getchar();
    23         for(int i=0;i<m;i++)
    24         scanf("%s",data[i]);
    25         for(int i=0;i<m;i++)
    26         for(int j=0;j<n;j++)
    27         if(data[i][j]=='@')
    28         {
    29             x=i;y=j;
    30         }
    31         cout<<f(x,y)<<endl;
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    设计模式
    包装类
    php 闭包的理解
    is_null empty isset等的判定
    PHP基础一 $this ,static 和 self的区别
    lumen安装踩过得坑
    composer的使用和安装
    使用submine来写c++
    php 和 thinkphp中的常量一览
    路径问题 ./ / ../ 空 的区别
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6212771.html
Copyright © 2020-2023  润新知