Untitled
unknown
plain_text
2 years ago
1.9 kB
6
Indexable
package backtrack; import java.awt.Checkbox; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class dominos { static final int n = 7; static final int m = 8; static int arr[][] = new int[n][m]; static boolean visit[][]; static boolean domino[][]; static int count; static void Try(int r, int c) { if (r == 7) { count++; return; } if (!visit[r][c]) { if (c < 7 && !visit[r][c + 1]) { if (!domino[arr[r][c]][arr[r][c + 1]]) { visit[r][c] = visit[r][c + 1] = true; domino[arr[r][c]][arr[r][c + 1]] = domino[arr[r][c + 1]][arr[r][c]] = true; Try(r, c + 1); visit[r][c] = visit[r][c + 1] = false; domino[arr[r][c]][arr[r][c + 1]] = domino[arr[r][c + 1]][arr[r][c]] = false; } } if (r < 6 && !visit[r + 1][c]) { if (!domino[arr[r][c]][arr[r + 1][c]]) { visit[r][c] = visit[r + 1][c] = true; domino[arr[r][c]][arr[r + 1][c]] = domino[arr[r + 1][c]][arr[r][c]] = true; if (c < 7) { Try(r, c + 1); } else { Try(r + 1, 0); } visit[r][c] = visit[r + 1][c] = false; domino[arr[r][c]][arr[r + 1][c]] = domino[arr[r + 1][c]][arr[r][c]] = false; } } } else { if (c < 7) { Try(r, c + 1); } else { Try(r + 1, 0); } } } public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream("Text")); Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); for (int Case = 1; Case <= tc; Case++) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = sc.nextInt(); } } visit = new boolean[n][m]; domino = new boolean[n][n]; count = 0; Try(0, 0); System.out.println("Case #" + Case); System.out.println(count); } } }
Editor is loading...
Leave a Comment