HDU 1312 Red and Black "Deep Search"

Source: Internet
Author: User

Red and Black

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 11531 Accepted Submission (s): 7177


Problem Description

There is a rectangular and covered with square tiles. Each tile is colored either red or black. A man was standing on a black tile. From a tiles, he can move to one of the four adjacent tiles. But he can ' t move on red tiles, and 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 the positive integers W and H; W and H is the numbers of tiles in the X-and y-directions, respectively. W and H is not more than 20.

There is H more lines in the data set, and each of the 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) 


Output

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


Sample Output

4559613


Source

Asia 2004, Ehime (Japan), Japan Domestic


#include <stdio.h> #include <string.h>int n,m,cnt;char map[30][30];int to[4][2] = {{1,0},{0,1},{-1,0},{0,- 1}};void DFS (int i,int j) {int k,x,y;     cnt++;    MAP[I][J] = ' # ';    for (k = 0; k<4; k++)    {        x = i + to[k][0];        y = j + to[k][1];        if (x<n && y<m && x>=0 && y>=0 && map[x][y] = = '. ') Control x, y do not let it cross the             dfs (x, y);    }} int main () {    int i,j,fi,fj;    while (scanf ("%d%d%", &m,&n))//horizontal is n, longitudinal is m     {    if (m==0&&n==0) break;        for (i = 0; i<n; i++)        {            for (j = 0; j<m; j + +)            {                scanf ("%c", &map[i][j]);                if (map[i][j] = = ' @ ')//Find the location of the first person                 {                    fi = i;                    FJ = j;                }            }            GetChar ();        }        CNT = 0;        DFS (FI,FJ);        printf ("%d\n", CNT);    }    return 0;}


HDU 1312 Red and Black "Deep Search"

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.