Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1312
Red and Black
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 15186 Accepted Submission (s): 9401
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 Output4559613
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <string>5 using namespacestd;6 #defineN 257 CharMp[n][n];8 intHot4][2] = {{1,0},{-1,0},{0,-1},{0,1}};9 Ten intn,m; One BOOLckintXinty) { A if(x<n&&x>=0&&y<m&&y>=0)return true; - Else return false; - } the BOOLVis[n][n]; - voidDfsintXintYint&sum) - { -Vis[x][y] =1; + BOOLFL =0; - for(inti =0; I <4; i++){ + intxx = x + go[i][0]; A intyy = y + go[i][1]; at if(CK (XX,YY) &&!vis[xx][yy]&&mp[xx][yy]=='.'){ -FL =1; -sum = sum+1; - DFS (xx,yy,sum); - } - } in if(fl==0)return; - } to intMain () + { - while(~SCANF ("%d%d",&m,&N)) the { * intx, y; $ if(n==0&&m==0) Break;Panax Notoginseng GetChar (); - for(inti =0; I < n; i++){ the for(intj =0; J < M; J + +){ +scanf"%c",&mp[i][j]); A if(mp[i][j]=='@') {x = i; y =J;} the } + GetChar (); - } $memset (Vis,0,sizeof(Vis)); $ intsum =0; - DFS (x,y,sum); -printf"%d\n", sum+1); the } - return 0;Wuyi}
Red and Black (Dfs water)