poj2386(简单dfs)

就是求图中有多少个水洼。对图进行dfs遍历,并把是水洼的地方全部标记。然后从下一个是水哇的地方再进行dfs。

 #include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int graph[][];
int sum;
bool vis[][];
int dx[] = {-, -, -, , , , , };
int dy[] = {-, , , -, , -, ,};
int row, col; bool check(int x, int y)
{
if( x >= && x < row && y >= && y < col)
return true;
return false;
} void dfs(int x, int y)
{
int ix, iy;
for(int i = ; i < ; i++)
{
ix = x + dx[i];
iy = y + dy[i];
if(!vis[ix][iy] && check(ix, iy) && graph[ix][iy] == )
{
vis[ix][iy] = true;
dfs(ix, iy);
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
cin >> row >> col;
getchar();
memset(vis, false, sizeof(vis)); char c;
for(int i = ; i < row; i ++)
{
for(int j = ; j < col; j++)
{
c = getchar();
graph[i][j] = (c == '.') ? : ;
}
getchar();
}
sum = ; for(int i = ; i < row; i ++)
{
for(int j = ; j < col; j++)
{
if(graph[i][j] == && vis[i][j] == false)
{
sum++;
vis[i][j] = true;
dfs(i, j);
}
}
} cout << sum << endl;
return ;
}
上一篇:python网页爬虫开发之六-Selenium使用


下一篇:readfile() 函数