GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Oct-2008, 09:24
wawachi776 wawachi776 is offline
New Member
 
Join Date: Oct 2008
Posts: 6
wawachi776 can only hope to improve

Knight tour of kai85


..i tried to execute kai85 program...i fixed the errors this is my code...
CPP / C++ / C Code:
#include<iostream.h>
#include<conio.h>
int f( int,  int,const int [][8],const int [][8], int [],int []);
int p,q;
int main()
{
	int h=1,lowest1,lowest2;
	int accessibility[8][8]={{2,3,4,4,4,4,3,2},
	{3,4,6,6,6,6,4,3},
	{4,6,8,8,8,8,6,4},
	{4,6,8,8,8,8,6,4},
	{4,6,8,8,8,8,6,4},
	{4,6,8,8,8,8,6,4},
	{3,4,6,6,6,6,4,3},
	{2,3,4,4,4,4,3,2}};
	int array[8][8]={0};
	int horizontal[]={2,1,-1,-2,-2,-1,1,2};//the vertical and horizontal 
	//arrays help make the l shaped moves.
	int vertical[]={-1,-2,-2,-1,1,2,2,1};
	int currentrow,currentcolumn,x,y,movenumber;
	
	cout<<"Enter a row and a column for the horse's start position:"<<endl;
	cin>>currentrow>>currentcolumn;
	array[currentrow][currentcolumn]=h,
		accessibility[currentrow][currentcolumn] -=1;
	
	
	for(int i=0;i<8;i++){
		for(int j=0;j<8;j++){
			cout<<array[i][j]<<' ';
			if(j==7)
				cout<<endl<<endl;}
		
	}
	
	
	for (int z=0;z<63;z++)//this step is repeated 63 times .. for the 63 
		// moves in which the program has to choose the 
		//least accessible chesshouse
		
	{
		int lowest=9;
		for(int i=0;i<8;i++){
			movenumber=i;
			x=currentrow+vertical[movenumber]; 
			y= currentcolumn+horizontal[movenumber];
			
			if(x>-1 && x<8 && y>-1 && y<8)   //so the knight doesn't 
				//go out of the  chess 
				//board
			{
				
				if(array[x][y] == 0 && accessibility[x][y] <= lowest)
					//to make sure the knight  doesn't go to the same houses
					
				{                                                   
					
					
					
					if (accessibility[x][y] == lowest)
						//if the accessibility elements have the same value
					{
						{						
							lowest1=f(x,y,accessibility,array,horizontal, vertical);//call to the f function
							//to find the house that should 
							//the horse be moved to, has the least 
							//accessibility option in it's next move!
							lowest2=f(p,q, accessibility,array,horizontal, vertical);
							if (lowest1 < lowest2)
								lowest=accessibility[x][y],	p=x,q=y;
							
						}
						
					}
					
					else	lowest=accessibility[x][y], p=x,q=y;
				}	   
				
				
			}	
		}
		
		
		
		array[p][q]=h+1,h++,accessibility[p][q] -=1,
			currentrow=p,currentcolumn=q;
		
	}
	
	
	
	cout<<endl<<endl<<endl<<endl;
	for(i=0;i<8;i++){
		for(int j=0;j<8;j++){
			cout<<array[i][j]<<' ';
			if(j==7)
				cout<<endl<<endl;}
		
	}
	
	cout<<endl<<endl<<endl<<endl;
	for(i=0;i<8;i++){
		for(int j=0;j<8;j++){
			cout<<accessibility[i][j]<<' ';
			if(j==7)
				cout<<endl<<endl;}
		
	}
	
	return 0;
	}
	
	
	int f(int a,int b,const int accessibility[][8],const int array[][8],int horizontal[],int vertical[])
	{
		int x1,y1,lowest1=9,p1,q1,movenumber,lowest2,lowest3;
		{	
			
			for(int i=0;i<8;i++)
			{
				movenumber=i;
				x1=a+vertical[movenumber];
				y1=b+horizontal[movenumber];
				
				if(x1>-1 && x1<8 && y1>-1 && y1<8)
				{
					
					if(array[x1][y1] == 0 && accessibility[x1][y1] <= lowest1)
					{
						
						
						
						if (accessibility[x1][y1] == lowest1)
						{
							
							lowest2=f(x1,y1,accessibility,array,horizontal, vertical);
							lowest3=f(p1,q1,accessibility,array,horizontal, vertical);
							if (lowest2 < lowest3)
								lowest1=accessibility[x1][y1],	p1=x1,q1=y1;
							
						}
						
					}
					else	lowest1=accessibility[x1][y1], p1=x1,q1=y1;
				}	   
									
			}
		}
		return lowest1;//should i make a base case here or is this enough?
	}



it will ask "Enter the row and column"
..after executing it... i came up with an output of when i put 0,0...

1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Last edited by LuciWiz : 08-Oct-2008 at 09:26. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 08-Oct-2008, 09:31
wawachi776 wawachi776 is offline
New Member
 
Join Date: Oct 2008
Posts: 6
wawachi776 can only hope to improve

Re: knight tour of kai85


please help me...your help is appreciated...
  #3  
Old 09-Oct-2008, 08:58
wawachi776 wawachi776 is offline
New Member
 
Join Date: Oct 2008
Posts: 6
wawachi776 can only hope to improve

Re: Knight tour of kai85


...please help me...i really nid it...i dont know what i did wrong in the code...i think the problem is in the logic...
  #4  
Old 10-Oct-2008, 03:52
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Knight tour of kai85


What is your problem and what is kai85 ?
  #5  
Old 10-Oct-2008, 11:23
wawachi776 wawachi776 is offline
New Member
 
Join Date: Oct 2008
Posts: 6
wawachi776 can only hope to improve

Re: Knight tour of kai85


kai85 is the creator of this program...i just corrected the errors.

1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

assumng that this is the output of a chessboard...now the problem is... 1 is the starting position of the knight...you need to move it around the chessboard labeled from 1 to 64...w/o touching the one where it gone b4...my problem is that i can't show the other numbers...i dont know if the code or the logic is correct
  #6  
Old 10-Oct-2008, 22:25
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Knight tour of kai85


That's mean you can enter [0][0] position in the chessboard.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Knight tour (working... I think... but inefficient) help. Elsydeon C++ Forum 8 26-Sep-2005 17:19
knight tour (chess program) kai85 C++ Forum 10 25-Mar-2005 07:12
Knight tour (arrays help needed) dilmv C++ Forum 7 18-Oct-2004 15:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:37.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.