UVa 572 油田

题目:相当于有一个m行n列的格子矩阵,每个格子要么@要么*,两个格子是连通的当且仅当这两个格子都是@、且一个格子和另一个是水平、垂直或对角线相邻,即一个格子在另一个格子的周围八个格子范围内。最后求连通块的个数。

思路:和书上例题黑白格子一样,只是一个01,一个*@,稍微改一下就可以了。即通过dfs(x,y)递归地深度优先遍历。

Code:

#include<stdio.h>
#include<string.h>
#define MAX 110

void dfs(int x,int y);

char mat[MAX][MAX];
int vis[MAX][MAX];
int m,n;

int main()
{
 while(scanf("%d%d",&m,&n)==2 && m)
 {
  memset(mat,'*',sizeof(mat));
  memset(vis,0,sizeof(vis));
  char s[MAX];
  for(int i=0;i<m;++i)
  {
   scanf("%s",s);
   for(int j=0;j<n;++j)
    mat[i+1][j+1]=s[j];
  }

  int cnt=0;
  for(int i=1;i<=m;++i)
   for(int j=1;j<=n;++j)
    if(mat[i][j]=='@' && !vis[i][j])
    {
     dfs(i,j);
     cnt++;
    }
  printf("%d\n",cnt);
 }
 return 0;
}

void dfs(int x,int y)
{
 if(mat[x][y]=='*' || vis[x][y]) return ;
 vis[x][y]=1;
 dfs(x-1,y-1); dfs(x-1,y); dfs(x-1,y+1);
 dfs(x,y-1);               dfs(x,y+1);
 dfs(x+1,y-1); dfs(x+1,y); dfs(x+1,y+1);
}
时间: 2025-01-05 06:37:16

UVa 572 油田的相关文章

UVA 572 油田连通块-并查集解决

题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... 1 #include <cstdio> 2 #include <iostream> 3 #include <sstream> 4 #include <cmath> 5 #include <cstring> 6 #include <cstdlib&

UVa 572 油田(DFS求连通块)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=513 终于开始接触图了,恩,开始接触DFS了,这道题就是求连通分量,比较简单. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int m, n; //记录连通块的数量 6 cha

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

UVA - 572 - Oil Deposits (图的DFS!)

UVA - 572 Oil Deposits Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works

UVa 572 Oil Deposits(DFS)

 Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.

UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常量数组或者写8条DFS调用. 下述算法是:种子填充(floodfill) 两种连通区域 四连通区域:从区域内一点出发,可通过上.下.左.右四个方向的移动组合,在不越出区域的前提下,能到达区域内的任意像素 八连通区域:从区域内每一像素出发,可通过八个方向,即上.下.左.右.左上.右上.左下.右下移动的

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小时连题解都出来了··· A-烤肉拌饭 ( uva 572) 就是求联通块的数量啊,刚学dfs的时候做的那种! 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <ios

uva 572

Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

UVA 572 dfs求连通块

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyze