GeeksforGeeks » Interview Questions

knight's minimum moves on a chessboard

(4 posts)
  1. realism
    Member
    Posted 6 months ago #

    1) What can be the algorithm for finding the minimum moves path from one box to a destination box in chess board.

  2. kartik
    Moderator
    Posted 6 months ago #

    Looks like a typical dynamic programming problem. Take the destiation coordinates and consider all poits (from source directiom) through which Knight can reach the destination. Now solve for these subproblems (Source to the poits which can reach destinatio)

  3. vasu
    guest
    Posted 6 months ago #

    it should be a little modification for a classic backtracking problem, the Knight's tour problem.
    Here we have two tasks,
    1) Finding out all the paths reaching to the point.
    2) Choosing the minimum path among them
    http://www.geeksforgeeks.org/archives/12916
    See this for the Knight's tour problem, now we can modify it for returning success when we have reached to a particular point, also along with the function, we can keep on counting the number of steps taken, i.e. the cost. Now save it in a global, and while returning success, check if this is minimum or not.
    It can be improved by reducing the number of tracks covered by aborting any path at any moment if the total cost exceeds the current cost.

  4. vasu
    guest
    Posted 6 months ago #

    solution:-

    #include "Knight.h"
    #include<stdio.h>
    #include<stdio.h>
    #include<string.h>
    #define N 8
    int a[N] = {  2, 1, -1, -2, -2, -1,   1,  2};
    int b[N] = {  1, 2,  2,  1,  -1, -2,  -2, -1 };
    int gcost;
    void solution();
    void printSolution(int sol[N][N]);
    bool issafe(int cost, int x, int y, int path[N][N]);
    void solveutil(int path[N][N], int x, int y, int cost, int targetx,int targety);
    int result[N][N];
    void main()
    {
    	solution();
    }
    void solution()
    {
    	int path[N][N] = {0};
    	gcost = 0xFFFF;
    	solveutil(path,0,0,0,6,6);
    	printSolution(result);
    }
    void solveutil(int path[N][N], int x, int y, int cost, int targetx,int targety)
    {
    	int i = 0;
    	cost++;
    	if(x == targetx && y == targety)
    	{
    		if(cost < gcost)
    		{
    			gcost = cost;
    			memcpy(result,path,(sizeof(int)*N*N));
    			result[x][y] = '*';
    		}
    	}
    	for(i = 0; i < N; i++)
    	{
    		x += a[i];
    		y += b[i];
    		if(issafe(cost,x,y,path))
    		{
    			path[x][y] = cost;
    			solveutil(path,x,y,cost,targetx, targety);
    			path[x][y] = 0;
    		}
    		x -= a[i];
    		y -= b[i];
    	}
    }
    bool issafe(int cost, int x, int y, int path[N][N])
    {
    	if( x &&y && x < N && y < N && path[x][y] == 0)
    	{
    		if(cost < gcost)
    		{
    			return true;
    		}
    	}
    	return false;
    }
    void printSolution(int sol[N][N])
    {
    	for (int i = 0; i < N; i++)
    	{
    		for (int j = 0; j < N; j++)
    			if(sol[i][j] == '*')
    				printf("[%02d]",gcost);
    			else
    				printf(" %02d ", sol[i][j]);
    			printf("\n");
    	}
    }
    

    Please check


Reply

You must log in to post.

RSS feed for this topic