Topic background
Christmas Series ~
Title Description
Santa back to the North Pole Christmas area, has been approaching 12 o'clock. That aurora show to begin. Aurora here is not the polar aurora unique natural scene. But Santa chaired artificial aurora.
Fireworks thundered ...... sounded (from China Liuyang fireworks). Then there is the aurora show.
Artificial aurora is actually a bitmap image depicting the air of n * m. Just because a particular bright and attracted the eyes of many, many elves, Christmas Eve has become the most beautiful moment.
However, in the bitmap of each piece of n * m, each point and non-light emission only two states. For all the glowing spot in the air to form a beautiful picture. And this is the picture number (s a) pattern thereof. For patterning, Santa has a strict definition: For two light emitting points, if they Manhattan distance (for A (x1, y1), and B (x2, y2), the Manhattan distance between A and B | X1- x2 | + | y1-y2 |) is less than or equal to 2. Then the two points belongs to a pattern ...... elves while enjoying the aurora, while counting the number of patterns each picture aurora image. Accompanied by singing and dancing, we had a beautiful Christmas Eve. ^ _ ^
Input and output formats
Input formats:
The first line, the two numbers n and m.
Next, a total of n lines of m characters. For the i-th row j-th character, if it is "-", it indicates that no light emission point, if it is a "#", it indicates that the emission point. Other characters impossible.
Output formats:
The first line, a number s.
Sample input and output
Input Sample # 1:
19 48 ------------------------------------------------ ---####-----#-----#----------------------####--- --######----#-----#---------------------######-- -########--#-#---#-#####--#-##-##---#--########- -###--###--#-#---#-#----#-##-##--#--#--###--###- -###--###--#--#-#--######-#--#---#-#---###--###- -########--#--#-#--#------#--#----##---########- --######---#---#---######-#--#-----#----######-- ---####----------------------------#-----####--- ----------------------------------#------------- ------------------------------------------------ ---###--#--------#------#----------------------- --#---#-#---------------#----------------------- -#------#-##--#-##--##-###-#-##-###--###-#--##-- -#------##--#-##-#-#----#--##--#---##---##-#---- -#------#---#-#--#--#---#--#---#---##----#--#--- --#---#-#---#-#--#---#--#--#---#---##---##---#-- ---###--#---#-#--#-##---#--#---#---#-###-#-##--- ------------------------------------------------
Output Sample # 1:
4
Problem-solving ideas
Search water problem together with BFS and DFS can, I use the BFS. Manhattan from the title of no more than 2, 12 is the search direction only, then marking, record output just fine. (Words like eggs do have input sample).
answer
. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 int n-, m, ANS; . 4 char MP [ 105 ] [ 105 ]; // save array of FIG . 5 int the dir [ 12 is ] [ 2 ] = { 0 , 1 , 0 - 1 , 1 , 0 - 1 , 0 - 2 , 0 - 1 - 1 - 1 , 1 , 0 ,2 , 0 - 2 , 1 - 1 , 1 , 1 , 2 , 0 }; 6 // twelve directions . 7 struct Node { . 8 int X; . 9 int Y; // coordinate 10 }; . 11 void BFS ( int X, int Y) 12 is { 13 is Queue <Node> Q; 14 q.push ((Node) {X, Y}); 15 MP [X] [Y] = ' - ';//给起点打标记 16 while(!q.empty()) 17 { 18 node head=q.front(); 19 q.pop(); 20 for(int i=0;i<12;i++)//枚举方向 21 { 22 int tx=head.x+dir[i][0]; 23 int ty=head.y+dir[i][1]; 24 if(tx>=1&&ty>=1&&tx<=n&&ty<=m&&mp[tx][ty]=='#')// Analyzing and emitting no bounds 25 { 26 is MP [TX] [TY] = ' - ' ; // tag directly on FIG. 27 q.push ((Node) {TX, TY}); // into the queue 28 } 29 } 30 } 31 is } 32 int main () 33 is { 34 is CIN >> >> n- m; 35 for ( int I = . 1 ; I <= n-; I ++ ) 36 { 37 [ for ( int J =. 1 ; J <= m; J ++ ) 38 is { 39 CIN >> MP [I] [J]; // save FIG. 40 } 41 is } 42 is for ( int I = . 1 ; I <= n-; I ++ ) 43 is { 44 is for ( int J = . 1 ; J <= m; J ++ ) 45 { 46 is IF (MP [I] [J] == ' # ' ) // if not found to be 47 { 48 ANS ++; // number plus one pattern 49 BFS (I, J); // search the complete pattern 50 } 51 is } 52 is } 53 is COUT << ANS; 54 is }