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