ACM-Satellite Photographs

本文介绍了一种使用深度优先搜索(DFS)算法解决卫星图片中寻找最大连续牧场面积的方法。通过遍历每个像素并标记已访问区域,可以有效确定牧场的最大尺寸。
题目描述:Satellite Photographs

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.) 

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: 

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

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.

输入

* Line 1: Two space-separated integers: W and H
* Lines 2..H+1: Each line contains W "*" or "." characters representing one raster line of a satellite photograph.

 

输出

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

样例输入
10 5
..*.....**
.**..*****
.*...*....
..****.***
..****.***
样例输出
16

思路:DFS求最大相连区域,8个方向

 1 // Satellite Photographs.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 
 6 #include <iostream>
 7 #include <cstring>
 8 #include <algorithm>
 9 using namespace std;
10 
11 const int MAX = 1005;
12 int n, m,ans, vis[MAX][MAX], dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1};
13 char map[MAX][MAX];
14 
15 void DFS(int x, int y, int num)
16 {
17     //cout << "x:" << x << "\ty:" << y << "\tnum:" << num << endl;
18     ans = max(ans, num);
19     
20     for (int i = 0; i < 4; i++)
21     {
22         int nx = x + dir[i][0];
23         int ny = y + dir[i][1];
24         if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && map[nx][ny] == '*')
25         {
26             //cout << "nx:" << nx << "\tny:" << ny << endl;
27             vis[nx][ny] = 1;
28             DFS(nx, ny, num + 1);
29             vis[nx][ny] = 0;
30         }
31     }
32 
33 
34 }
35 
36 
37 int main()
38 {
39 
40         memset(vis, 0, sizeof(vis));
41         memset(map, '\0', sizeof(map));
42         ans = 0;
43 
44         cin >> m >> n ;
45         for (int i = 0; i < n; i++)
46             cin >> map[i];
47 
48         for (int i = 0; i < n; i++)
49         {
50             for (int j = 0; j < m; j++)
51             {
52                 if (map[i][j] == '*' && !vis[i][j])
53                 {
54                     vis[i][j] = 1;
55                     DFS(i, j, 1);
56                     vis[i][j] = 0;
57                 }
58 
59             }
60         }
61         cout << ans << endl;
62 
63 
64 }

 




 
 

 

转载于:https://www.cnblogs.com/x739400043/p/8505380.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值