Here is a systematic way to count the number of triangulations of your “blocked‐side” polygon in time O(n³) (and in particular to get the final number when all four sides have the same length k, so n=4k). The idea is simply to run the usual “interval‐DP” for counting non‐crossing triangulations, but forbid any diagonal whose both endpoints lie on the same square side.
1. Labeling and side‐membership
- Arrange your n=4k vertices around a circle and label them 0,1,2,…,n−1 clockwise.
- Declare that side 1 consists of vertices
0,1,…,k−1; side 2 of k,…,2k−1; side 3 of 2k,…,3k−1; side 4 of 3k,…,4k−1.
- We will never allow a diagonal to connect two vertices that both lie in the same side‐interval above.
2. Interval‐DP setup
Define
DP[i][j] = # of valid triangulations of the polygonal chain (i,i+1,…,j)
together with the chord (j,i) closing it—subject to the rule
“no diagonal may connect two vertices of the same side.”
We interpret indices modulo n, but for implementation you usually unwrap so that 0≤i<j<n and treat the interval i..j as going clockwise without wraparound.
Base case
Any interval of length 1 (i.e. j = i+1) has exactly one way to “triangulate” (it’s just an edge), so
DP[i][i+1] = 1.
Recurrence
For j ≥ i+2,
DP[i][j]
= ∑_{k = i+1 to j-1}
[ diagonal (i,k) is allowed AND diagonal (k,j) is allowed ]
· DP[i][k] · DP[k][j].
Here
- “diagonal (i,k) is allowed” means that i and k do not both lie in the same one of the four side‐intervals;
- similarly for (k,j).
This is exactly the usual Catalan‐style triangulation recurrence, restricted by a predicate that rejects diagonals inside the same side.
3. Implementation sketch
def count_triangulations(n, side_of):
# side_of[v] in {1,2,3,4} tells which square‐side v belongs to.
DP = [[0]*n for _ in range(n)]
for i in range(n-1):
DP[i][i+1] = 1
# consider all interval lengths ℓ = 2..n-1
for ℓ in range(2, n):
for i in range(n-ℓ):
j = i + ℓ
total = 0
for k in range(i+1, j):
# check if we can draw (i,k) and (k,j)
if side_of[i] != side_of[k] and side_of[k] != side_of[j]:
total += DP[i][k] * DP[k][j]
DP[i][j] = total
return DP[0][n-1]
To specialize to the case n=4k with k points on each side, just build
side_of[v] = 1 + (v // k) # integer division: 0..k-1→1, k..2k-1→2, 2k..3k-1→3, 3k..4k-1→4
and then call count_triangulations(4*k, side_of).
4. Complexity
- You fill in an n×n table (n=4k)
- For each interval (i,j) you do an O(n) sum over k
→ total time O(n³) = O(k³).
For moderate k this is perfectly fine, and it directly returns the exact integer count of all triangulations avoiding “same‐side” chords.
5. Example
Suppose k=2 so n=8 and each side has 2 vertices. Running the above DP you get
- Total triangulations of an octagon without any restriction = Catalan(6)=132.
- With the “no‐same‐side‐diagonals” rule one finds
DP[0][7] = 38.
You can verify by brute‐force enumeration for k=2,3,4 that your DP agrees.
That’s it! This interval‐DP is the standard way to count non‐crossing structures subject to local “forbidden‐diagonal” conditions, and in your case it runs in O((4k)³)=O(k³).