#include <stdio.h>
int arr[25][25], row[25], col[25];
struct Pos{
    int r, c;
};

int f(Pos s, Pos e, bool rc){
    int cnt = 0;
    bool c = 1;
    for(int i=s.r; i<=e.r; i++){
        for(int j=s.c; j<=e.c; j++){
            if(arr[i][j] == 1){
                c = 0;
                break;
            }
            if(arr[i][j] == 2) cnt++;
        }
    }
    if(c){
        if(cnt == 0) return 0;
        else if (cnt == 1) return 1;
    }
    int res = 0;
    for(int i=s.r; i<=e.r; i++){
        for(int j=s.c; j<=e.c; j++){
            if(arr[i][j] == 1){
                bool check = 1;
                if(rc){
                    for(int c=s.c; c<e.c; c++){
                        if(arr[i][c]!=2) continue;
                        check = 0;
                        break;
                    }
                    if(check) res += f(s, {i-1 , e.c}, !rc) * f({i+1, s.c}, e, !rc);
                }
                else{
                    for(int r=s.r; r<e.r; r++){
                        if(arr[r][j]!=2) continue;
                        check = 0;
                        break;
                    }
                    if(check) res += f(s, {e.r , j-1}, !rc) * f({s.r, j+1}, e, !rc);
                }
            }
        }
    }
    return res;
}

int main(){
	int N;
	scanf("%d", &N);
	for(int i=1; i<=N; i++){
        for(int j=1; j<=N; j++){
            scanf("%d", &arr[i][j]);
            row[i]=col[i]=1;
        }
	}
	int cnt = 0;
    cnt = f({1, 1}, {N, N}, 0) + f({1, 1}, {N, N}, 1);
	if(cnt) printf("%d", cnt);
	else printf("-1");
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: