Simple search Red and Black:

Simple search Red and Black:

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.

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)

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

Note: My English is not good on their own translation;

 

 

just kidding.

Subject description:

There is a rectangular room, covered with tiles above. Each tile is red or black. A man standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can not move on red tiles, can only move on the black tiles.

 

Write a program to calculate the number of black tiles he can reach by repeating the above action.

 

A plurality of data input sets composition. Data set to include two positive integers beginning of the line W and H; W and H are the number of tiles in the x and y directions. W and H does not exceed 20.

 

There dataset H lines each containing W characters. Each tile represents a character color, as shown below.

 

'.'- piece of black tile

"35;" - red tile

"@" - a person standing on a black tile (appears only once in the dataset)

 

For each data set, your program should output a row containing the tile he can count initial tile (including itself) arriving from.

Man of few words said on the code:

#include <iostream>
#include <CString> // I can not be heard with a universal head?
using namespace std; // need to explain this
char f [501] [501] ; // save tiles "map"
int ANS, In Flag;
int n-= -1, m = -1; // just defined variables
void dfs ( int x, int y) // core
{
IF (X <0 || Y <0 || X> n-|| Y> m) // there is determined bounds
return; // bye bounds on the
f [x] [ y] = '*'; // not go through the sub-blocks
if (f [x] [y + 1] == '.') ans ++, dfs (x, y + 1); // see if can not walk, can stay away step; (upward)
IF ( '.' F [X] [Y-. 1] ==) ANS ++, DFS (X, Y-. 1); // can go to see, can go go one step; (downward)
IF ( '.' F [. 1 + X] [Y] ==) ANS ++, DFS (. 1 + X, Y); // can look down, to stay away step; (right)
( '.' F [. 1-X] [Y] ==) IF ANS ++, DFS (. 1-X, Y); // can look down, to stay away step; (left)
}
int main ()
{
the while (CIN >> >> n-m) as a plurality of sets of data //
{
Memset (F, 0, the sizeof (F)); // initialize the array F
ans = 1,
if (n == 0 && m == 0) return 0; // Do not ask me why, look at the title!
for (int i = 1; i <= n; i ++) // for loop
{
for (int. 1 = J; J <= m; J ++) // for two cycles
{
CIN >> F [I] [J ]; // read map
}
}
for (int I =. 1; I <= n-; I ++)
{
for (int J =. 1; J <= m; J ++) // loop about find the location of the man
{
IF ( f [i] [j] == '@') // determines whether the above
{
DFS (I, J); // walk, live to 99!
flag = 1; // ready to exit;
BREAK; // first exit;
}
}
IF (In Flag ==. 1) BREAK; // second exit.
}
COUT ANS << << endl; // output.
}
Return 0; // perfect
}

Guess you like

Origin www.cnblogs.com/WWWZZZQQQ/p/12042247.html