[ create a new paste ] login | about

Link: http://codepad.org/XZ9k9V0S    [ raw code | output | fork ]

gauravalgo - C, pasted on Jun 22:
/*well i am back with re-creating my work which i have done in my past*/
/*here is the ********KNIGHT -TOUR*********problem in which you have to place the */
/*knight anywhere at the chess board and the traverse the whole chessboard once*/
/*i have only given solutions but problem have been availaible you to http://en.wikipedia.org/wiki/Knight's_tour*/
/*for finding out the solution you have to just put values in the main block*/
/*of row=?,col=? then you have to trace it from last code run may be it can produce */
/*solution because its running complexity is about 10^8 so its your luck at 8x8 board*/ 
#include<stdio.h>
int a[100][100];
int move[8][2]={1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1, -2,1, -1,2};
int ccol=5;

void
show()
{
	
	int k,j;
	//printf("\n");
		for(k=0;k<ccol;k++)
		{
			for(j=0;j<ccol;j++)
			{
				printf("%d ",a[k][j]);
			}
		printf("\n");
		}
}

int 
knightmove(int row,int col,int counter)
{
	
	int tempc,tempr,r,i;
	tempc=col;
	tempr=row;
	if(counter==(ccol*ccol)-1)
	{
		show();
		return 2;
	}
	counter=counter+1;
	a[tempr][tempc]=1;
	for(i=0;i<8;i++)
	{
		tempr=row+move[i][0];
		tempc=col+move[i][1];
		
	if(tempc>=0&&tempc<ccol&&tempr>=0&&tempr<ccol&&a[tempr][tempc]==0)
	{
		//printf("%d %d %d\n",tempr,tempc,i);
		
		
		//printf("\n");
		
		r=knightmove(tempr,tempc,counter);
		a[tempr][tempc]=r;
		if(r==2)
		{
		show();
		printf("\n");
		return r;}
	}
	
	}
	return 0;
	
}
int
main()
{
	
	int row=2,col=2,res,i,j;
	res=knightmove(row,col,0);
	a[row][col]=res;
	if(res==2)
	{
		printf("solution is possible\n");
		show();
	}
	else
	printf("solution is not possible\n");
	return 0;		
}


Output:
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
0 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
2 1 1 1 1 

1 1 1 1 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