• poj 1979 Red and Black


    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) 
    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
    

    Source

    简单dfs。
    代码:
    import java.util.*;
    
    public class Main {
        static int w,h,ans;
        static char mp[][];
        static boolean vis[][];
        static int dir[][];
        static void dfs(int x,int y) {
            for(int i = 0;i < 4;i ++) {
                int tx = x + dir[i][0];
                int ty = y + dir[i][1];
                if(tx < 0 || ty < 0 || tx >= h || ty >= w || vis[tx][ty] || mp[tx][ty] == '#')continue;
                vis[tx][ty] = true;
                ans ++;
                dfs(tx,ty);
            }
        }
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int sx = 0,sy = 0;
            dir = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
            while(true) {
                w = in.nextInt();
                h = in.nextInt();
                if(w == 0 && h == 0)break;
                mp = new char[h][w];
                vis = new boolean[h][w];
                ans = 1;
                for(int i = 0;i < h;i ++) {
                    String s = in.next();
                    for(int j = 0;j < s.length();j ++) {
                        if(s.charAt(j) == '@') {
                            sx = i;
                            sy = j;
                        }
                        mp[i][j] = s.charAt(j);
                    }
                }
                vis[sx][sy] = true;
                dfs(sx,sy);
                System.out.println(ans);
            }
        }
    }
  • 相关阅读:
    不同版本strtotime("2016-09-04")输出不同问题
    Jquery,YUI这个著名js库名称作用的理解
    函数和方法
    js的关联数组
    windows信息
    改centos7的网卡名
    GIT命令
    安装时遇到:正在尝试其它镜像。 http://mirrors.btte.net/centos/7.2.1511/extras/x86_64/repodata/repomd.xml: [Errno 14] curl#6
    本地怎样访问虚拟机上的服务器
    yolo
  • 原文地址:https://www.cnblogs.com/8023spz/p/9458617.html
Copyright © 2020-2023  润新知