ACM-Satellite Photographs

题目描述: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

时间: 2024-11-02 07:10:33

ACM-Satellite Photographs的相关文章

POJ 3051 Satellite Photographs

Satellite Photographs Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3051 Description Farmer John purchased satellite photos of W x H pixels of his farm (1 <= W <= 80, 1 <= H <= 1000

Problem I: Satellite Photographs

Problem I: Satellite Photographs Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 208  Solved: 118 [Submit][Status][Web Board] Description Farmer John purchased satellite photos of W x H pixels of his farm (1 <= W <= 80, 1 <= H <= 1000) and wis

深度优先搜索:Satellite Photographs

Description 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 ca

图的深搜应用-Satellite Photographs

Description 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 ca

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

论文学习——《Learning to Compose with Professional Photographs on the Web》 (ACM MM 2017)

这篇论文的思路基于一个简单的假设:专业摄影师拍出来的图片一般具备比较好的构图,而如果从他们的图片中随机抠出一块,那抠出的图片大概率就毁了.也就是说,原图在构图方面的分数应该高于抠出来的图片.而这种比较的方式,可以很方便地用 Siamese Network 和 hinge loss 实现.另外,这篇论文另一个讨人喜欢的地方在于,它几乎不需要标注数据,只需要在网上爬取很多专业图片,再随机抠图就可以快速构造大量训练样本,因此成本近乎为零,即使精度不高也可以接受. 当然,这篇论文的训练方式只能让网络知道

《ACM/ICPC 算法训练教程》读书笔记一之数据结构(堆)

书籍简评:<ACM/ICPC 算法训练教程>这本书是余立功主编的,代码来自南京理工大学ACM集训队代码库,所以小编看过之后发现确实很实用,适合集训的时候刷题啊~~,当时是听了集训队final的意见买的,感觉还是不错滴. 相对于其他ACM书籍来说,当然如书名所言,这是一本算法训练书,有着大量的算法实战题目和代码,尽管小编还是发现了些许错误= =,有部分注释的语序习惯也有点不太合我的胃口.实战题目较多是比较水的题,但也正因此才能帮助不少新手入门,个人认为还是一本不错的算法书,当然自学还是需要下不少