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
    
    
    
     
  • 相关阅读:
    EF 错误解决
    TortoiseHg 学习笔记 (转)
    Mysql 命令行 使用 (转)
    2017-9-3 时间字符串格式化(转)
    2017-8-25 c# 获取url参数的五种方法(转)
    alert 的使用方法
    表单关键字查询写法
    Mysql和Mysqli的区别
    php MySQL中 增、删、改、查的写法格式
    一维、二维数组 与 常用的返回数组 以及 fetch_all与fetch_row的区别
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11230286.html
Copyright © 2011-2022 走看看