Code:

#include <stdlib.h>
#include <stdio.h>
struct coordinates{
int moves;
int row;
int column;
struct coordinates* next;
struct coordinates* previous;
}coordinates;
int gridSize,C1,C2,R1,R2,amountOfMoves;
int queueAmount=0;
struct coordinates* queue;
struct coordinates* queueEnd;
void queuePosibleMoves(int,int,int);
void startDequeingCheck();
main(){
int i,amountOfTestCases;
FILE* fp;
FILE* fout;
fp= fopen("goldknight.in","r");
fout = fopen("goldknight.out","w");
fscanf(fp,"%i",&amountOfTestCases);
for(i=0;i<amountOfTestCases;i++){
fscanf(fp,"%i",&gridSize);
fscanf(fp,"%i",&R1);
fscanf(fp,"%i",&C1);
fscanf(fp,"%i",&R2);
fscanf(fp,"%i",&C2);
queue = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd = queue;
queuePosibleMoves(R1,C1,1);
printf("Case #%i: %i\n\n",i+1,amountOfMoves);
queueAmount=0;
}
return 0;
}
void queuePosibleMoves(int currentRow,int currentColumn,int moves){
struct coordinates* temp;
struct coordinates* temp2;
int p=0;
if(queueAmount >1){
temp2 = queue;
queue = queue->next;
//free(temp2);
}
// This will do all 8 possible ways
//creates the queue if there is no queue
if(queueEnd == queue){
queue->moves = moves;
queue->row = currentRow-2;
queue->column = currentColumn-1;
temp = (struct coordinates*)malloc(sizeof(coordinates));
queue->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
p = 1;
queueAmount++;
}
//p is the indicator that the queue does exist
if(p == 0){
if(currentRow-2 > 0 && currentRow-2 <= gridSize && currentColumn-1 > 0 && currentColumn-1 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow-2;
queueEnd->column = currentColumn-1;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
}
if(currentRow-2 > 0 && currentRow-2 <= gridSize && currentColumn+1 > 0 && currentColumn+1 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow-2;
queueEnd->column = currentColumn+1;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
if(currentRow+2 > 0 && currentRow+2 <= gridSize && currentColumn-1 > 0 && currentColumn-1 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow+2;
queueEnd->column = currentColumn-1;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
if(currentRow+2 > 0 && currentRow+2 <= gridSize && currentColumn+1 > 0 && currentColumn+1 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow+2;
queueEnd->column = currentColumn+1;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
if(currentRow-1 > 0 && currentRow-1 <= gridSize && currentColumn-2 > 0 && currentColumn-2 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow-1;
queueEnd->column = currentColumn-2;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
if(currentRow+1 > 0 && currentRow+1 <= gridSize && currentColumn-2 > 0 && currentColumn-2 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow+1;
queueEnd->column = currentColumn-2;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
if(currentRow-1 > 0 && currentRow-1 <= gridSize && currentColumn+2 > 0 && currentColumn+2 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow-1;
queueEnd->column = currentColumn+2;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
if(currentRow+1 > 0 && currentRow+1 <= gridSize && currentColumn+2 > 0 && currentColumn+2 <= gridSize){
temp = (struct coordinates*)malloc(sizeof(coordinates));
queueEnd->moves = moves;
queueEnd->row = currentRow+1;
queueEnd->column = currentColumn+2;
queueEnd->next = temp;
queueEnd->next->previous = queueEnd;
queueEnd = temp;
queueAmount++;
}
startDequeingCheck();
}
void startDequeingCheck(){
queueAmount--;
//checks if it's inside the boundaries
if(queue->row >0 && queue->column >0 && queue->row <=gridSize && queue->column <=gridSize){
//checks if it has found the final position
if(queue->row == R2 && queue->column == C2){
amountOfMoves = queue->moves;
return;
}
//else enqueue all the posibilities of the current position
queuePosibleMoves(queue->row,queue->column,queue->moves+1);
}else{
startDequeingCheck();
}
}