初学DFS:HDU1241 Oil deposits

#include <iostream>
#include <cstdio>
#include <cstring>
//#define test
using namespace std;
char vis[105][105];
int cnt;
int M,N;
int dir[8][2] = {{0,1},{1,0},{-1,0},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
void dfs(int x,int y)
{
vis[x][y]='*';
//printf("%d %d\n",x,y);
int xx,yy;
for(int i=0; i<8; i++)
{
xx=x+dir[i][0];
yy=y+dir[i][1];
if(xx>M||yy>N||xx<1||yy<1||vis[xx][yy]=='*')
continue;
if(vis[xx][yy]=='@')
dfs(xx,yy);
}
return;
}
int main()
{
//fp=fopen("out.txt","w");
while(scanf("%d %d",&M,&N)!=EOF)
{
getchar();
if(M==0||N==0)
break;
for(int i=1; i<=M; i++)
{
for(int j=1; j<=N; j++)
scanf("%c",&vis[i][j]);
getchar();//get the char '\n'
}
#ifdef test
for(int i=1; i<=M; i++)
{
for(int j=1; j<=N; j++)
printf("%c",vis[i][j]);
printf("\n");
}
#endif
cnt=0;
for(int i=1; i<=M; i++)
for(int j=1; j<=N; j++)
if(vis[i][j]=='@')
{
cnt++;
dfs(i,j);
}
printf("%d\n",cnt);
//memset(vis,0,sizeof(vis));
}
return 0;
}



• 本文已收录于以下专栏：

【HDU 1241 Oil Deposits】 + DFS || BFS

Oil DepositsProblem Description The GeoSurvComp geologic survey company is responsible for detectin...

PKU 1562/HDU 1241 Oil Deposits(原油有多少块区域---BFS,DFS)

Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De...

HDU1241：Oil Deposits(DFS)

Problem Description The GeoSurvComp geologic survey company is responsible for detecting undergroun...

K - Oil Deposits HDU - 1241——DFS

Think: 1本题DFS函数含义:寻找符合条件的连接体 2遍历方向:8个方向建议参考博客链接K - Oil Deposits HDU - 1241 The GeoSurvComp g...

HDU 1241 Oil Deposits (基础DFS 或者 BFS)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot...

HDU1241 Oil Deposits 解题报告--dfs

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot...

HDU/HDOJ 1241 Oil Deposits (DFS)深度优先搜索

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)