• Red and Black (简单dfs)


     
    题目:https://vjudge.net/problem/POJ-1979
     
     
     
    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 Out

      45

    59
    6
    13


    题意:
    从@点出发,,问能走多少个.

    思路:简单dfs

    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<queue>
    #include<set>
    #include<algorithm>
    #include<map>
    #define maxn 200005
    using namespace std;
    int x[4]={-1,1,0,0};
    int y[4]={0,0,1,-1};
    char mp[1005][1005];
    int vis[1005][1005]={0};
    int nx,ny;
    int ans;
    int n,m;
    int s1,s2;
    void dfs(int i,int j)
    {
        if(i<0||i>n||j<0||j>m)return ;
        vis[i][j]=1;
        for(int k=0;k<4;k++){
            nx=i+x[k];ny=j+y[k];
            if(nx>=0&&nx<n&&ny>=0&&ny<m&&!vis[nx][ny]&&mp[nx][ny]=='.'){ans++;vis[nx][ny]=1;dfs(nx,ny);}
        }
    }
    int main()
    {
        while(cin>>m>>n){
                if(m==0&&n==0)break;
        getchar();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
        {
            cin>>mp[i][j];
            if(mp[i][j]=='@'){s1=i;s2=j;}
        }
        memset(vis,0,sizeof(vis));
        ans=0;
        dfs(s1,s2);
        cout<<ans+1<<endl;
        }
        return 0;
    }




  • 相关阅读:
    Sizzle选择器引擎介绍
    from、includes、indexOf
    indexOf与includes的区别
    ES6 find 和 filter 的区别
    Spring MVC多个视图解析器及优先级
    Spring MVC多个视图解析器及优先级
    freemarker 中文乱码问题解决办法
    freemarker 中文乱码问题解决办法
    freemarker 中文乱码问题解决办法
    Spring集成MyBatis 通用Mapper以及 pagehelper分页插件
  • 原文地址:https://www.cnblogs.com/huangzzz/p/8604922.html
Copyright © 2020-2023  润新知