【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=1241
题目意思
一块地区下面可能有油田可能为空,如果两块油田相连(8个方向横竖加斜对角)算一块大油田,问你一共有多少油田。
解题思路
遍历一遍图,当遇到油田把相连的油田深搜全部标记了
代码部分
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <string>
#include <map>
using namespace std;
#define LL long long
#define inf 0x3f3f3f3
const int N = 1e5+5;
char maps[105][105];
int n,m,s;
int dir[8][2]={0,1,0,-1,-1,0,1,0,-1,-1,-1,1,1,-1,1,1};
void dfs(int x, int y)
{
for (int i = 0; i < 8; i++) ///标记相连的油田
{
int xx = x+dir[i][0];
int yy = y+dir[i][1];
if (xx >= 0 && yy >= 0 && xx < n && yy < m && maps[xx][yy] == '@')
{
maps[xx][yy] ='#';
dfs(xx,yy);
}
}
}
int main()
{
while (scanf("%d %d",&n,&m)!=EOF)
{
int ans = 0;
if (n == 0 && m == 0)
break;
for (int i = 0;i < n; i++)
scanf("%s",maps[i]);
for (int i = 0; i < n; i++) ///遍历整个图
for (int j = 0; j < m;j++)
{
if (maps[i][j] == '@')
{
ans++;
dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}