2806 Red and black
time limit: 1 sspace limit: 64000 KBtitle level: Silver SolvingView Run ResultsTitle Description
Description
There is a rectangular room that covers square tiles. Each tile is painted in red or black. A man standing on a black tile, starting from here, can move to one of four adjacent tiles, but he cannot move to the red brick and only move to the black brick. Write a program that calculates the number of black bricks he can pass by repeating the above movement.
Enter a description
Input Description
The input contains more than one data set. The beginning row of a dataset contains two positive integers w and h,w and H, respectively, representing the number of columns and rows of the rectangular room, and none more than 20.
Each dataset has an H row, where each row contains a W character. The meanings of each character are as follows:
‘.‘ --Black Brick
' # '--red brick
' @ '--men (only one occurrence per dataset)
Two 0 indicates the end of the input.
Output description
Output Description
For each dataset, the program should output a line that contains the number of tiles the man can reach from the initial tile.
Sample input
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#.. #.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
. #.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
.. #.. #.. #..
.. #.. #.. #..
.. #.. #.. ###
.. #.. #.. #@.
.. #.. #.. #..
.. #.. #.. #..
7 7
.. #.#..
.. #.#..
###.###
[Email protected]
###.###
.. #.#..
.. #.#..
0 0
Sample output
Sample Output
45
59
6
13
Data range and Tips
Data Size & Hint
No
#include <cstdio> #include <cstring> #include <iostream>using namespace Std;int W,h,ans,x,y;char a[30 ][30];int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0},f[30][30];int dfs (int x,int y) {int xx,yy;ans++;f[x][y]=1;for (int i=0;i <4;i++) { xx=x+dx[i]; Yy=y+dy[i]; if ((xx>0) && (xx<=h) && (yy>0) && (yy<=w) && (a[xx][yy]== '. ') && (!f[xx][yy])//Super See whether the cross-border, whether it is black brick, whether to walk through Dfs (XX,YY); }} int main () {scanf ("%d%d", &w,&h), while (w&&h) { ans=0; memset (F,0,sizeof (f)); for (int i=1;i<=h;i++) for (int j=1;j<=w;j++) { cin>>a[i][j]; if (a[i][j]== ' @ ') { x=i;//be careful not to represent the x-axis, y for the horizontal axis, do not confuse!!! y=j; A[i][j]= '. '; } } DFS (x, y); printf ("%d\n", ans); scanf ("%d%d", &w,&h); }}
Codevs 2806 Red and Black