POJ 1979 Red and Black (Red and Black)
Original
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)
The end of the input is indicated by a line consisting of two zeros.
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.#..........#.#######..#.#.....#..#.#.###.#..#.#..@#.#..#.#####.#..#.......#..#########............11 6..#..#..#....#..#..#....#..#..###..#..#..#@...#..#..#....#..#..#..7 7..#.#....#.#..###.###...@...###.###..#.#....#.#..0 0
Sample Output
4559613
Analysis
The questions have the following requirements:
Only four adjacent points can be taken. Only black points can be taken.
You need to calculate the sum of the Black points that can be reached.
Because "." indicates a Black Point, you must determine that the current point is a Black Point in the dfs function to perform the next search.
And follows the direc two-dimensional array defined in the Code below.
The specific direction and other information are all listed in.
Code
# Include
Using namespace std; // The maximum width and height given in the question # define MAX_W 20 # define MAX_H 20 // the width and height to be input and the number of steps that have been taken int W, H; int step = 0; // the two-dimensional array to be written. char room [MAX_W] [MAX_H]; // clockwise direction const int direc [4] [2] = {0,-1}, {}, {0, 1}, {-1, 0 },}; int dfs (const int & row, const int & col) {// point room [row] [col] = '#'; // calculate the number of steps + + step; for (int d = 0; d <4; ++ d) {int curRow = row + direc [d] [1]; int curCol = col + direc [d] [0]; if (curRow> = 0 & curRow <H & curCol> = 0 & curCol <W & room [curRow] [curCol] = '. ') {dfs (curRow, curCol) ;}} return step;} int main () {bool found; while (cin >>w >> H, W> 0) {step = 0; int col, row; // enter for (row = 0; row <H; ++ row) {for (col = 0; col <W; ++ col) {cin> room [row] [col] ;}} found = false; // locate the start point for (row = 0; row <H; ++ row) {for (col = 0; col <W; ++ col) {if (room [row] [col] = '@') {found = true; break ;}} if (found) {break ;}/// start searching for cout <dfs (row, col) <endl ;}}
No.
Request voting or forwarding support ...... Hope I don't want to die so badly ......
Click here to vote
The voting continued from the 10th to the 20th. Thank you!
------------------- Of course, you can click the image directly.