Untitled

 avatar
unknown
plain_text
2 years ago
2.3 kB
5
Indexable
Here is a Java program that uses backtracking to solve the problem you described:

```java
import java.util.Scanner;

public class Main {
    static int[][] board = new int[7][8];
    static boolean[][] used = new boolean[7][8];
    static int[] dx = {0, 1};
    static int[] dy = {1, 0};
    static int count;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int t = 1; t <= T; t++) {
            for (int i = 0; i < 7; i++) {
                for (int j = 0; j < 8; j++) {
                    board[i][j] = sc.nextInt();
                    used[i][j] = false;
                }
            }
            count = 0;
            solve(0);
            System.out.println("Case #" + t);
            System.out.println(count);
        }
    }

    public static void solve(int pos) {
        if (pos == 56) {
            count++;
            return;
        }
        int x = pos / 8;
        int y = pos % 8;
        if (used[x][y]) {
            solve(pos + 1);
            return;
        }
        for (int i = 0; i < 2; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (nx >= 0 && nx < 7 && ny >= 0 && ny < 8 && !used[nx][ny] && board[x][y] == board[nx][ny]) {
                used[x][y] = true;
                used[nx][ny] = true;
                solve(pos + 1);
                used[x][y] = false;
                used[nx][ny] = false;
            }
        }
    }
}
```
This program reads the number of test cases and the board for each test case from standard input. It then uses a recursive backtracking function `solve` to find all possible ways to cover the board with dominos. The function keeps track of the current position on the board and tries to place a domino at that position by checking if it can be placed horizontally or vertically. If a domino can be placed, the function marks the squares as used and calls itself recursively to continue placing dominos on the rest of the board. When all squares on the board have been covered, the function increments a counter to keep track of the number of solutions found.

The output of this program is in the format specified in the problem statement.

I hope this helps! Let me know if you have any questions or need further clarification.
Editor is loading...