Red and Black
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total submissions: 41057 |
|
accepted: 22269 |
Description There is a rectangular room, covered with square tiles. Each tile are colored either red or black. A mans is standing on a black tile. From a tile, he can move to one of the four adjacent tiles. But he can's ' t move on red tiles, and he can move in black tiles.
Write a program to count the number of black tiles which and can reach by repeating the moves described above.
Input the 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, respectively. W and H are not more than 20.
There are H lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'. '-a black tile
' # '-A red tile
' @ '-a mans on a black tile (appears exactly once in a data set)
The end of the "input is indicated by a" consisting of two zeros.
Output for each data set, your program should output a-line which contains the number of tiles to the the Initi Al tile (including itself).
Sample Input
6 9
..... .....#
......
......
......
......
......
#@...#
.#.. #.
One 9. # .....
#.#######.
. #.#.....#.
. #.#.###.#.
. #.#.. @#.#.
. #.#####.#.
. #.......#.
. #########.
...........
One 6
. #.. #.. #..
.. #.. #.. #..
.. #.. #.. ###
.. #.. #.. #@.
.. #.. #.. #..
.. #.. #.. #..
7 7
. #.#..
.. #.#..
###.###
...@...
###.###
.. #.#..
.. #.#..
0 0
Sample Output
6
13
The general meaning of the topic is that a warrior embarks from a certain point (@), can only walk the adjacent Black lattice (#), but not the red lattice (.)
Code:
Main.cpp//dfs--red and Black. 1////Created by Showlo on 2018/4/19. COPYRIGHT©2018 year Showlo.
All rights reserved.
#include <stdio.h> #include <algorithm> using namespace std;
#define Max Char Map[max][max];
int m,n,sx,sy;
int num;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
void Dfs () void Dfs (int x,int y) {int i,nx,ny; if (x<0| | x>=m| | y<0| |
Y>=n) {return;
for (i=0; i<4; i++) {nx=x+dx[i];
Ny=y+dy[i];
printf ("%d%d\n", nx,ny); if (map[nx][ny]== '. ')
{num++;
map[nx][ny]= ' # ';
DFS (NX, NY);
} return;
int main () {int i,j; while (scanf ("%d%d", &n,&m)!=eof) {if (m==0| |
n==0) {break;
} memset (map, 0, sizeof (map));
num=0;
For (i=0 i<m; i++) {scanf ("%s", Map[i]); For (i=0. i<m; i++) {for (j=0; j<n; J + +) {IF (map[i][j]== ' @ ') {sx=i;
Sy=j;
}}//printf ("%d%d\n", sx,sy);
Num=1;
map[sx][sy]= ' # ';
DFS (SX,SY);
printf ("%d\n", num);
return 0;
}