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>=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>>RM>>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)