当前位置:网站首页>-red and black-

-red and black-

2022-08-10 01:47:00 -JMY-

Title description

There is a rectangular house with square tiles in red and black.You stand on one of the black tiles and can only move up, down, left, and right adjacent black tiles.Write a program that counts how many black tiles you can reach in total.

Enter

The first line is two integers W and H, representing the number of tiles in the x and y directions, respectively.Neither W nor H exceeds 20.In the next H lines, each line includes W characters.Each character represents the color of a tile, the rules are as follows
1) '.': black tile;
2) '#': white tile;
3) '@': black tile,And you stand on this tile.This character occurs only once in each dataset.

Output

Output a line showing the number of tiles you can reach from the initial position (including the initial position when counting).

Sample input

6 9 …#......#..............................#@...#.#..#.

Sample output

45

Reference code:

#include
using namespace std;
int w,h,l;
char s[105][105];
void around(int i,int j){
if(s[i][j]=='#'||i<1||i>w||j<1||j>h)
return;
s[i][j]='#';
l++;
around(i,j-1);
around(i-1,j);
around(i,j+1);
around(i+1,j);
return;
}
int main(){
cin>>h>>w;
for(int i=1;i<=w;i++)
for(int j=1;j<=h;j++)
cin>>s[i][j];
for(int i=1;i<=w;i++)
for(int j=1;j<=h;j++)
if(s[i][j]=='@'){
around(i,j);
cout< return 0;
}
return 0;
}

原网站

版权声明
本文为[-JMY-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092346037057.html