• HDU 1312 Red and Black (DFS)


      

    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
    要小心它的输入
    行列是相反的
    坑在这里找了好多次TAT
     1 #include<stdio.h>
     2 #include<iostream>
     3 using namespace std;
     4 char map[25][25];
     5 int n,m;
     6 int num;
     7 int rx[]={0,0,1,-1};
     8 int ry[]={1,-1,0,0};
     9 void dfs(int i,int j)
    10 {
    11     map[i][j]='#';
    12     num++;
    13     int t,x,y;
    14     for(t=0;t<4;t++)
    15     {
    16         x=i+rx[t];
    17         y=j+ry[t];
    18         if(x>=0 && x<n && y>=0 && y<m && map[x][y]=='.')
    19             dfs(x,y);    
    20     }
    21 }
    22 int main()
    23 {
    24     int i,j;
    25     int si,sj;
    26     while(scanf("%d%d",&m,&n)!=EOF)//原来是行数和列数反了...我真是醉了....
    27     {
    28         
    29         if(m==0&&n==0)break;
    30         for(i=0;i<n;i++)
    31             for(j=0;j<m;j++)
    32             {
    33                 cin>>map[i][j];
    34                 if(map[i][j]=='@')
    35                 {
    36                     si=i;
    37                     sj=j;
    38                 }
    39             }
    40         num=0;
    41         dfs(si,sj);
    42         printf("%d
    ",num);
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    linux定时器的使用
    6410 spi总线
    6410 spi 设备驱动
    qtcreator 安装后不能显示菜单
    基于Tiny 6410的内核移植 (NAND FLASH 、UBIFS篇)
    有关ajax跨域问题
    sqlmap的安装
    C# 连接数据库的配置方法
    初识Markdown
    ASP.NET 使用Ueditor富文本编辑器
  • 原文地址:https://www.cnblogs.com/Annetree/p/5641818.html
Copyright © 2020-2023  润新知