题目链接:P1506 拯救oibh总部
程序说明:
深度优先搜索,和P1162填涂颜色差不多(没有区别)。
大佬们的题解: P1506 拯救oibh总部 题解
代码如下:
#include <iostream>
#define MAX 520
using namespace std;
int a[MAX][MAX];
int n, m, cnt = 0;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
void dfs(int x, int y) {
if(x < 0 || y < 0 || x > n + 1 || y > m + 1 || a[x][y] == 1)
return;
a[x][y] = 1;
for(int i = 0; i < 4; i++) {
dfs(x + dx[i], y + dy[i]);
}
}
int main() {
cin>>n>>m;
char ch;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
cin>>ch;
if(ch == '*')
a[i][j] = 1;
else
a[i][j] = 0;
}
dfs(0, 0);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
if(a[i][j] == 0)
cnt++;
}
cout<<cnt;
return 0;
}