【广度优先搜索】2000: 卫星照片 USACO 2005 NOV

广度优先搜索求卫星照片最大连续牧场
题目描述

Farmer John purchased satellite photos of W x H pixels of his farm (1 <= W <= 80, 1 <= H <= 1000) and wishes to determine the largest 'contiguous' (connected) pasture. Pastures are contiguous when any pair of pixels in a pasture can be connected by traversing adjacent vertical or horizontal pixels that are part of the pasture. (It is easy to create pastures with very strange shapes, even circles that surround other circles.)

Satellite Photographs 农场主约翰给他的农场买了W x H像素的卫星照片(1 <= W <= 80, 1 <= H <= 1000),希望找出最大的"连续的"(互相连接的)牧场。对于一个牧场的任何一对像素,其中一个像素如果能横向的或纵向的与属于这个牧场的另一个像素相连,这样的牧场称作是连续的。 (很容易创建形状稀奇古怪的牧场,甚至是围着其它圆圈的圆圈。)

Each photo has been digitally enhanced to show pasture area as an asterisk ('*') and non-pasture area as a period ('.'). Here is a 10 x 5 sample satellite photo:

 每一张照片都数字化的抽象了,牧场区显示为"*",非牧场区显示为"."。下面是一个10 x 5的卫星照片样例: 

..*.....**
.**..*****
.*...*....
..****.***
..****.***

This photo shows three contiguous pastures of 4, 16, and 6 pixels. Help FJ find the largest contiguous pasture in each of his satellite photos.

这张照片显示了大小分别为4、16、6个像素的连续牧场区。帮助农场主约翰在他的每张卫星照片中找到最大的连续牧场。

Input
* Line 1: Two space-separated integers: W and H

 * 第1行: 两个由空格分开的整数,W 和 H。
* Lines 2..H+1: Each line contains W "*" or "." characters representing one raster line of a satellite photograph.

 * 第2到H+1行: 每一行包含W个"*"或者".",代表卫星照片的横向行。

Output
* Line 1: The size of the largest contiguous field in the satellite photo.

* 1行: 最大连续牧场的大小 

Sample Input
10 5
..*.....**
.**..*****
.*...*....
..****.***
..****.***

Sample Output
16

点个赞和关注,上代码

我用好几种语言来编写,所以不全补充

c++代码:

#include <iostream>

#include <vector>

#include <queue>

#include <bits/stdc++.h>

using namespace std;

int bfs(vector<vector<char>>& grid, int i, int j) {

    int w = grid[0].size();

    int h = grid.size();

    queue<pair<int, int>> q;

    q.push({i, j});

    grid[i][j] = '.';

    int size = 0;

   

    vector<pair<int, int>> directions = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}};

   

    while (!q.empty()) {

        auto [x, y] = q.front();

        q.pop();

        size++;

       

        for (auto& dir : directions) {

            int nx = x + dir.first;

            int ny = y + dir.second;

           

            if (nx >= 0 && nx < h && ny >= 0 && ny < w && grid[nx][ny] == '*') {

                grid[nx][ny] = '.';

                q.push({nx, ny});

            }

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呱呱呱~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值