Poj 1979 red and black

Source: Internet
Author: User
Red and black
Time limit:1000 ms   Memory limit:30000 K
Total submissions:22300   Accepted:12041

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.#..........#.#######..#.#.....#..#.#.###.#..#.#[email protected]#.#..#.#####.#..#.......#..#########............11 6..#..#..#....#..#..#....#..#..###..#..#..#@...#..#..#....#..#..#..7 7..#.#....#.#..###.###[email protected]###.###..#.#....#.#..0 0

Sample output

4559613

Source

Japan 2004 Domestic

Question:
First, this is an image. '.' indicates the black brick, '#' indicates the red brick, and '@' indicates the black brick where the hero stays. Red bricks cannot be taken, but they can only follow the Black bricks. Each time you take a step, ask the maximum number of Black bricks that can be taken!

Golden DFS;
Pay attention to the boundary and recursive end conditions!

Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <queue> using namespace STD; # define maxn 25 # define mm 10000000 char s [maxn] [maxn]; int W [maxn] [maxn]; // mark the passed vertex int Sx, Sy; int conut; // count int n, m; int XX [] = {0, 1,-1}; int YY [] = {1,-1, 0 }; int DFS (int x, int y) {If (s [x] [Y] = '#' | W [x] [Y]) return 0; conut ++; W [x] [Y] = 1; for (INT I = 0; I <4; I ++) {int xi = x + XX [I]; int YI = Y + YY [I]; If (xi> = 0 & xi <n & Yi> = 0 & Yi <m) DFS (XI, Yi) ;}return conut ;}int main () {While (~ Scanf ("% d", & M, & N) {If (M = 0 & n = 0) break; memset (W, 0, sizeof (w); conut = 0; For (INT I = 0; I <n; I ++) {scanf ("% s", s [I]); // input for (Int J = 0; j <m; j ++) if (s [I] [J] = '@') // mark the start point {SX = I; Sy = J ;}} int K = DFS (sx, Sy); printf ("% d \ n", k );} return 0 ;}


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.