题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241
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 analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
题意:八个方向判断有几个连通。
思路:dfs深搜,代码如下:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char map[100][100];
int vis[100][100]; //用来判重,之前是否已经遍历
int dir[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1}; //8个方向
int n,m,ans;
void DFS(int i,int j)
{
vis[i][j]=1;
for(int k=0;k<8;k++) //枚举8个方向
{
int x=i+dir[k][0];
int y=j+dir[k][1];
if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&map[x][y]=='@') //判断是否越界
{
DFS(x,y);
}
}
return ;
}
int main()
{
while(cin>>n>>m&&n&&m)
{
for(int i=0;i<n;i++)
cin>>map[i];
ans=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(!vis[i][j]&&map[i][j]=='@')
{
ans++;
DFS(i,j);
}
}
}
cout<<ans<<endl;
}
return 0;
}
ps:有期待才会有结果!
紫书上的写法很厉害:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
char map[100][100];
int vis[100][100];
int m,n;
void DFS(int r,int c,int id)
{
if(r<0||r>=m||c<0||c>=n) return ; //出界的格子
if(vis[r][c]>0||map[r][c]!='@') return ; //不是'@'或者已经访问过的格子
vis[r][c]=id; //连通分量编号
for(int dr=-1;dr<=1;dr++) //通过二重循环找到当前格子相邻的8个格子,也可用方向数组写
{
for(int dc=-1;dc<=1;dc++)
{
if(dr!=0||dc!=0)
DFS(r+dr,c+dc,id);
}
}
}
int main()
{
while(cin>>m>>n&&m&&n)
{
for(int i=0;i<m;i++)
cin>>map[i];
memset(vis,0,sizeof(vis));
int cnt=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(vis[i][j]==0&&map[i][j]=='@')
DFS(i,j,++cnt);
}
}
cout<<cnt<<endl;
}
return 0;
}
下面贴下辞树大佬的这道题的题解法:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
int n,m;
char s[110][110];
int mapp[110][110];
int xx[8]={-1,1,0,0,-1,-1,1,1};
int yy[8]={0,0,-1,1,-1,1,-1,1};
void dfs(int a,int b){
if(a<0||a>=n||b<0||b>=m||mapp[a][b]==0){
return ;
}
mapp[a][b]=0;
for(int i=0;i<8;i++){
int na=a+xx[i];
int nb=b+yy[i];
dfs(na,nb);
}
}
//0是* 1是@ 0 是墙 1是路
int main(){
while(~scanf("%d%d",&n,&m)&&(n!=0&&m!=0)){
memset(mapp,0,sizeof(mapp));
memset(s,0,sizeof(s));
getchar();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%c",&s[i][j]);
if(s[i][j]=='*')
mapp[i][j]=0;
else
mapp[i][j]=1;
}
getchar();
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mapp[i][j]==1){
dfs(i,j);
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}
https://blog.youkuaiyun.com/l18339702017/article/details/81280637