• hdu 1312 Red and Black


    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): 6095    Accepted Submission(s): 3883


    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
     

     dfs模板题 

     1 #include<stdio.h>
     2 #include<string.h>
     3 char map[25][25];
     4 int dir[4][2]={1,0,-1,0,0,1,0,-1};//方向数组
     5 int vis[25][25];//标记数组
     6 int n,m,count;//行,列,计数器
     7 void dfs(int x,int y)//标准的DFS模板
     8 {
     9      int i;
    10      for(i=0;i<4;i++)
    11      {
    12           int xx=x+dir[i][0];
    13           int yy=y+dir[i][1];
    14           if(xx>=0&&xx<n&&yy>=0&&yy<m&&!vis[xx][yy]&&map[xx][yy]!='#')
    15           {
    16                count++;
    17                vis[xx][yy]=1;
    18                dfs(xx,yy);
    19           }
    20      }
    21    //  return ;
    22 }
    23 int main()
    24 {
    25     int i,j;
    26     while(scanf("%d %d",&m,&n)!=EOF)
    27     {
    28          if(m==0||n==0)
    29          break;
    30          memset(map,0,sizeof(map));//因为是多组输入所以每次得清0
    31          memset(vis,0,sizeof(vis));
    32          count=1;
    33          for(i=0;i<n;i++)
    34          scanf("%s",map[i]);
    35          for(i=0;i<n;i++)
    36             for(j=0;j<m;j++)
    37             {
    38                  if(map[i][j]=='@')
    39                  {
    40                       map[i][j]='#';
    41                       vis[i][j]=1;
    42                       dfs(i,j);
    43                  }
    44             }
    45             printf("%d
    ",count);
    46     }
    47      return 0;
    48 }
    View Code
  • 相关阅读:
    Qt 动态添加控件,并删除指定控件
    Qt 采用遍历方式删除QList中全部元素的方法
    Qt 动态添加checkbox,并添加点击事件
    Qt 动态添加删除控件总结
    Qt 动态添加删除控件的方法
    Qt QScrollArea显示滚动条(添加自定义控件)
    Qt Qt::​Alignment 设置对齐方式
    Qt QListView 简单应用
    MATLAB R2019b超详细安装教程
    OpenCV 伪彩 applyColorMap
  • 原文地址:https://www.cnblogs.com/llei1573/p/3196541.html
Copyright © 2020-2023  润新知