Red and Black Deep Search

Source: Internet
Author: User

 B-Red and Black crawling in process ... crawling failedTime limit:MS Memory Limit:30000KB 64bit IO Format: %I64D &%i64u SubmitStatus

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)
The end of the input is indicated by a line consisting of the zeros.

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
Test instructions: It's just a matter of how many steps you can take to get where you are.
Idea: Find the place to do a deep search there are four directions, no pruning
AC Code:
#include <iostream> #include <cstdio>using namespace std;
Char map[20][20];int b[4]={0,1,-1,0};int bb[4]={1,0,0,-1};int r,c;int sx,sy;int tol;int ok (int x,int y) {  & nbsp if (x<0| | y<0| | x>=r| | y>=c| | map[x][y]== ' # ') return 0;    return 1; void Dfs (int x,int y) {    for (int i=0;i<4;i++) {        map[x][y ]= ' # ';        if (ok (X+b[i],y+bb[i])) {             tol++;            DFS (x+b[ I],y+bb[i]);       }   }}int Main () {    while ( scanf ("%d%d", &c,&r)!=eof&&r!=0&&c!=0) {        GetChar ();        for (int i=0;i<r;++i) {                 for (int j=0;j<c;j++) {                    scanf ("%c", &map[i][j]);// Remember here gets () is not possible, may enter the space                     if (map[i][j]== ' @ ') {                     sx=i;                     sy=j;                   }                }        if (i<r-1) { GetChar ();}
} tol=1;        DFS (SX,SY);    printf ("%d\n", tol); } return 0;}
This has been done before, see or find a little strange, long time do not knock on the forgotten how to do, and strengthen.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.