Hiho #1310: Islands (Dfs,hash)

Source: Internet
Author: User

Topic 2: Island time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

Give you a satellite photograph of a certain sea area, you need statistics:

1. Number of islands in the photo

2. Number of islands with different areas in the photo

3. Number of pirates with different shapes in the photo

The photos of the sea are as follows, "." Represents the ocean, "#" denotes land. An island is formed on a piece of land connected together in the "Up and down" four directions.

.####..  .....#.  ####.#.  .....#.  .. ##.#.  

A total of 4 islands are shown, of which 3 are 4 and one area is 2, so the number of islands in different areas is 2, and the two shapes are "# # #", so the number of islands with different shapes is 3.

Input

The first line contains two person integers: N and M , (1≤ N , M ≤50), representing the number of rows and columns of the photo.

The following N is a M matrix that represents a photograph of the sea area.

Output

Outputs 3 integers, followed by the number of islands in the photo, the number of islands with different areas, and the number of islands with different shapes.

Sample input
5 7.####.  .....#.  ####.#.  .....#.  .. ##.#.  
Sample output
4 2 3

Ideas:

Deep search, searched islands, set the island for X, and record with an area array to search the size of the island's size. An island of different size, through the location of the island, hash out a value, using set of the de-weight function, and finally determine how many elements in the sharp array.

Note: About hash, hash value, I use 84 when re,,, 91AC, guess the effect of prime number is better.

AC Code:

1#include <stdio.h>2#include <Set>3 using namespacestd;4 5 Chars[ the][ the];6 intN, M;7 intdi[4] = {1, -1,0,0};8 intdj[4] = {0,0,1, -1};9 Set<int>Area , shape;Ten  One intDfsintIintJint*mini,int*minj,int*maxi,int*MAXJ) { A     if(I < *Mini) -*mini =i; -     if(I > *Maxi) the*maxi =i; -     if(J < *Minj) -*minj =J; -     if(J > *MAXJ) +*MAXJ =J; -S[I][J] ='x'; +     intA =1; A      for(intD =0; D <4; ++d) { at         intNI = i + di[d], NJ = j +Dj[d]; -         if(S[ni][nj] = ='#') -A + =dfs (NI, NJ, Mini, Minj, Maxi, MAXJ); -     } -     returnA; - } in  - intMhash (intMiniintMinj,intMaxiintMAXJ) { to     intK = -, sh =0; +      for(inti = Mini; I <= Maxi; ++i) { -          for(intj = Minj; J <= Maxj; ++j) { theSH = sh * k +S[i][j]; *         } $k*= the;Panax Notoginseng     } -     returnsh; the } +  A intMain () { thescanf"%d%d", &n, &M); +      for(inti =0; i < N; ++i) -scanf"%s", S[i +1] +1); $     intN1 =0; $      for(inti =1; I <= N; ++i) -          for(intj =1; J <= M; ++j) -             if(S[i][j] = ='#') { then1++; -                 intMini = i, Maxi = i, Minj = j, Maxj =J;WuyiArea.insert (Dfs (i, J, &mini, &minj, &maxi, &maxj)); the Shape.insert (Mhash (Mini, Minj, Maxi, MAXJ)); -             } Wuprintf" %d%d%d", N1, Area.size (), shape.size ()); -     return 0; About}
View Code

Hiho #1310: Islands (Dfs,hash)

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.