20612 statistics eight connected blocks

Source: Internet
Author: User

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

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.