POJ1562 Oil Deposits(比较水的dfs)

xiaoxiao2021-02-28  61

#题目大意:出现一个油田,其周围八个位置(不是上下左右)只要有油田,就被认为是一块连通的油田,求连通油田的个数。

#include<iostream> #include<cstdio> using namespace std; char oil[105][105]; //油田 int arry[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};//往八个方向搜索 int m, n;//油田的行和列 void dfs(int x, int y){ oil[x][y] = '*'; //将遇到的 @ 变成 *,避免重复 for(int i = 0; i < 8; i++){ int tempx = x + arry[i][0]; int tempy = y + arry[i][1]; if(tempx >= 0 && tempx < m && tempy >=0 && tempy < n && oil[tempx][tempy] == '@' ) dfs(tempx, tempy); } } int main(){ while(scanf("%d %d", &m, &n) && m && n){ for(int i = 0; i < m; i++) scanf("%s",oil[i]); //字符数组一次输入一行,注意一下 int cnt = 0; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(oil[i][j] == '@'){ dfs(i,j); cnt++; } } } printf("%d\n",cnt); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-83887.html

最新回复(0)