There 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.
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 more 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 man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample
Input
6 9
…#.
…#
…
…
…
…
…
#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.
…
11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Output
45
59
6
13
题目大意:这有一间铺满方形瓷砖的长方形豪宅。 由于hjq学长只喜欢黑色和红色,所以每块瓷砖的颜色是红色或者黑色。 hjq学长站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块。 但是他只能移动到黑色瓷砖上,而不能移动到红色瓷砖上。 编写一个程序,通过重复上述动作来计算hjq学长可以达到的黑色瓷砖的数量。
输入包含多组数据。 每组数据包含两个正整数W和H; H表示瓷砖的行数,W表示瓷砖的列数。 W和H不超过20。
当W、H均为0时结束询问。
瓷砖的颜色用字符表示,如下所示。
‘.’ 表示一个黑色瓷砖
‘#’ 表示一个红色瓷砖
‘@’ 表示hjq学长初始站在的瓷砖
对于每组数据,你的程序应输出一行,其中包含hjq学长可以到达的黑色瓷砖数目。(初始学长所站在的黑色瓷砖也要包含在内)
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
int w,h;
char a[50][50];
int z[4][2]{{1,0},{0,1},{-1,0},{0,-1}};
int all=0;
int fx,fy;
void bfs(int x,int y){
queue<pair<int,int> >q;
q.push({x,y});
while(!q.empty()){
if(q.front().x<0||q.front().x>=h||q.front().y<0||q.front().y>=w||a[q.front().x][q.front().y]=='#'){q.pop();
continue;}
a[q.front().x][q.front().y]='#';
all++;
for(int i=0;i<4;i++)
q.push({q.front().x+z[i][0],q.front().y+z[i][1]});
q.pop();
}
}
int main(void){
while(cin>>w>>h&&w!=0&&h!=0){
for(int i=0;i<h;i++){
getchar();
for(int j=0;j<w;j++) {
scanf("%c", &a[i][j]);
if (a[i][j] == '@')fx = i, fy = j;
}
}
all=0;
bfs(fx,fy);
cout<<all<<endl;
}
return 0;
}
典型BFS题目,但DFS也可以
优化程序
#include <bits/stdc++.h>
#define CHECK(x,y) (x<0||x>=h||y<0||y>=w)
#define x first
#define y second
using namespace std;
int w,h,fx,fy,all=0;
char a[50][50];
int z[4][2]{{1,0},{0,1},{-1,0},{0,-1}};
void bfs(int x,int y){
queue<pair<int,int> >q;
q.push({x,y});
while(!q.empty()){
if(CHECK(q.front().x,q.front().y)||a[q.front().x][q.front().y]=='#'){q.pop();
continue;}
a[q.front().x][q.front().y]='#';
all++;
for(int i=0;i<4;i++)
q.push({q.front().x+z[i][0],q.front().y+z[i][1]});
q.pop();
}
}
int main(void){
while(cin>>w>>h&&w!=0&&h!=0){
for(int i=0;i<h;i++){
for(int j=0;j<w;j++) {
cin>>a[i][j];
if (a[i][j] == '@')fx = i, fy = j;
}
}
all=0;
bfs(fx,fy);
cout<<all<<endl;
}
return 0;
}
DFS
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
int w,h,fx,fy,all=1;
char a[50][50];
int z[4][2]{{1,0},{0,1},{-1,0},{0,-1}};
inline bool check(int x,int y){return (x>=0&&x<h&&y>=0&&y<w&&a[x][y]!='#');}
void dfs(int x,int y){
a[x][y]='#';
for(int i=0;i<4;i++)
{
if(check(x+z[i][0],y+z[i][1])&&++all)
dfs(x+z[i][0],y+z[i][1]);
}
}
int main(void){
while(cin>>w>>h&&w!=0&&h!=0){
for(int i=0;i<h;i++){
for(int j=0;j<w;j++) {
cin>>a[i][j];
if (a[i][j] == '@')fx = i, fy = j;
}
}
all=1;
dfs(fx,fy);
cout<<all<<endl;
}
return 0;
}