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 }
  • 相关阅读:
    CodeBlocks "no such file or directory" 错误解决方案(创建类找不到头文件)
    WCF配置文件与文件下载之坎坷路
    使用Visual Studio 2010打造C语言编译器
    一个小程序引发的思考
    在C#使用文件监控对象FileSystemWatcher 实现数据同步
    C 语言 static、extern与指针函数介绍
    检测端口是否被占用
    C# ini文件读写类
    C学习笔记(2)--指针
    plsql auto 常用语法
  • 原文地址:https://www.cnblogs.com/Annetree/p/5641818.html
Copyright © 2011-2022 走看看