Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
1.4 kB
1
Indexable
Never
#include<iostream>
using namespace std;

int used[7][7], ans, t, mang[7][8], visit[7][8];

void bt(int r, int c) {
	if (r == 6 && c == 7) {
		ans++;
		return;
	}

	if (!visit[r][c]) {
		if (c < 7 && !visit[r][c + 1]) {
			int num1 = mang[r][c];
			int num2 = mang[r][c + 1];
			if (!used[num1][num2]) {
				visit[r][c] = visit[r][c + 1] = 1;
				used[num1][num2] = used[num2][num1] = 1;
				if (c < 7) {
					bt(r, c + 1);
				}
				else {
					bt(r + 1, 0);
				}
				visit[r][c] = visit[r][c + 1] = 0;
				used[num1][num2] = used[num2][num1] = 0;
			}
		}

		if (r < 6 && !visit[r + 1][c]) {
			int num1 = mang[r][c];
			int num2 = mang[r + 1][c];
			if (!used[num1][num2]) {
				visit[r][c] = visit[r + 1][c] = 1;
				used[num1][num2] = used[num2][num1] = 1;
				if (r < 6) {
					bt(r, c + 1);
				}
				else {
					bt(r + 1, 0);
				}
				visit[r][c] = visit[r + 1][c] = 0;
				used[num1][num2] = used[num2][num1] = 0;
			}
		}
	}
}
void clear() {
	ans = 0;
	for (int i = 0; i < 7; i++) {
		for (int j = 0; j < 8; j++) {
			visit[i][j] = 0;
			used[i][j] = 0;
		}
	}
}
int main() {
	cin >> t;
	for (int tc = 1; tc <= t; tc++) {
		for (int i = 0; i < 7; i++) {
			for (int j = 0; j < 8; j++) {
				cin >> mang[i][j];
			}
		}
		clear();
		bt(0, 0);
		cout << "Case #" << tc << endl;
		cout << ans << endl;
	}
	return 0;
}
Leave a Comment