zoukankan      html  css  js  c++  java
  • Red and Black---POJ

    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
    

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 int n,m;
     7 int ans=0;
     8 int vis[30][30];
     9 char title[30][30];
    10 int fx[4]={-1,0,1,0},fy[4]={0,1,0,-1};
    11 
    12 void dfs(int x,int y){
    13     ans++;
    14     vis[x][y]=1;
    15     for(int i=0; i<4; i++ ){
    16         int nx=x+fx[i];
    17         int ny=y+fy[i];
    18         if(nx>=0&&nx<n&&ny>=0&&ny<m&&title[nx][ny]=='.'&&!vis[nx][ny]){
    19             dfs(nx,ny);
    20         }
    21     }
    22 }
    23 
    24 int main(){
    25     while(~scanf("%d%d",&m,&n)&&n&&m){
    26 //        getchar();
    27         ans=0;
    28         memset(vis,0,sizeof(vis));
    29         for( int i=0; i<n; i++ ){
    30             cin>>title[i];
    31         }
    32         for( int i=0; i<n; i++ ){
    33             for(int j=0; j<m; j++ ){
    34                 if(title[i][j]=='@'&&!vis[i][j]){
    35                     dfs(i,j);
    36                     break;
    37                 }
    38             }
    39         }
    40         printf("%d
    ",ans);
    41     }
    42 
    43     return 0;
    44 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    docker-compose.yml容器网络
    docker-compose搭建redis集群
    docker-compose简易搭建rabbitmq
    docker仓库快速搭建
    CNN是怎样一步步工作的?
    全连接层分类的原理
    vector中erase()与insert()用法
    MySQL安装教程
    红黑树与AVL树比较
    C++内存管理(超长,例子很详细,排版很好)
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10632535.html
Copyright © 2011-2022 走看看