```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Point
{
public int x, y;
public Point()
{
x = 0;
y = 0;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Program
{
static void Main(string[] args)
{
int[,] chessBoard = new int[8, 8];
string start = "a1";
string end = "h8";
Point startPoint = new Point();
startPoint = GetCoordinate(start);
Point endPoint = new Point();
endPoint = GetCoordinate(end);
int numberOfMoves = 0;
while (startPoint.x != endPoint.x || startPoint.y != endPoint.y)
{
chessBoard[startPoint.x, startPoint.y] = -1;
Point[] jumpStart = PossibleJumps(startPoint, chessBoard);
Point[] jumpEnd = PossibleJumps(endPoint, chessBoard);
if (CheckDestination(jumpStart, endPoint))
{
numberOfMoves++;
break;
}
else if (CheckCommon(jumpStart, jumpEnd))
{
numberOfMoves += 2;
break;
}
else
{
int distance = 0, position = 0;
for (int i = 0; i < jumpStart.Length; i++)
{
if (i == 0)
{
distance = GetDistance(jumpStart[i], endPoint);
position = i;
}
else
{
if (distance > GetDistance(jumpStart[i], endPoint))
{
distance = GetDistance(jumpStart[i], endPoint);
position = i;
}
}
}
startPoint = jumpStart[position];
numberOfMoves++;
}
}
Console.WriteLine("Minimum number of moves = " + numberOfMoves);
}
public static Point GetCoordinate(string str)
{
int x = 0, y = 0;
switch (str[0])
{
case 'a':
x = 0;
break;
case 'b':
x = 1;
break;
case 'c':
x = 2;
break;
case 'd':
x = 3;
break;
case 'e':
x = 4;
break;
case 'f':
x = 5;
break;
case 'g':
x = 6;
break;
case 'h':
x = 7;
break;
}
y = int.Parse(str[1].ToString()) - 1;
Point P = new Point(x, y);
return P;
}
public static int GetDistance(Point P1, Point P2)
{
return (Math.Abs(P1.x - P2.x) + Math.Abs(P1.y - P2.y));
}
public static Point[] PossibleJumps(Point P, int[,] chessBoard)
{
ArrayList xArray = new ArrayList();
ArrayList yArray = new ArrayList();
//Point 1
if (P.x - 1 >= 0 && P.y + 2 <= 7)
{
if (chessBoard[P.x - 1, P.y + 2] != -1)
{
xArray.Add((int)(P.x - 1));
yArray.Add((int)(P.y + 2));
}
}
//Point 2
if (P.x + 1 <= 7 && P.y + 2 <= 7)
{
if (chessBoard[P.x + 1, P.y + 2] != -1)
{
xArray.Add((int)(P.x + 1));
yArray.Add((int)(P.y + 2));
}
}
//Point 3
if (P.x + 2 <= 7 && P.y + 1 <= 7)
{
if (chessBoard[P.x + 2, P.y + 1] != -1)
{
xArray.Add((int)(P.x + 2));
yArray.Add((int)(P.y + 1));
}
}
//Point 4
if (P.x + 2 <= 7 && P.y - 1 >= 0)
{
if (chessBoard[P.x + 2, P.y - 1] != -1)
{
xArray.Add((int)(P.x + 2));
yArray.Add((int)(P.y - 1));
}
}
//Point 5
if (P.x + 1 <= 7 && P.y - 2 >= 0)
{
if (chessBoard[P.x + 1, P.y - 2] != -1)
{
xArray.Add((int)(P.x + 1));
yArray.Add((int)(P.y - 2));
}
}
//Point 6
if (P.x - 1 >= 0 && P.y - 2 >= 0)
{
if (chessBoard[P.x - 1, P.y - 2] != -1)
{
xArray.Add((int)(P.x - 1));
yArray.Add((int)(P.y - 2));
}
}
//Point 7
if (P.x - 2 >= 0 && P.y - 1 >= 0)
{
if (chessBoard[P.x - 2, P.y - 1] != -1)
{
xArray.Add((int)(P.x - 2));
yArray.Add((int)(P.y - 1));
}
}
//Point 8
if (P.x - 2 >= 0 && P.y + 1 <= 7)
{
if (chessBoard[P.x - 2, P.y + 1] != -1)
{
xArray.Add((int)(P.x - 2));
yArray.Add((int)(P.y + 1));
}
}
Point[] pArray = new Point[xArray.Count];
for (int i = 0; i < pArray.Length; i++)
{
pArray[i] = new Point();
pArray[i].x = (int)xArray[i];
pArray[i].y = (int)yArray[i];
}
return pArray;
}
public static bool CheckCommon(Point[] P1, Point[] P2)
{
for (int i = 0; i < P1.Length; i++)
{
for (int j = 0; j < P2.Length; j++)
{
if (P1[i].x == P2[j].x && P1[i].y == P2[j].y)
{
return true;
}
}
}
return false;
}
public static bool CheckDestination(Point[] P1, Point P)
{
for (int i = 0; i < P1.Length; i++)
{
if (P1[i].x == P.x && P1[i].y == P.y)
{
return true;
}
}
return false;
}
}
}
```

Output
=======
Minimum number of moves = 6
Press any key to continue . . .

Advertisements
(function(g,$){if("undefined"!=typeof g.__ATA){
g.__ATA.initAd({collapseEmpty:'after', sectionId:26942, width:300, height:250});
g.__ATA.initAd({collapseEmpty:'after', sectionId:114160, width:300, height:250});
}})(window,jQuery);
var o = document.getElementById('crt-1073668447');
if ("undefined"!=typeof Criteo) {
var p = o.parentNode;
p.style.setProperty('display', 'inline-block', 'important');
o.style.setProperty('display', 'block', 'important');
Criteo.DisplayAcceptableAdIfAdblocked({zoneid:388248,containerid:"crt-1073668447",collapseContainerIfNotAdblocked:true,"callifnotadblocked": function () {var o = document.getElementById('crt-1073668447'); o.style.setProperty('display','none','important');o.style.setProperty('visbility','hidden','important'); }
});
} else {
o.style.setProperty('display', 'none', 'important');
o.style.setProperty('visibility', 'hidden', 'important');
}