/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
public class Main
{
static int count=0;
public static void knight(boolean[][] box,int tk,int kpsf,int row,int col,String result)
{
if(kpsf==tk)
{
count++;
System.out.println(count+" "+result);
return;
}
if(col==box[0].length)
{
row=row+1;
col=0;
}
if(row==box.length)
{
return;
}
if(isItSafe(box, row, col))
{
box[row][col]=true;
knight(box,tk,kpsf+1,row,col+1,result+"row"+row+"-"+"col"+col+ " ");
box[row][col]=false;
}
knight(box,tk,kpsf,row,col+1,result);
}
public static boolean isItSafe(boolean[][] box,int row,int col)
{
int[] rows= {-1,-2,-2,-1};
int[] cols= {2,1,-1,-2};
for(int i=0;i<4;i++)
{
row=row+rows[i];
col=col+cols[i];
if(row>=0 && row<box.length && col>=0 && col<box[0].length)
{
if(box[row][col])
{
return false;
}
}
}
return true;
}
public static void main(String[] args) {
boolean[][] hh=new boolean[4][4];
knight(hh,hh.length,0,0,0,"");
}
}