zoukankan      html  css  js  c++  java
  • 杭电1312--Red and Black(Dfs)

    Red and Black

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 13090    Accepted Submission(s): 8116


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

     

    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
     

     

    Recommend
    Eddy   |   We have carefully selected several similar problems for you:  1016 1010 1372 1253 1240 
    //比较基础的搜索题, 但是做了很久。→ → !!
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #define M 30
     5 using namespace std;
     6 
     7 int ans;
     8 int x, y;
     9 char map[M][M];
    10 int dx[4]={0, 0, 1, -1}; 
    11 int dy[4]={1, -1, 0, 0};
    12 int a, b;
    13 void dfs(int x, int y)
    14 {
    15     int m, n;
    16     ans++;
    17     map[x][y] = '@';
    18     //printf("%d %d
    
    
    ", x, y);
    19     for(int i = 0; i < 4; i++) //  
    20     {
    21         m = x + dx[i];
    22         n = y + dy[i];
    23         //printf("%d %d
    ", m, n); 
    24         if(m >= 0 && m < b && n >= 0 && n < a && map[m][n] == '.')   //限制边界。 
    25         {
    26         //    printf("1");
    27         //    printf(" %d %d
    ", m, n);
    28             dfs(m, n);
    29         }
    30     }
    31 }
    32 
    33 int main()
    34 {
    35     int i, j;
    36     while(~scanf("%d %d", &a, &b), a|b)    
    37     {
    38         ans = 0;
    39         for(i=0; i<b; i++)
    40         {
    41             for(j=0; j<a; j++)
    42             {
    43                 cin >> map[i][j];
    44                 if(map[i][j] == '@')
    45                 {
    46                     x = i;
    47                     y = j;
    48                 }
    49             }
    50         }
    51         //printf("%d %d
    
    ", x, y);
    52         dfs(x, y);
    53         printf("%d
    ", ans);
    54     }
    55     return 0;
    56 } 

    注意:

     1 void dfs(int x, int y)
     2 {
     3     int nx, ny;
     4     ant++;
     5     map[x][y] = '@';
     6     for(int i = 0; i < 4; i++)
     7     {
     8         nx = x + acx[i];
     9         ny = y + acy[i];
    10         if(nx >= 0 && nx < m && ny >= 0 && ny < n && map[nx][ny] == '.')
    11         {
    12             dfs(nx, ny);    
    13         }  
    14     }
    15 }
    important
     
  • 相关阅读:
    将Mat类型坐标数据生成pts文件
    DelaunayTriangulation_VoronoiDiagram_using_OpenCV的实现
    安装python第三方库
    安装wordcloud第三方库Unable to find vcvarsall.bat
    Vec3b类型数据确定颜色通道
    使用Inno Setup Compiler制作安装软件包
    QT-This application failed to start because it could not find or load the Qt platform plugin "windows"
    m函数与m文件的命名
    当前目录如何打开cmd
    [Machine Learning & Algorithm] 随机森林(Random Forest)-转载
  • 原文地址:https://www.cnblogs.com/soTired/p/4699829.html
Copyright © 2011-2022 走看看