Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.6 kB
1
Indexable
Never
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Solution {
	static int[][] B = new int[7][8];
	static int[][] D, visit;
	static int count, k;
	static int[] dx = { 0, 1 };
	static int[] dy = { 1, 0 };

	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		System.setIn(new FileInputStream("src/input.txt"));
		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++) {
					B[i][j] = sc.nextInt();
				}
			}

			D = new int[7][7];
			visit = new int[7][8];
			count = 0;
			k = 0;
			play(k);
			System.out.println("#"+t+" "+ count);
		}
		sc.close();
	}

	private static void play(int k) {
		// TODO Auto-generated method stub
		if (k == 56) {
			count++;
			return;
		}

		int r = k / 8;
		int c = k % 8;

		if (visit[r][c] == 0) {
			for (int i = 0; i < 2; i++) {
				int nr = r + dx[i];
				int nc = c + dy[i];

				if (nr >= 0 && nr < 7 && nc >= 0 && nc < 8 && visit[nr][nc]==0) {
					int x = B[r][c];
					int y = B[nr][nc];
					if (x <= y && D[x][y] == 0) {
						D[x][y] = 1;
						visit[r][c] = visit[nr][nc] = 1; 
						play(k+1);
						D[x][y] = 0;
						visit[r][c] = visit[nr][nc] = 0; 
					}
					else if(x > y && D[y][x] == 0) {
						D[y][x] = 1;
						visit[r][c] = visit[nr][nc] = 1; 
						play(k+1);
						D[y][x] = 0;
						visit[r][c] = visit[nr][nc] = 0; 
					}

				}

			}

		} else {
			play(k + 1);
		}
	}
}