Funky Chess Code Explanation

I Understood the problem but unable to do the Code , Please Provide the Code Related to it.

@saurabhvit, what we have to do is find the minimum number of cells that the knight can not reach under the constraint that the knight may not move to any square that is not on the board and cannot rest in any spot more than once . here i am trying to find the maximum number of squares knight can visit in one go and the i will subtract it with total number of cells

I am solving this using recursion with backtracking . once you visit a cell, then visit all the cells that can be visited from this cell and also visit the cells that can be further visited from the next reachable cells. and keep tracking the number of cells you have visited in one traversal until there is no cell that you can visit further ,also keep marking the visited cell so that you don;t visit those cells again

refer this code :-

below is the explaination of every line of code :-
So this is how i am implementing this logic
set(i,j,count) : this is the state of function when i have reached position i,j from 0,0 by following some path and count denotes the number of cells i have visited in the path (not including the current sqare)

line 9-10 : **
if(i<0 || i>=10 || j<0 || j>=10 || board[i][j] == 0)
** return;

ensures that we are not visiting any cell that is out of bounds or have been visited before

line 12 : board[i][j] = 0; : this is done to denote that this cell can’t be visited from now onwards
with this we are ensuring that the knight will not visit this cell again while traversing the path

line 13: hi = max(hi,count+1); , here hi , stores our answer i.e the maximum cells that can be visited in one go under the given constraint and count+1 is the count of cells i have visited including current cell,in the path traversed by the knight from (0,0) to (i,j) and if this value is greater than hi , then hi is updated
line 15-22 :
set(i-1,j-2,count+1);
** set(i-2,j-1,count+1);**
** set(i+1,j-2,count+1);**
** set(i+2,j-1,count+1);**
** set(i-1,j+2,count+1);**
** set(i-2,j+1,count+1);**
** set(i+1,j+2,count+1);**
** set(i+2,j+1,count+1);**
these are the possible position of cells that our knight can visit from (i,j) in one jump ,notice that we are increasing count by 1 to include cell i,j in the count
line 23 : board[i][j] = 1 Backtracking

In case you still have any doubt feel free to ask :slight_smile:

Just Explain me the need of back tracking here.

basically what your goal is to traverse different paths and find the longest path , you are keeping track of path visited by setting board[i][j] = 0 , you need to backtrack because if you don’t then you wouldn’t be able to traverse the cells that may have been before in some iterations , this might be little confusing to understand, the reasoning for using backtracking in this question is same as in rat in a maze, or n queen problem
If you have’nt done these problem then i would suggest you to attempt them first.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.