painting
#include <iostream> using namespace std; int n; int a[100][100]; int visit[100]; int ans; bool checkDiffColor(int step, int color) { for (int j = 1; j <= n; j++) { if (a[step][j] == 1 && color == visit[j]) { return false; } } return true; } void Pain(int step) { if (step > n) { ans++; return; } else { for (int color = 1; color <= 4; color++) { if (visit[step] == 0) { if (checkDiffColor(step, color)) { visit[step] = color; Pain(step + 1); visit[step] = 0; } } } } } void reset() { for (int i = 1; i <= 100; i++) { visit[i] = 0; } } void solve(int testcase) { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } reset(); ans = 0; Pain(1); cout << "Case #" << testcase << endl; cout << ans << endl; } int main() { int t; cin >> t; for (int i = 1; i <= t; i++) { solve(i); } return 0; }
Leave a Comment