knights tour.

Hey guys, I'm trying to solve knights tour problem. but after all my knight makes only 42 moves out of 64. maybe u can see where is the problem?

// knights_tour.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
using namespace std;

const int rows = 8;
const int columns = 8;
void printArray(int[rows][columns]);

int main()
{	
	int board[rows][columns] = {0};
	int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
	int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};
	int counter = 1;
	int currentColumn = 4;
	int currentRow = 3;
	int pastColumn, pastRow;
	int moveNumber = 0;

	printArray(board);
	cout << endl;
	
	

	for (int i = 1; i < 65; i++)
	{
		pastRow = currentRow;
		pastColumn = currentColumn;
		currentRow += horizontal[moveNumber];
		currentColumn += vertical[moveNumber];

		if (board[currentRow][currentColumn] != 0)
		{
				currentRow = pastRow;
				currentColumn = pastColumn;
				moveNumber++;
		}
		else
		{
			board[currentRow][currentColumn] = counter;
			counter++;
			moveNumber = 1;
		}
	};

	cout << counter << "\n";
	cout << endl;
	printArray(board);


	system ("PAUSE");
	return 0;
};

void printArray(int a[rows][columns])
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < columns; j++)
			cout << a[i][j] << ' ';
			
		cout << endl;
	}
}
Not sure about the knights problem, but your column value is going into negative in the 4th iteration. Check after correcting it.
Topic archived. No new replies allowed.