zoukankan      html  css  js  c++  java
  • POJ 1979 题解

    Red and Black
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 31722   Accepted: 17298

    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

     
     1 #include "cstdio"
     2 #include "cstring"
     3 #include "cmath"
     4 #include "iostream"
     5 #include "string"
     6 
     7 using namespace std ;
     8 const int maxN = 210 ;
     9 
    10 const int dx[ 10 ] = { 1 , 0 , 0 , -1 } ;
    11 const int dy[ 10 ] = { 0 , 1 , -1 , 0 } ;
    12 
    13 char mp[ maxN ][ maxN ] ;
    14 int N , M ;
    15 
    16 void DFS ( const int xi , const int yi ) {
    17         if ( !mp[ xi ][ yi ] )return ;
    18         mp[ xi ][ yi ] = '%' ;
    19         for ( int i=0 ; i<4 ; ++i ) {
    20                 int xx = xi + dx[ i ] ;
    21                 int yy = yi + dy[ i ] ;
    22                 if ( xx > 0 && xx <= M && yy > 0 && yy <= N && mp[ xx ][ yy ] == '.' )  
    23                         DFS( xx , yy ) ;
    24         }
    25 }
    26 
    27 int sumerize ( const int n , const int m ) {
    28         int ret ( 0 ) ; 
    29         for ( int i=1 ; i<=n ; ++i ) {
    30                 for ( int j=1 ; j<=m ; ++j ) {
    31                         if ( mp[ i ][ j ] == '%' ) ++ret ; 
    32                 }
    33         }
    34         return ret ;
    35 }
    36 
    37 int main ( ) {
    38         int start_x , start_y ; 
    39         while ( scanf ( "%d%d
    " , &N , &M ) == 2 && N && M ) {
    40                 for ( int i=1 ; i<=M ; ++i ) {
    41                         scanf ( "%s" , mp[ i ] + 1 ) ;
    42                 }
    43                 for ( int i=1 ; i<=M ; ++i ) {
    44                         for ( int j=1 ; j<=N+1 ; ++j ) {
    45                                 if ( mp[ i ][ j ] == '@' ) {
    46                                         mp[ i ][ j ] = '.' ; 
    47                                         start_x = i ; 
    48                                         start_y = j ;
    49                                         goto Loop ; 
    50                                 }
    51                         }
    52                 }
    53                 Loop :
    54                 DFS ( start_x , start_y ) ;
    55                 
    56                 printf ( "%d
    " , sumerize ( M , N ) ) ; 
    57                 memset ( mp , 0 , sizeof ( mp ) ) ;
    58         } 
    59         
    60         return 0 ; 
    61 } 
    View Code

    2016-10-21  16:28:48

  • 相关阅读:
    this is test,,this is only test!!!
    js设置鼠标悬停改变背景色
    免费数据恢复软件
    ORA-00600: 内部错误代码
    js控制只能输入数字和控制小数的位数
    Eclipse换背景色
    记JavaScript的入门学习(二)
    html+css基础篇
    记JavaScript的入门学习(一)
    前端之Photoshop切片
  • 原文地址:https://www.cnblogs.com/shadowland/p/5985104.html
Copyright © 2011-2022 走看看