Hihocoder 1310 Islands

Source: Internet
Author: User

#1310: 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
 
  

Ideas: (1) The number of islands is very simple, initialize the number of islands Numofislands to 0, traverse all the points, if this point is not accessed and for ' # ', then numofislands++, perform DFS search, and this point belongs to the same island where all the dots for ' # ' are marked as visited.

(2) When solving an island, calculate its area, save all the area, remove the duplicate elements, the number of the remaining elements is the area of different islands.

(3) Dfs searches all islands, and also preserves the pixel coordinates of each island and sorts them by coordinates (first from small to large, if the x coordinates are the same, then y from small to large). The number of islands of the same shape we can get by comparing each pixel of the island one at a. When we compare Island X and Island Y, the shape of x and Y is the same if each pair of pixels has the same coordinate difference. (First, if the size of the two islands is different, the shape must be different, and then the coordinates of the X-I (1 <= i <= area-1) coordinate with the coordinate of its first coordinate ? = The coordinates of the first and second coordinates of the island y are poor, and if there is an inequality, Shapes are different).

1#include <iostream>2#include <cstdio>3#include <Set>4#include <vector>5#include <algorithm>6 using namespacestd;7 8 intN, M;//N is the number of rows, and M is the number of columns9 Charmap[ -][ -];//Storing character matricesTen BOOLvisit[ -][ -];//as an array of tokens One  A intdx[4] = {-1,0,1,0};//direction Array, in order to optimize DFS code - intdy[4] = {0,1,0, -1}; - intArea =0; the  - intNumofislands =0, Nodai =0, NODCI;//The final numofislands represents the number of islands, Nodai represents the number of islands in different areas, - //The numofislands is also the number of an island during the calculation, with the island numbering starting from 0 -  + structPosition { -     intx; +     inty; A }; at  - intnum[ -];//Num[i] Stores the area size of the number I island -  - structPosition a[ -][ -];//indicates a maximum of 300 islands, with a maximum area of 300 for each island that corresponds to 300 coordinates -  -  in BOOLflag[ -]; -  to BOOLcmpstructPosition A,structposition B) { +     if(A.x! =b.x) -         returnA.x <b.x; the     Else  *         returnA.y <b.y; $ }Panax Notoginseng  - intIssame (structPosition *c,structPosition *d,intXintY) {//determine if two islands are of the same shape the     intFlag =1; +     if(Num[x]! =Num[y]) A         return 0; the      for(inti =1; i < num[x]; i++){ +         if(((c[i].x-c[0].x) = = (d[i].x-d[0].x) && (c[i].y-c[0].Y) = = (d[i].y-d[0].y ))) -             Continue; $         Else { $Flag =0; -              Break; -         } the     } -     returnFlag;Wuyi } the  - voidDfsintXinty) { Wua[numofislands][area].x = x;//Save the value of section Numofislands X of the first island -A[NUMOFISLANDS][AREA].Y =y; Aboutarea++;//area number plus 1 $Visit[x][y] =1;//tagged coordinates (x, y) are accessed -      for(inti =0; I <4; i++){ -         intNX = x +Dx[i]; -         intNY = y +Dy[i]; A         if(NX >=0&& NX < N && NY >=0&& NY < M && Map[nx][ny] = ='#'&& Visit[nx][ny] = =0) + DFS (NX, NY); the     } - } $  the intMain () { the      the     Set<int>v; the     intI, J; -      inCIN >> N >>M; the     //Input character Matrix the      for(i =0; i < N; i++) AboutCIN >>Map[i]; the          the      for(i =0; i < N; i++) { the          for(j =0; J < M; J + +){ +             if(Map[i][j] = ='#'&& Visit[i][j] = =0){ -Area =0;//initialize an island with an area of 0 the Dfs (I, j);BayiNum[numofislands] =Area ; the V.insert (area); thenumofislands++; -             } -         } the     } the  theNodai = V.size ();//number of islands of different size the  -NODCI =numofislands; the  the     //sort the coordinates of each island to make it easier to compare the shapes of two islands the      for(i =0; i < numofislands; i++){94Sort (A[i], A[i] +Num[i], CMP); the     } the  the     //calculate the number of islands with different shapes98      for(i =0; I < numofislands-1; i++){ About         if(Flag[i] = =1){ -             Continue;101         }102         Else {103              for(j = i+1; J < Numofislands; J + +) {104                 if((flag[j] = =0) &&(Issame (A[i], a[j], I, j))) { theFLAG[J] =1;106nodci--;107                 }108             }109         } the             111     } the 113cout << numofislands <<" "<< Nodai <<" "<< NODCI <<Endl; the     //System ("pause"); the     return 0; the}

Hihocoder 1310 Islands

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.