#include<stdio.h> #include<assert.h> //#define N 7 //#define ALLOWED_CHANGES 1 int X[N][N][N][N]; long cnt = 0; inline int test_valid(int a,int b,int c,int d) { int i,j,k,l,m; int scenario=0; for(int e=0;e<d;e++) { if(e == a || e == b || e == c || e == d) continue; if(e<a) { i = e; j = a; k = b; l = c; m = d; scenario = 1; } else { if(e<b) { i = a; j = e; k = b; l = c; m = d; scenario = 2; } else { if(e<c) { i = a; j = b; k = e; l = c; m = d; scenario = 3; } else // e<d since e is maximal element { i = a; j = b; k = c; l = e; m = d; scenario = 4; } } } int s1 = X[i][j][k][l]; int s2 = X[i][j][k][m]; int s3 = X[i][j][l][m]; int s4 = X[i][k][l][m]; int s5 = X[j][k][l][m]; if(s1 == 0 || s2 == 0 || s3 == 0 || s4 == 0 || s5 == 0) continue; int changes = 0; if(s1*s2 < 0) changes++; if(s2*s3 < 0) changes++; if(s3*s4 < 0) changes++; if(s4*s5 < 0) changes++; if(changes > ALLOWED_CHANGES) return 0; } return 1; } inline void display() { cnt++; printf("#%ld: ",cnt); for(int a=0;a<N;a++) { for(int b=a+1;b<N;b++) { for(int c=b+1;c<N;c++) { for(int d=c+1;d<N;d++) { printf(X[a][b][c][d] > 0 ?"+":"-"); } } } } printf("\n"); } void rec_fill2(int a,int b,int c,int d) { if(a == b) { a = 0; b++; if(b == c) { b = 1; c++; if(c == d) { c = 2; d++; } } } if(d == N) { display(); } else { for(int v=-1;v<=+1;v+=2) { X[a][b][c][d] = v; if(test_valid(a,b,c,d)) rec_fill2(a+1,b,c,d); X[a][b][c][d] = 0; } } } void rec_fill1(int a,int b,int c,int d) { if(d == N) { c++; d = c+1; if(d == N) { b++; c = b+1; d = c+1; if(d == N) { a++; b = a+1; c = b+1; d = c+1; if(d == N) { display(); return; } } } } { for(int v=-1;v<=+1;v+=2) { X[a][b][c][d] = v; if(test_valid(a,b,c,d)) rec_fill1(a,b,c,d+1); X[a][b][c][d] = 0; } } } void init_zeros() { for(int a=0;a<N;a++) { for(int b=a+1;b<N;b++) { for(int c=b+1;c<N;c++) { for(int d=c+1;d<N;d++) { X[a][b][c][d] = 0; } } } } } int main() { init_zeros(); rec_fill2(0,1,2,3); return 0; }