Depth First search: satellite photographs

Source: Internet
Author: User

Description

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

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

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

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

Input

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

Output

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

Sample Input

10 5..*.....**.**. *****.*...*......****.***.. ****.***

Sample Output

16

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <string>5#include <algorithm>6 7 using namespacestd;8 9 Const intX[] = {0,0, -1,1 };Ten Const intY[] = {-1,1,0,0 }; One  A Const intMAX =1010; - CharArr[max][max]; - BOOLVis[max][max]; the  - intW, H; - inttmp, ans; -  + voidDFS (intXinty); -  + intMain () A { at #ifdef OFFLINE -Freopen ("In.txt","R", stdin); -Freopen ("OUT.txt","W", stdout); - #endif -  -      while(~SCANF ("%d%d", &w, &h)) in     { -memset (arr,0,sizeof(arr)); tomemset (Vis,false,sizeof(Vis)); +          for(inti =0; I < H; i++) -scanf"%s", Arr[i]); the  *Ans =0; $          for(inti =0; I < H; i++)Panax Notoginseng         { -              for(intj =0; J < W; J + +) the             { +                 if(Arr[i][j] = ='*'&&!Vis[i][j]) A                 { theTMP =0; + DFS (i, j); -Ans =max (ans, tmp); $                 } $             } -         } -  theprintf"%d\n", ans); -     }Wuyi  the     return 0; - } Wu  - voidDFS (intXinty) About { $Vis[x][y] =true; -  -      for(inti =0; I <4; i++) -     { A         intNX = x + x[i], NY = y +Y[i]; +         if(NX >=0&& NX < h && NY >=0&& NY < w && Arr[nx][ny] = ='*'&&!Vis[nx][ny]) the         { - DFS (NX, NY); $         } the     } the  thetmp++; the     return; -}
View Code

Depth First search: satellite photographs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.