Simplified Province

I need some smart and fairly simple solution to my problem - the formation of the province. Suppose the mapping is an NxM matrix. Each cell is represented by a natural number. 0 means that the tile does not belong to any province. the number 1 means that it belongs to the region nr 1, nr 2 means that the cell belongs to the province nr 2 ... etc.

Consider this mapping, which is 4x4:

0000
0000
0000
0000

This map represents 16 tiles that do not belong to any province.

This is a map containing 1 province:

0010
0111
0100
0000

it is a province of size 5 and id = 1. It has no neighbors.

Consider 3 provinces:

1133
2100
2200
2000

So, province 1 is neighbor 2 and 3. Province 3 is only neighbor 1, and province 2 is only neighbor 1. There are also 7 unconnected tiles.

: k NxN. :

  • (, min = 2, max = 10)
  • ( , ).

( ):

1100
0000
0011
0000
  • ( )

, . . 300x300 200 , .

+3
3

, , "".

. .

:

1) . , , .

alt text

2) ( , , )

alt text

3)

4) , > ( ). , 1

5) , , , , > ( )

6) , < ( ). , .

7)

  • > ( )

, Mathematica, :

 Clear["Global`*"];
 Needs["ComputationalGeometry`"];
 data2D = Table[{RandomReal[16], RandomReal[16]}, {10}]
 convexhull = ConvexHull[data2D]
 (delval = DelaunayTriangulation[data2D]) // Shallow[#, {5, 6}] &
 b1 = {{0, 0}, {16, 0}, {16, 16}, {0, 16}};
 {diagvert1, diagval1} = BoundedDiagram[b1, data2D, delval, convexhull];
 Show[{Graphics[Join[{PointSize[Large]}, {Point@data2D}], Frame -> True]}]
 Show[{Graphics@Point[data2D],   DiagramPlot[data2D, diagvert1, diagval1]}]

, , .

. , ...

+5

, : , .

 1111222
 3333322
 3344555        
 0000665

.. ?

void insert(Matrix matrix){
    lastProvince=0;
    missingProvince=MIN;
    if(matrix.dimensio<MIN*K) throw new RuntimeException("Matrix too small");
    for(y=0;y<matrix.height;y++){
        if(y%2==0){
            for(x=0;x<matrix.width;x++){
                matrix[x][y]=lastProvince;
                missingProvince--;
                if(missingProvince==0) {
                    lastProvince++;
                    missingProvince=MIN;
                }
                if(lastProvince==k) return;
            }
        }else{
            for(x=matrix.width;x>=0;x--){// is -- not ++
                matrix[x][y]=lastProvince;
                missingProvince--;
                if(missingProvince==0) {
                    lastProvince++;
                    missingProvince=MIN;
                }
                if(lastProvince==k) return;
            }
         }
   }
}

, ..

+2

:

  • "" , . NxM L , L [0-(N*M-1)]. X,Y P P/M P%M.
  • :

    . , ( , ).

    . , - .

There is little chance of enclaves with this technique, but it is very low. You can improve step b to test them and not grow if growth will create.

There is also the possibility that provinces will remain too small if they become completely surrounded by other provinces before they themselves can grow large enough. You can check and configure for this after completing step 2.

+2
source

Source: https://habr.com/ru/post/1772453/


All Articles