zoukankan      html  css  js  c++  java
  • Red and Black(DFS入门题)

    Red and Black

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

    原题链接:点击打开链接


    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
     
    Recommend
    Eddy
     
     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <vector>
     8 #define LL long long
     9 #define MAXI 2147483647
    10 #define MAXL 9223372036854775807
    11 #define eps (1e-8)
    12 #define dg(i) cout << "*" << i << endl;
    13 using namespace std;
    14 
    15 int w, h, sx, sy, cnt;
    16 char map[22][22];
    17 char d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    18 
    19 void DFS(int i, int j)
    20 {
    21     int ii, jj, k;
    22     for(k = 0; k < 4; k++)
    23     {
    24         ii = i + d[k][0];
    25         jj = j + d[k][1];
    26         if(ii > -1 && jj > -1 && ii < h && jj < w && map[ii][jj] == '.')
    27         {
    28             cnt++;
    29             map[ii][jj] = '#';
    30             DFS(ii, jj);
    31         }
    32     }
    33 }
    34 
    35 int main()
    36 {
    37     int i, j;
    38     while(scanf("%d %d", &w, &h) && w)
    39     {
    40         cnt = 1;
    41         for(i = 0; i < h; i++)
    42         {
    43             scanf("%s", map[i]);
    44         }
    45         for(i = 0; i < h; i++)
    46         {
    47             for(j = 0; j < w; j++)
    48                 if(map[i][j] == '@')
    49                 {
    50                     sx = j;
    51                     sy = i;
    52                     i = h;
    53                     break;
    54                 }
    55         }
    56         DFS(sy, sx);
    57         printf("%d\n", cnt);
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    关于JS动态切换样式表
    关于header()函数重定向的问题
    微信团队讲课笔记 Android 开发(二)UI设计
    Effective C++ 笔记:4设计与声明
    某面试算法题_最短时间找出十包粉末中的两蓝粉末。
    VS2015 配置opengl的一些库
    URAL 1225 Flags 简单DP,一重循环
    POJ 1384 Piggy-Bank 完全背包分析
    POJ 1651 Multiplication Puzzle DP 类似矩阵链
    URAL 1183 Brackets Sequence DP 路径输出
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910461.html
Copyright © 2011-2022 走看看