Topic background
Tall sky, clusters of sparkling stars in the shape great. A constellation (Cluster) is non-empty star galaxy communication group consisting of communication, where the communication means is a horizontal, vertical or diagonally adjacent two stars. A constellation not another part of a larger constellation, the constellation may be similar (similar). If two signs have the same shape, but also the same number of stars, so regardless of their direction, even if similar. Generally, the constellation has eight possible directions, as shown in FIG.
Title Description
Can be expressed as a night sky map (sky map), which is a two-dimensional matrix consisting of the characters 0 and 1, character 1 represents the location of a star; 0 indicates that the character position is given a blank. FIG celestial parts, with all the signs lowercase identification (Mark) with a similar. Similar constellations must different constellation represented by the same identification letter as different letters. Identifying a constellation in which each star is a character corresponding to a respective replaced lowercase.
Input and output formats
Input formats:
The first two lines of the file are recorded celestial width W, the depth H. The celestial map is represented by the following H lines, each line comprising W characters. ......
Output formats:
FIG celestial file contains a record file STARRY.IN similar, except that each of the identified constellation (Mark) in accordance with the "job" in the requirements.
For the same input file, there may be many different identity, this time, the output lexicographically minimal identification.
Sample input and output
Input Sample # 1:
23 15 10001000000000010000000 01111100011111000101101 01000000010001000111111 00000000010101000101111 00000111010001000000000 00001001011111000000000 10000001000000000000000 00101000000111110010000 00001000000100010011111 00000001110101010100010 00000100110100010000000 00010001110111110000000 00100001110000000100000 00001000100001000100101 00000001110001000111000
Output Sample # 1:
a000a0000000000b0000000 0aaaaa000ccccc000d0dd0d 0a0000000c000c000dddddd 000000000c0b0c000d0dddd 00000eee0c000c000000000 0000e00e0ccccc000000000 b000000e000000000000000 00b0f000000ccccc00a0000 0000f000000c000c00aaaaa 0000000ddd0c0b0c0a000a0 00000b00dd0c000c0000000 000g000ddd0ccccc0000000 00g0000ddd0000000e00000 0000b000d0000f000e00e0b 0000000ddd000f000eee000
Problem-solving ideas
This question is the standard to find Unicom block, but it can be rotated eight galaxies direction, this sentence is very severe trouble, so we use a simple mathematical reason to re-sentence: two figures, if they own any two and an equal distance from point to prove they are the same graphics.
answer
. 1 #include <bits / STDC ++ H.> 2 #define N 105 . 3 the using namespace STD; . 4 int n-, m, Star = 0 ; // this kind of galaxies . 5 BOOL In Flag [N] [N]; // Search mark . 6 char MP [N] [N]; // save FIG . 7 Double Star [N]; // distance and each galaxy . 8 int the dir [ . 8 ] [ 2 ] = { 0 , . 1 , 0 , - 1 , 1 , 0 , -1 , 0 , 1 , 1 , 1 - 1 - 1 , 1 - 1 - 1 }; // eight directions . 9 struct Node { // coordinates 10 int X; . 11 int Y; 12 is }; 13 is Node Galaxy [N]; // coordinates of each star of the search to the current galaxies 14 void the Galaxy ( int X) // processing of the current letter galaxies 15 { 16 Double ANS = 0 ;// sum of the distances . 17 int In Flag = 0 ; 18 is for ( int I = . 1 ; I <= X; I ++ ) . 19 { 20 is for ( int J = . 1 ; J <= X; J ++ ) 21 is { 22 is ANS + = sqrt (( galaxy [i] .x-galaxy [ j] .x) * (galaxy [i] .x-galaxy [j] .x) + (galaxy [i] .y-galaxy [j] .y) * (galaxy [ I] .y- Galaxy [J] .y)); 23 is // distance between two points in a plane equation 24 } 25 } 26 is for ( int I = . 1; I <= Star; I ++ ) 27 { 28 IF (FABS (Star [I] -ans) <= 0.00001 ) // if the difference is less than 0.00001, we determined that the same galaxy 29 { 30 In Flag = I; // record the galaxy, convenient storage letters 31 is BREAK ; 32 } 33 is } 34 is IF (in Flag!) // If it is a new galaxy 35 { 36 Star ++; // galaxy type increases 37 [ Star [Star] = ANS; 38 is for ( int I = . 1; I <= X; I ++ ) 39 { 40 MP [Galaxy [I] .x] [Galaxy [I] .y] = ' A ' + The Star- . 1 ; // store letters 41 is } 42 is return ; 43 is } 44 is for ( int I = . 1 ; I <= X; I ++ ) 45 { 46 is MP [Galaxy [I] .x] [Galaxy [I] .y] = ' a ' + FLAG- . 1 ; // find similar letters stars 47 } 48 } 49 voidBFS ( int X, int Y) // find the link block 50 { 51 is int NUM = . 1 ; // start starting star 52 is Queue <Node> Q; 53 is q.push ((Node) {X, Y}) ; 54 is In flag [X] [Y] = to true ; /// numerals 55 Galaxy [NUM] = (Node) {X, Y}; // record the coordinates 56 is the while (! q.empty ()) 57 is { 58 Node head = q.front (); 59 q.pop (); 60 for (int I = 0 ; I < . 8 ; I ++ ) 61 is { 62 is int TX + = head.x the dir [I] [ 0 ]; 63 is int TY + = head.y the dir [I] [ . 1 ]; 64 IF (TX> = . 1 && TY> = . 1 && TX <= n-&& TY <= m &&! In flag [TX] [TY] && MP [TX] [TY] == ' . 1 ' ) // out of range with the labeled 65 { 66 In flag [TX] [TY] = to true ; 67 NUM ++; // number of stars in the galaxy increase 68 Galaxy [NUM] = (Node) {TX, TY}; // store the coordinates 69 q.push ((Node) {TX, TY}); 70 } 71 is } 72 } 73 is the Galaxy (NUM); // start determination that galaxy 74 } 75 int main () 76 { 77 CIN >> m >> n-; 78 for ( int I = . 1 ; I <= n-; I ++ ) 79 { 80 for ( int J = . 1 ; J <= m; J ++ ) 81 { 82 CIN MP >> [I] [J]; // save FIG. 83 } 84 } 85 for ( int I = . 1 ; I <= n-; I ++ ) 86 { 87 for ( int J = . 1 ; J <= m ; J ++ ) 88 { 89 IF ! (in Flag [I] [J] && MP [I] [J] == ' . 1 ' ) // not been found over the stars start looking galaxy 90 { 91 is BFS (I, J); 92 } 93 } 94 } 95 for(int i=1;i<=n;i++) 96 { 97 for(int j=1;j<=m;j++) 98 { 99 cout<<mp[i][j];//输出 100 } 101 cout<<endl; 102 } 103 }