How are we keeping track and printing the path of the visited I did not understand
How are we printing
You can maintain a visited array as, int visited[1000][1000]… and then before traversing, check if you have already visited that path before or not… This should be done to print unique path…
As you reach any node you mark it visited and try to go to next node and reach the last node of the matrix after you reach that node you back track your previous position from where you came with every call a new stack is being generated. You may try to dry run yourself to have a better understanding.