zoukankan      html  css  js  c++  java
  • 《挑战》2.1 POJ POJ 1979 Red and Black (简单的DFS)

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

    题目大意:

      就是说,对于一个h*w的网格,每次从‘@’开始,求最多能走多少个‘.’,每次只能够向四个方向进行扩展。

    解题思路:

      直接dfs,然后用book数组随时记录目前这个点有没有走过,如果这个点是'.'并且没有走过的话,那么我们就res++,由于是多组数据,每次输出结束后,记着memset,

    复杂度:

      O(4*w*h)

    代码: 

     1 # include<cstdio>
     2 # include<iostream>
     3 # include<cstring>
     4 
     5 using namespace std;
     6 
     7 # define MAX 23
     8 
     9 char grid[MAX][MAX];
    10 int book[MAX][MAX];
    11 int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1} };
    12 int res;
    13 int w,h;
    14 
    15 int can_move ( int x,int y )
    16 {
    17     if ( x>=0&&x<h&&y>=0&&y<w )
    18     {
    19         if ( grid[x][y]=='.'&&book[x][y]==0 )
    20             return 1;
    21     }
    22     return 0;
    23 }
    24 
    25 void dfs ( int x,int y )
    26 {
    27     book[x][y] = 1;
    28     for ( int i = 0;i < 4;i++ )
    29     {
    30         int n_x = x+nxt[i][0], n_y = y+nxt[i][1];
    31         if ( can_move ( n_x,n_y ) )
    32         {
    33             res++;
    34             book[n_x][n_y] = 1;
    35             dfs(n_x,n_y);
    36         }
    37     }
    38 }
    39 
    40 int main(void)
    41 {
    42     while ( scanf("%d%d",&w,&h)!=EOF )
    43     {
    44         res = 1;
    45         if ( w==0&&h==0 )
    46             break;
    47         for ( int i = 0;i < h;i++ )
    48             scanf("%s",grid[i]);
    49         for ( int i = 0;i < h;i++ )
    50         {
    51             for ( int j = 0;j < w;j++ )
    52             {
    53                 if ( grid[i][j]=='@' )
    54                 {
    55                     book[i][j] = 1;
    56                     dfs(i,j);
    57                 }
    58             }
    59         }
    60 
    61         printf("%d
    ",res);
    62         memset(book,0,sizeof(book));
    63     }
    64 
    65 
    66 
    67     return 0;
    68 }
  • 相关阅读:
    gaia 开源多语言的pipeline 平台
    vernemq 集群 docker-compose 搭建简单试用
    nginx http2 push 试用
    几个方便进行micro frontend 开发的工具&&类库
    Compoxure example 应用说明
    Compoxure 微服务组合proxy 中间件
    SCS Characteristics
    How_Require_Extensions_Work
    nodejs-hook 开发
    microcks 微服务mocks 工具&&运行时
  • 原文地址:https://www.cnblogs.com/wikioibai/p/4675109.html
Copyright © 2011-2022 走看看