#include <stdio.h>
#define SIDE 8
#define VISITED 1
#define NOT_VISITED 0
#define FALSE 0
#define TRUE !FALSE
void printBoard(int board[][SIDE]);
int goHorsie(int board[][SIDE], int x, int y, int step, int cor1, int cor2, int cor3);
int main(void)
{
int board[SIDE][SIDE] = { NOT_VISITED };
int found = 0;
found = goHorsie(board, 0, 0, 1, 0, 0, 0);
if (found)
{
printf("Yes, there is a path from 0,0 through all corners! Here it is:\n");
printBoard(board);
}
else
{
printf("No path from 0,0 through all corners\n");
printBoard(board);
}
getchar();
return 0;
}
int goHorsie(int board[][SIDE], int x, int y, int step, int cor1, int cor2, int cor3)
{
int res = FALSE, check = 1;
if (board[x][y] != NOT_VISITED //We were here already!
|| x >= SIDE || y >= SIDE || x < 0 || y < 0)
{
res = FALSE;
check = 0;
}
if (x == 7 && y == 7)
{
printf("1)found!\n");
cor1 = 1;
}
if (x == 7 && y == 0)
{
printf("2)found!\n");
cor2 = 1;
}
if (x == 0 && y == 7)
{
printf("3)found!\n");
cor3 = 1;
}
if (cor1 == 1 && cor2 == 1 && cor3 == 1)
{
printf("FOUND ALL!\n");
return TRUE;
}
else if(check == 1)
{
board[x][y] = step;
step++;
res =
goHorsie(board, x + 1, y - 2, step, cor1, cor2, cor3) ||
goHorsie(board, x + 2, y + 1, step, cor1, cor2, cor3) ||
goHorsie(board, x + 2, y - 1, step, cor1, cor2, cor3) ||
goHorsie(board, x + 1, y + 2, step, cor1, cor2, cor3) ||
goHorsie(board, x - 2, y + 1, step, cor1, cor2, cor3) ||
goHorsie(board, x - 2, y - 1, step, cor1, cor2, cor3) ||
goHorsie(board, x - 1, y + 2, step, cor1, cor2, cor3) ||
goHorsie(board, x + 1, y - 2, step, cor1, cor2, cor3);
if (!res)
{
board[x][y] = NOT_VISITED;
}
}
return res;
}
void printBoard(int board[][SIDE])
{
int i = 0, j = 0;
for (int i = 0; i < SIDE; i++)
{
for (int j = 0; j < SIDE; j++)
{
printf("%3d", board[i][j]);
}
printf("\n");
}
}
I'm using recursion to find the path to all 3 corners.
I ran the program for about 20min now and it's still didn't get to the solution.
Ik why its taking too long but not sure if it will even get me to the answer, and I think it's looping forever.
So my question is did I make the function right and will it eventually give me the right answer (the path to all 3 corners), or what do I need to change in order to get to the answer.
What I mean by 3 corners is: top right, bottom right and bottom left.