Red and Black (Simple Dfs)

Source: Internet
Author: User

Red and Black

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


Problem Descriptionthere 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.

Inputthe 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)

Outputfor each data set, your program should output a line which contains the number of tiles he can reach from the Initia L tile (including itself).

Sample Input6 9 ..... .....# ...... ...... ...... ...... ...... #@...# .#.. #. 11 9. # ..... #.#######. .#.#.....#. .#.#.###.#. . #.#[email protected]#.#. .#.#####.#. .#.......#. .#########. ........... 11 6.. #.. #.. #.. .. #.. #.. #.. .. #.. #.. ### .. #.. #.. #@. .. #.. #.. #.. .. #.. #.. #.. 7 7.. #.#.. .. #.#.. ###.### [email protected] ###.###. #.#.. .. #.#.. 0 0

Sample OUTPUT45 59 6 13 Simple DFS, no pruning; code:#include <stdio.h>
CharMap [ + ][ + ];
int
Dir [4 ][2 ]={1 ,0 ,-1 ,0 ,0 ,1 ,0 ,-1 },Step ,W ,H ;
void
Dfs ( intX , intY ){
int
Tx ,Ty ;
Step ++;
for
( intI =0 ;I <4 ;++I ){
Tx =X +Dir [I ][0 ];Ty =Y +Dir [I ][1 ];
If
(Tx >=W ||Ty >=H ||Tx <0 ||Ty <0 ||Map [Ty ][Tx ]==' # ' ||Map [Ty ][Tx ]==' @ ' ) Continue ;
Map [Ty ][Tx ]=' @ ' ;
Dfs (Tx ,Ty );
}
}

int
Main (){ intX ,Y ;
while
(scanf ("%d%d" ,&W ,&H ),W ||H ){Step =0 ;
for
( intI =0 ;I <H ;++I )scanf ('%s ' ,Map [I ]);
for
( intI =0 ;I <H ;++I ) for ( intJ =0 ;J <W ;++J ) if (Map [I ][J ]==' @ ' )X =J ,Y =I ;
Dfs (X ,Y );
Printf ("%d\n" ,Step );
}

return
0 ;
}

Red and Black (Simple Dfs)

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.