UVA 572-Oil deposits (Dfs find connectivity block + seed fill algorithm)

Source: Internet
Author: User

UVA 572-Oil deposits (Dfs seek connectivity block)

The diagram also has DFS and BFS traversal, because DFS is better written, so it is common to look for connectivity blocks with DFS.

The following code uses a double loop to find the adjacent 8 squares of the current lattice, or to use a constant array or write 8 DFS calls.

The following algorithm is: seed Fill (FloodFill)

  Two connected areas

Four connected areas: from a point in the region, you can move in the upper, lower, left, and right four directions of the mobile combination, on the premise of no more out of the region, can reach any pixel within the region

Eight connected areas: from each pixel in the region, you can go through eight directions, that is, upper, lower, left, right, top left, right, left, lower, right down the combination of moving, in the case of no more out of the region, can reach any pixel within the region.

Fundamentals

Starting from a pixel within the polygon area (called a seed), all other pixels within the region are found.

the boundary definition used

All pixels on the area boundary have a particular color value, and all pixels inside the area do not take that particular color, while pixels outside the boundary can have the same color value as the boundary.

the execution process of the algorithm:

Starting with (x, y), the color of the point is detected first, and if it is different from the boundary and fill colors, the point is filled with a fill color. Adjacent locations are then detected to determine whether they are boundary and fill colors, or not, to populate the adjacent points. Until all the pixels in the area boundary range are detected.
Methods for detecting neighboring pixels from the current point: four-or eight-connected
Looking for the next pixel in four directions, called a four-way algorithm (only four connected regions can be filled);
Look for the next pixel in eight directions, called an eight-way algorithm (you can fill eight connected areas and four connected regions).

A seed-filling recursive algorithm for four-connected regions:

1 voidZhongZiTC4 (intSeedx,intSeedy,intFcolor,intBcolor)2 {    3      intCurrent =GetPixel (Seedx, seedy);4      if(Current! = Bcolor) && (Current! =fcolor))5 {putpixel (Seedx, seedy, fcolor);6ZhongZiTC4 (seedx+1, seedy, Fcolor, Bcolor);//Right7ZhongZiTC4 (seedx–1, seedy, Fcolor, Bcolor);//left8ZhongZiTC4 (Seedx, seedy+1, Fcolor, Bcolor);//on9ZhongZiTC4 (Seedx, seedy–1, Fcolor, Bcolor);//underTen      } One}

UVA 572 Code:

1#include <iostream>2#include <cstring>3 using namespacestd;4 Const intMAXN = -+5;5 CharDEPOSITS[MAXN][MAXN];6 intID[MAXN][MAXN];7 intrm,cm;8 voidDfsintRintCintCNT)9 {Ten     ///Judging if it's out of bounds One     if(r<0|| R>=rm | | c<0|| C&GT;=CM)return; A     ///Critical Conditions -     if(Deposits[r][c] = ='*')return; -     if(Id[r][c])return; the  -ID[R][C] =CNT; -      for(inti=-1; i<=1; i++) -          for(intj=-1; j<=1; j + +) +             if(i!=0|| j!=0) DFS (r+i,c+j,cnt); -  + } A  at intMain () - { -  -      while(CIN&GT;&GT;RM&GT;&GT;CM && RM &&cm) -     { -          for(intI=0; i<rm;i++) cin>>Deposits[i]; in         intCnt=0; -memset (ID,0,sizeof(ID)); to          for(intI=0; i<rm;i++) +              for(intj=0; j<cm;j++) -             { the                 if(!id[i][j] && deposits[i][j]=='@')///No numbering *DFS (i,j,++CNT); $             }Panax Notoginsengcout<<cnt<<Endl; -  the     } +     return 0; A}

UVA 572-Oil deposits (Dfs find connectivity block + seed fill algorithm)

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.