Hdu1312 -- red and black

Source: Internet
Author: User

Red and black

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 ctions, respectively. W and H are not more than 20.
There are h more lines in the data set, each of which between des 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 shocould output a line which contains the number of tiles he can reach from the initial tile (including itself ).
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
. #. # [Email protected] #. #.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7
..#.#..
..#.#..
###.###
[Email protected]
###.###
..#.#..
..#.#..
0 0
Sample output
45
59
6
13

Question:

A tile map ,'. 'represents a black tile,' # 'represents a Red Tile,' # 'represents the position of the hero's station, and the hero will only move up or down in four ways, and only black tiles can be removed (the initial position is also black tiles );

Calculate the number of detachable black tiles, including the initial position.

Ideas:

In simple DFS, search for all the black tiles connected to the initial position, and record the output.

Code;

1 # include <iostream> 2 # include <string> 3 # include <cstdio> 4 # define maxn 50 5 using namespace STD; 6 bool vis [maxn + 10] [maxn + 10], is_black [maxn + 10] [maxn + 10]; // black tile flag 7 char tile [maxn + 10] [maxn + 10]; 8 int n, m; 9 int DFS (int I, Int J) 10 {11 12 if (is_black [I] [J] = 0 | Vis [I] [J] = 1) return 0; // if you encounter a searched or Red Tile during the search, the system returns the result without recording the number of tiles. 13 vis [I] [J] = 1; 14 int sum = 1; 15 if (I-1> = 1) sum + = DFS (I-1, J ); // search for top, bottom, left, and right conditions 16 if (I + 1 <= N) sum + = DFS (I + 1, J); 17 if (J-1> = 1) sum + = DFS (I, J-1); 18 if (J + 1 <= m) sum + = DFS (I, j + 1); 19 Return sum; 20} 21 int main () 22 {23 int first_ I, first_j; 24 while (CIN> m> N) 25 {26 if (M = 0 & n = 0) break; 27 memset (is_black, 0, sizeof (is_black); 28 memset (VIS, 0, sizeof (VIS); 29 getchar (); 30 for (INT I = 1; I <= N; I ++) 31 {32 for (Int J = 1; J <= m; j ++) 33 {34 CIN> tile [I] [J]; 35 if (tile [I] [J] = '@') first_ I = I, first_j = J, tile [I] [J] = '. '; // record the initial position to call DFS and convert the initial position to a black tile using the question (it seems unnecessary --!) 36 IF (tile [I] [J] = '#') is_black [I] [J] = 0; // use the is_black array to mark the tile color 37 Else is_black [I] [J] = 1; 38} 39 getchar (); 40} 41 42 printf ("% d \ n ", DFS (first_ I, first_j); 43} 44 return 0; 45}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.