• Red and Black(dfs水)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

    Red and Black

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


    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
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<string>
     5 using namespace std;
     6 #define N 25
     7 char mp[N][N];
     8 int go[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};
     9 
    10 int n,m;
    11 bool ck(int x, int y){
    12     if(x<n&&x>=0&&y<m&&y>=0) return true;
    13     else return false;
    14 }
    15 bool vis[N][N];
    16 void dfs(int x, int y, int &sum)
    17 {
    18     vis[x][y] = 1;
    19     bool fl = 0;
    20     for(int i = 0; i < 4; i++){
    21         int xx = x + go[i][0];
    22         int yy = y + go[i][1];
    23         if(ck(xx,yy)&&!vis[xx][yy]&&mp[xx][yy]=='.'){
    24             fl = 1;
    25             sum = sum+1;
    26             dfs(xx,yy,sum);
    27         }
    28     }
    29     if(fl==0) return;
    30 }
    31 int main()
    32 {
    33     while(~scanf("%d%d",&m,&n))
    34     {
    35         int x, y;
    36         if(n==0&&m==0) break;
    37         getchar();
    38         for(int i = 0; i < n; i++){
    39             for(int j = 0; j < m; j++){
    40                 scanf("%c",&mp[i][j]);
    41                 if(mp[i][j]=='@'){ x = i; y = j; }
    42             }
    43             getchar();
    44         }
    45         memset(vis,0,sizeof(vis));
    46         int sum = 0;
    47         dfs(x,y,sum);
    48         printf("%d
    ",sum+1);
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    ReSharper License Server
    QGEditors.WinForms WinForms下使用的部分扩展控件
    .net 项目生成时自动更新版本号
    IntelliJ运行下载的Servlet时报错 Error running Tomcat 8.5.8: Unable to open debugger port (127.0.0.1:49551): java.net.SocketException
    在Mac系统中安装及配置Apache Tomcat
    继承
    对象和封装
    事务、视图、索引、备份、还原
    高级查询
    影院售票系统
  • 原文地址:https://www.cnblogs.com/shanyr/p/5207020.html
Copyright © 2020-2023  润新知