题目描述: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个方向
 // Satellite Photographs.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; const int MAX = ;
int n, m,ans, vis[MAX][MAX], dir[][] = { , , -, , , , , -};
char map[MAX][MAX]; void DFS(int x, int y, int num)
{
//cout << "x:" << x << "\ty:" << y << "\tnum:" << num << endl;
ans = max(ans, num); for (int i = ; i < ; i++)
{
int nx = x + dir[i][];
int ny = y + dir[i][];
if (nx >= && nx < n && ny >= && ny < m && !vis[nx][ny] && map[nx][ny] == '*')
{
//cout << "nx:" << nx << "\tny:" << ny << endl;
vis[nx][ny] = ;
DFS(nx, ny, num + );
vis[nx][ny] = ;
}
} } int main()
{ memset(vis, , sizeof(vis));
memset(map, '\0', sizeof(map));
ans = ; cin >> m >> n ;
for (int i = ; i < n; i++)
cin >> map[i]; for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (map[i][j] == '*' && !vis[i][j])
{
vis[i][j] = ;
DFS(i, j, );
vis[i][j] = ;
} }
}
cout << ans << endl; }

 
 

最新文章

  1. JAVA理解逻辑程序的书上全部重要的习题
  2. React组件属性部类(propTypes)校验
  3. 几种方法实现ajax请求内容时使用浏览器后退和前进功能
  4. SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder
  5. 关于三星I9305出现android.process.acore提示问题
  6. ubuntu 13.10 Ralink RT3290 无线与蓝牙4.0的驱动安装
  7. ACE - 代码层次及Socket封装
  8. ssh 安全配置
  9. gulp-less学习教程
  10. NSString NSCFString区别
  11. (转) 学习C++ -&gt; 引用( References )
  12. 【翻译】Asp.net Core介绍
  13. bzoj 3879 虚树
  14. OSI模型和TCP/IP协议族(二)
  15. Redis缓存实现排序功能
  16. 2018软件工程W班第一次助教小结
  17. maven(一):是否有必要使用maven
  18. nginx防盗链、nginx访问控制、nginx解析php相关配制、nginx代理
  19. 设置tab标签页 遮挡部分
  20. 调用css文件,进行调色

热门文章

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:将页面元素所包含的文本内容替换为背景图
  2. 回顾PHP:第一章:PHP基础语法(2)
  3. 仿照Android标准API写的各种形式的弹出框
  4. js网页拉起支付宝支付
  5. Tomcat+JSP经典配置实例
  6. javascript if else优化指南
  7. sklearn调用分类算法的评价指标
  8. 使用loadrunner监控apcahe资源
  9. Object.getOwnPropertyDescriptors()
  10. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:插入符