Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
#include <stdio.h>
int N, M;

bool selected[7][7];

int map[7][8];
bool visited[7][8];

int cnt;

void go(int pos) {
	if (pos == 56) {
		cnt++;
		return;
	}
	int r = pos / M;
	int c = pos % M;

	if (visited[r][c]) {
		go(pos + 1);
		return;
	}
	visited[r][c] = true;
	
	if (r < N - 1 && !visited[r + 1][c] && !selected[map[r][c]][map[r + 1][c]]) {
		selected[map[r][c]][map[r + 1][c]] = selected[map[r + 1][c]][map[r][c]] = true;
		visited[r + 1][c] = true;
		go(pos + 1);
		visited[r + 1][c] = false;
		selected[map[r][c]][map[r + 1][c]] = selected[map[r + 1][c]][map[r][c]] = false;
	}
	if (c < M - 1 && !visited[r][c + 1] && !selected[map[r][c]][map[r][c + 1]]) {
		selected[map[r][c]][map[r][c + 1]] = selected[map[r ][c + 1]][map[r][c]] = true;
		visited[r][c + 1] = true;
		go(pos + 1);
		visited[r][c + 1] = false;
		selected[map[r][c]][map[r][c + 1]] = selected[map[r ][c + 1]][map[r][c]] = false;
	}
	visited[r][c] = false;
}

int main() {
	int T;
	N = 7; M = 8;
	scanf("%d", &T);
	for (int tc = 1; tc <= T; tc++) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				visited[i][j] = false;
				scanf("%d", &map[i][j]);
			}		
		}
		for (int i = 0; i < 7; i++)
			for (int j = 0; j < 7; j++)
				selected[i][j] = false;
		cnt = 0;
		go(0);
				
		printf("Case #%d\n%d\n", tc, cnt);
	}

	return 0;
}