uva 572 - Oil Deposits

本文介绍了使用广搜算法解决UVA 572问题的方法,该问题要求通过广搜计算油田块的数量。通过定义结构体、判断函数以及广搜函数实现了解决方案。
// uva 572 - Oil Deposits
// 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=513
// 八方向用广搜,求连通的块数
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int row, column;
int count;
char map[200][200];
int flag[200][200];
struct Node{
    int x, y;
};
queue<Node> que;
Node now_node;
bool inmap(Node node){
    return node.x >= 0 && node.x < row && node.y >= 0 && node.y < column;
}
int direct[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
void bfs();
int main(){
    while (scanf("%d %d", &row, &column), row || column){
        getchar();
        memset(map, 0, sizeof(map));
        memset(flag, 0, sizeof(flag));
        count = 0;
        for(int i = 0; i < row; i++) {
            gets(map[i]);
        }
        for(int i = 0; i < row; i++){
            for(int j = 0; j < column; j++){
                if(map[i][j] == '@' && !flag[i][j]){
                    flag[i][j] = ++count;
                    now_node.x = i;
                    now_node.y = j;
                    bfs();
                }
            }
        }
        cout << count << endl;
    }
    return 0;
}
void bfs(){
    que.push(now_node);
    Node tmp_node;
    while(que.size()){
        now_node = que.front();
        que.pop();
        for(int i = 0; i < 8; i++){
            tmp_node.x = now_node.x + direct[i][0];
            tmp_node.y = now_node.y + direct[i][1];
            if(inmap(tmp_node) && map[tmp_node.x][tmp_node.y] == '@' && !flag[tmp_node.x][tmp_node.y]){
                flag[tmp_node.x][tmp_node.y] = count;
                que.push(tmp_node);
            }
        }

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值