I can't think of base case. Please explain

since you are calculation number of number of cells that have been marked at the base condition taking using double loop is quite inefficient , also instead what you can do is to update the count at every function call
refer this for more clarity (i have commented some necessary part ) :-

Can you explain how is this code is working?

@mb129162, i assume that the question is clear to you so , 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

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 11 : ans=0; (useless line . ignore)

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:

Why you have used ans=0 ?

it doesn’t do anything i forgot to delete it