POJ 1562 Oil Deposits

8 篇文章 0 订阅
   简单的一道搜索题, 题目意思是给你一幅图, *代表没油, @代表有油, 让你求连在一起的油有几块。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#define mem(a) memset(a, 0, sizeof(a))
#define maxn 105
using namespace std;

int ox[8] = {-1, 1, 0, 0, 1, -1, -1, 1}, oy[8] = {0, 0, -1, 1, 1, -1, 1, -1}, n, m;
char map[maxn][maxn];

void bfs(int sx, int sy)
{
	int i, x, y;
	queue<int> q;
	q.push(sx);
	q.push(sy);
	while(!q.empty())
	{
		x = q.front();
		q.pop();
		y = q.front();
		q.pop();
		for(i = 0;i < 8;i++)
		{
			if((x + ox[i]) >= 0&&(x + ox[i]) < n&&(y + oy[i]) >= 0&&(y + oy[i]) < m&&map[x + ox[i]][y + oy[i]] == '@')
			{
				map[x + ox[i]][y + oy[i]] = '*';
				q.push(x + ox[i]);
				q.push(y + oy[i]);
			}
		}
	}
}

int main(int argc, char *argv[])
{
	int i, j, k, ans;
	for(;;)
	{
		ans = 0;
		mem(map);
		scanf("%d%d%*c",&n, &m);
		if(n == 0&&m == 0)
		break;
		for(i = 0;i < n;i++)
		{
			scanf("%s", map[i]);
		}
		for(i = 0;i < n;i++)
		{
			for(j = 0;j < m;j++)
			{
				if(map[i][j] == '@')
				{
					ans++;
					map[i][j] = '*';
					bfs(i, j);
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值