忘记具体是哪个题目的代码了贴出来当作模版记录一下
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int arr[25][25];
char vis[25][25];
int num;
int H, L;
struct node {
int x;
int y;
};
int fx[] = { 1,0,0,-1 };
int fy[] = { 0,1,-1,0 };
bool pan(node temp) {
return (temp.x >= 0 && temp.x < H&& temp.y >= 0 && temp.y < L&& arr[temp.x][temp.y] == 0 && vis[temp.x][temp.y] == '.');
}
void BFS(int x, int y) {
queue<node>Q;
Q.push({ x, y });
while(!Q.empty()) {
node A(Q.front());
Q.pop();
for (int i = 0; i < 4; ++i) {
node temp;
temp.x = A.x + fx[i];
temp.y = A.y + fy[i];
if (pan(temp)) {
arr[temp.x][temp.y] = 1;
++num;
Q.push(temp);
}
}
}
}
int main() {
while (cin >> L >> H, H, L) {
num = 0;
memset(arr, 0, sizeof(arr));
for (int i = 0; i < H; ++i) {
for (int j = 0; j < L; ++j) {
cin >> vis[i][j];
}
}
for (int i = 0; i < H; ++i) {
for (int j = 0; j < L; ++j) {
if (vis[i][j] == '@') {
++num;
BFS(i, j);
}
}
}
cout << num << endl;
}
return 0;
}