• 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 }
    有些目标看似很遥远,但只要付出足够多的努力,这一切总有可能实现!
  • 相关阅读:
    ios8 xcode6 下的启动界面设置和图标设置
    写一些有关android的东西吧,那时候玩android时候的一些笔记
    使用sqlite3 有关tableview删除cell的问题
    iOS 使用CLGeocoder获取地理位置
    ios 解决有关火星坐标的问题
    iOS 火星坐标相关整理及解决方案汇总(转)
    IOS开发UI篇之──自定义UIActionSheet
    python中新式类和经典类的区别
    python中几个常用的算术函数
    多线程
  • 原文地址:https://www.cnblogs.com/Bravewtz/p/10632535.html
Copyright © 2020-2023  润新知