zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Java单例模式深入详解
    深入理解Java的接口和抽象类
    java中为什么要给类使用代理?它有哪些好处?
    Log4j--java日志
    Log4j rootLogger配置
    selenium之 下拉选择框Select
    关于弹框
    spring controller接口中,用pojo对象接收页面传递的参数,发现spring在对pojo对象赋值时,有一定顺序的问题
    navicat mysql报错误:2013 Lost connection to MySQL server during query
    记录一次mysql导入千万条测试数据过慢的问题!
  • 原文地址:https://www.cnblogs.com/Annetree/p/5641818.html
Copyright © 2011-2022 走看看