L - Oil Deposits
很清新的一道题,搜索里面最基础的题目,深搜广搜都可以.....不过还是喜欢深搜,写起来简单》。。
////////////////////////////////////////////////
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn = 105;
const int oo = 0xfffffff;
int dir[8][2] = { {0,1},{0,-1},{1,0},{-1,0},
{1,-1},{1,1},{-1,1},{-1,-1}};
int M, N;
char G[maxn][maxn];
void DFS(int x, int y)
{
if(x<0||x==M||y<0||y==N||G[x][y] != '@')
return ;
G[x][y] = '*';
for(int i=0; i<8; i++)
DFS(x+dir[i][0], y+dir[i][1]);
}
int main()
{
while(scanf("%d%d", &M, &N), M+N)
{
int i, j, ans=0;
for(i=0; i<M; i++)
scanf("%s", G[i]);
for(i=0; i<M; i++)
for(j=0; j<N; j++)
{
if(G[i][j] == '@')
{
ans++;
DFS(i, j);
}
}
printf("%d\n", ans);
}
return 0;
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn = 105;
const int oo = 0xfffffff;
int dir[8][2] = { {0,1},{0,-1},{1,0},{-1,0},
{1,-1},{1,1},{-1,1},{-1,-1}};
int M, N;
char G[maxn][maxn];
void DFS(int x, int y)
{
if(x<0||x==M||y<0||y==N||G[x][y] != '@')
return ;
G[x][y] = '*';
for(int i=0; i<8; i++)
DFS(x+dir[i][0], y+dir[i][1]);
}
int main()
{
while(scanf("%d%d", &M, &N), M+N)
{
int i, j, ans=0;
for(i=0; i<M; i++)
scanf("%s", G[i]);
for(i=0; i<M; i++)
for(j=0; j<N; j++)
{
if(G[i][j] == '@')
{
ans++;
DFS(i, j);
}
}
printf("%d\n", ans);
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步