2806 red and black blog: doubleq. win, 2806doubleq. win

Source: Internet
Author: User

2806 red and black blog: doubleq. win, 2806doubleq. win
Blog: doubleq. win2806 red and black

 

Time Limit: 1 s space limit: 64000 KB title level: Silver Question View running resultsDescription Description

There is a rectangular room with square tiles covered. Each tile is painted in red or black. A man is standing on a black tile. From this, he can move to one of the four adjacent tiles, but he cannot move to the red brick, he can only move to the black brick. Write a program to calculate the number of black bricks that he can move by repeating the above steps.

 

Input description Input Description

The input contains multiple datasets. The first line of a dataset contains two positive integers W and H. W and H indicate the number of columns and the number of rows in the rectangle room, respectively, and both cannot exceed 20.
Each dataset has H rows, each of which contains W characters. The meaning of each character is as follows:
'.' -- Black brick
'#' -- Red brick
'@' -- Man (each dataset only appears once)
Two zeros indicate that the input is complete.

Output description Output Description

For each dataset, the program should output a line containing the number of tiles that men can reach from the initial tile.

Sample Input Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

Sample output Sample Output

45
59
6
13

Data range and prompt Data Size & Hint

None

 
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 int a [101] [101]; 6 int x, y; 7 int xx [9] = {-1, + 1,0, 0}; 8 int yy [9] = {0,-1, + 1}; 9 int tot; 10 int vis [101] [101]; 11 void dfs (int x, int y) 12 {13 for (int I = 0; I <4; I ++) 14 {15 if (a [x + xx [I] [y + yy [I] = 1 & vis [x + xx [I] [y + yy [I] = 0) 16 {17 vis [x + xx [I] [y + yy [I] = 1; 18 tot ++; 19 dfs (x + xx [I], y + yy [I]); 20} 21} 22} 23 int main () 24 {25 int n, m; 26 while (1) 27 {28 cin> m> n; 29 if (m = 0 & n = 0) 30 break; 31 tot = 0; 32 memset (vis, 0, sizeof (vis); 33 memset (a, 0, sizeof (a); 34 for (int I = 1; I <= n; I ++) 35 {36 for (int j = 1; j <= m; j ++) 37 {38 char p; 39 cin> p; 40 if (p = '@') 41 {42 x = I; 43 y = j; // initial position 44} 45 else if (p = '. ') 46 a [I] [j] = 1; // The black brick can walk 47 else if (p =' # ') 48 a [I] [j] = 0; // red bricks cannot walk 49 else continue; 50} 51} 52 dfs (x, y); 53 cout <tot + 1 <endl; 54} 55 return 0; 56}

Simplest and most violent search

Related Article

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.