Hangdian 1312 red and black

Source: Internet
Author: User
Red and black
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 8911 accepted submission (s): 5535


Problem descriptionthere 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.
 
Inputthe 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)
 
Outputfor 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 .. #. #.... #. #.. ###. ### [email protected] ###. ###.. #. #.... #. #.. 0 0 in this question, @ is also considered to be a allowed location, so the answer should be added with 1. When searching, pay attention to marking the location of the vertices that have passed so as to avoid repeated computation and boundary.
# Include <iostream> # include <cstring> using namespace STD; int ans, n, m; int dy [4] = {0, 1, 0,-1 }; int DX [4] = {1005,-1005}; char a [1005] [1005], B [] []; void DFS (INT X, int y) {int XX, YY, I; for (I = 0; I <4; I ++) {xx = x + dx [I]; // note that X + = d [x] cannot be used directly here; otherwise, the initial value YY = Y + dy [I] of X will not be returned during backtracking; if (A [XX] [YY] = '. '& XX> = 0 & XX <M & YY> = 0 & YY <n) {ans ++; A [XX] [YY] = '#'; DFS (XX, YY) ;}} int main () {int I, j; memset (B, 0, sizeof (B); While (CIN> N> M, M & N) {ans = 0; for (I = 0; I <m; I ++) for (j = 0; j <n; j ++) CIN> A [I] [J]; for (I = 0; I <m; I ++) for (j = 0; j <n; j ++) if (a [I] [J] = '@') {A [I] [J] = '#'; DFS (I, j) ;}/ * cout <Endl; for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) cout <A [I] [J]; cout <Endl ;} */cout <ans + 1 <Endl;} 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.