标签:搜索 dep string ++ dfs int deposit pac algorithm
HDU - 1241 链接
连通块个数统计问题
选对一块油田八个方向进行dfs。
dfs的次数就是连通块的个数了,极度水题
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int ax[4] = {1, -1, 0, 0};
const int ay[4] = {0, 0, 1, -1};
const int N = 110;
int maze[N][N], cnt, maxn, n, m;
bool judge(int x, int y) {
if(x >= 0 && x < n && y >= 0 && y < m && !maze[x][y])
return true;
return false;
}
void dfs(int x, int y) {
for(int i = -1; i <= 1; i++)//八个方向进行搜索
for(int j = -1; j <= 1; j++) {
int tempx = x + i;
int tempy = y + j;
if(judge(tempx, tempy)) {
maze[tempx][tempy] = 1;
dfs(tempx, tempy);
}
}
}
int main() {
// freopen("in.txt", "r", stdin);
char c;
while(cin >> n >> m && n) {
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
cin >> c;
if(c == ‘@‘) maze[i][j] = 0;
else maze[i][j] = 1;
}
cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
if(maze[i][j] == 0) {
maze[i][j] = 1;
dfs(i, j);
cnt++;
}
}
cout << cnt << endl;
}
return 0;
}
标签:搜索 dep string ++ dfs int deposit pac algorithm
原文地址:https://www.cnblogs.com/lifehappy/p/12604443.html