50237242 Island Empire: the holy Day
"Question description"
Enter a character matrix for the M row n column, and the statistical character "@" to make up the number of eight connected blocks. If the two-character "@" is adjacent to the grid (either vertically or diagonally), it says that they belong to the same eight-connected block. For example, there are two eight connected blocks.
"Input Requirements"
The first line consists of two positive integers m and n, separated by a space, followed by m rows, with n characters per line, and the characters only include "*" and "@".
"Output Requirements"
A number that represents the number of eight even blocks
"Input Instance"
5 5****@*@@*@*@**@@@@*@@@**@
"Output instance"
2
"Other Notes"
Data range: 0<m,n<101. Produced by LJX
"Analysis of Test questions"
Very classic DFS, the teacher said: First into the map, and then find a @, from now on, if not, return. At that time just contact DFS when the problem, very simple.
Code
#include <iostream> #include <cstring>int mm[101][101],r[101][101]; void lk (int x, int y) { if (!mm[x][y]| | R[x][y]) return; R[x][y]=1; LK (x-1,y-1); All Around, LK (x-1,y+1); LK (x+1,y-1); LK (x+1,y+1); LK (x-1,y); LK (x+1,y); LK (x,y-1); LK (x,y+1);} int main () { int n,i,j,m,ans=0; Char s[101]; scanf ("%d%d", &n,&m); for (i=0;i<n;i++) { memset (s,0,sizeof (s)); scanf ("%s", s); for (j=0;j<m;j++)//Here is the record of the entire map moved, from 0 to 1 if (s[j]== ' @ ') mm[i+1][j+1]=1; else mm[i+1][j+1]=0; } for (i=1;i<=n;i++) for (j=1;j<=m;j++) if (!r[i][j]&&mm[i][j]) { ans++; LK (i,j); } printf ("%d\n", ans);}
20612 statistics eight connected blocks