zoukankan      html  css  js  c++  java
  • Day2-K-Red and Black-HDU1312

    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. 

    InputThe 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) 
    OutputFor 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求连通,读入时找到起点即可,代码如下:
    const int maxm = 25;
    const int dx[] = {-1, 1, 0, 0};
    const int dy[] = {0, 0, -1, 1};
    
    int m, n, sx, sy, vis[maxm][maxm], t;
    char buf[maxm][maxm];
    
    bool inside(int x,int y) {
        return x >= 0 && x < m && y >= 0 && y < n;
    }
    
    void dfs(int x,int y) {
        if(vis[x][y])
            return;
        vis[x][y] = 1;
        for (int i = 0; i < 4; ++i) {
            int nx = x + dx[i], ny = y + dy[i];
            if(inside(nx,ny) && buf[nx][ny] == '.' &&!vis[nx][ny]) {
                ++t;
                dfs(nx, ny);
            }
        }
    }
    
    
    int main() {
        while(scanf("%d%d",&n,&m) && m + n) {
            getchar();
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    scanf("%c", &buf[i][j]);
                    if(buf[i][j] == '@') {
                        sx = i, sy = j;
                    }
                }
                getchar();
            }
            memset(vis,0,sizeof(vis));
            t = 1;
            dfs(sx, sy);
            printf("%d
    ", t);
        }
        return 0;
    }
    View Code
    
    
    
     
  • 相关阅读:
    国内医保控费公司简单比较
    程序员生存定律--管理向左,技术向右
    程序员生存定律--细论软件这个行当的根本特征
    程序员生存定律--细论影响人生成绩的四个要素(2)
    程序员生存定律--细论影响人生成绩的四个要素(1)
    程序员生存定律--定律的概要
    程序员生存定律--交换是职场里一切的根本
    程序员生存定律--目录
    程序员生存定律--那个是你的人生出口
    程序员生存定律--程序人生的出口
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11230286.html
Copyright © 2011-2022 走看看