hdoj 1241 Oil Deposits

来源:互联网 发布:台式电脑怎么链接网络 编辑:程序博客网 时间:2024/06/13 09:15

题目大意:@表示油田,求一共有多少组油田是连通的,连通的意思是与@的八个方向相邻

解题思路:水题。。

用深度优先遍历每个点,每走过了一个油田就标记为走过了。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 110;char field[maxn][maxn];int dir[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};int m, n;bool flag;bool dfs(int x, int y);int main(){int ans;while(true){scanf("%d %d", &m ,&n);if(0 == m && 0 == n)break;ans = 0;for(int i = 0; i < m; i++)scanf("%s", field[i]);for(int i = 0; i < m; i++){for(int j = 0; j < n; j++){flag = false;if(dfs(i, j))ans++;}}printf("%d\n", ans);}return 0;}bool dfs(int x, int y){if(field[x][y] == '*')return flag;flag = true;field[x][y] = '*';int tx, ty;for(int i = 0; i < 8; i++){tx = x + dir[i][0]; ty = y + dir[i][1];if(tx >= 0 && tx < m && ty >= 0 && ty < n)dfs(tx, ty);}return flag;}


原创粉丝点击