import java.io.FileInputStream;
import java.util.Scanner;
public class Solution {
static int a[][]=new int[7][8],count;
static boolean visit[][]=new boolean[7][8];
static boolean domino[][];
static void Try(int x,int y,int dem)
{
if(dem==28) {
count++;
return;
}
int nx=0,ny=0;
for(int i=x;i<7;i++){
boolean kt=false;
for(int j=0;j<8;j++)
{
if(visit[i][j]==false){
visit[i][j]=true;
nx=i;
ny=j;
kt=true;
break;
}
}
if(kt) break;
}
if(nx+1< 7){
if(!domino[a[nx][ny]][a[nx+1][ny]] && !visit[nx+1][ny]){
visit[nx+1][ny]=true;
domino[a[nx][ny]][a[nx+1][ny]]=true;
domino[a[nx+1][ny]][a[nx][ny]]=true;
Try(nx,ny,dem+1);
visit[nx+1][ny]=false;
domino[a[nx][ny]][a[nx+1][ny]]=false;
domino[a[nx+1][ny]][a[nx][ny]]=false;
}
}
if(ny+1< 8){
if(!domino[a[nx][ny]][a[nx][ny+1]] && !visit[nx][ny+1]){
visit[nx][ny+1]=true;
domino[a[nx][ny]][a[nx][ny+1]]=true;
domino[a[nx][ny+1]][a[nx][ny]]=true;
Try(nx,ny,dem+1);
visit[nx][ny+1]=false;
domino[a[nx][ny]][a[nx][ny+1]]=false;
domino[a[nx][ny+1]][a[nx][ny]]=false;
}
}
visit[nx][ny]=false;
}
public static void main(String[] args) throws Exception{
System.setIn(new FileInputStream("data"));
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
for(int tc=1;tc<=T;tc++)
{
domino=new boolean[7][8];
visit=new boolean[7][8];
for(int i=0;i<7;i++)
for(int j=0;j<8;j++)
a[i][j]=sc.nextInt();
//================================
count=0;
Try(0,0,0);
System.out.println("#"+tc+" "+count);
}
sc.close();
}
}
Input
5
6 1 6 5 3 2 5 0
6 6 0 1 6 0 4 4
2 2 3 6 5 5 1 5
1 2 0 4 4 3 4 2
5 2 1 1 4 1 3 0
3 3 0 2 3 5 2 6
1 3 4 6 4 5 0 0
6 6 6 0 1 4 6 3
2 5 3 3 3 5 5 4
0 0 4 3 3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
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.