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 13-Oct-2008, 09:10
wawachi776 wawachi776 is offline
New Member
 
Join Date: Oct 2008
Posts: 6
wawachi776 can only hope to improve

Knights tour


guys...i got from the net...i want to study about it....i got a problem because i dont know how to solve it mathematically...i cant solve the "void exits(int) part...i need ur help...tnx alot hope you can help me...


CPP / C++ / C Code:
#include<iostream.h>  
#include<iomanip.h>      
  

void possible();  
void exits(int);  
  
  
const int ver[]={-1,-2,-2,-1,1,2,2,1},  
          hor[]={2,1,-1,-2,-2,-1,1,2};  
  
int row,col,npos;  
int board[8][8],nextij[8][8][8],accessible[8][8];  
int main()  
{  
  
    int count = 1,k,j;
  
    cout <<"position [from (0,0) to (7,7)]:";  
    cin >>row >>col;  
    cout << endl;  
  
    board[row][col]=count;  
  
  
    while(count!=64)  
    {  
        count++;  
        possible();  
        exits(count);  
        
    }  
    for(j=0;j<=7;j++)  
    {  
        for(k=0;k<=7;k++)  
            cout <<  setw(3) << board[j][k];  
        cout <<"\n\n";  
    }  
  
  
    return 0;  
}  
void possible()  
{  
    int npos;  
    for(int r=0;r<=7;r++)  
    {  
  
        for(int c=0;c<=7;c++)  
        {  
            npos = 0;  
              
            for(int i=0;i<=7;i++)  
            {  
                if(((r+ver[i] >=0) && (r+ver[i] <=7))&&((c+hor[i] >=0) && (c+hor[i] <=7))&&(board[r+ver[i]][c+hor[i]] == 0))  
                {  
                      
                    nextij[r][c][npos] = i;  
                    npos++;  
                }  
            }  
            accessible[r][c] = npos;      
        }  
    }     
      
}  
void exits(int l)  
{  
  
    int min = accessible[row+ver[nextij[row][col][0]]][col+hor[nextij[row][col][0]]];  
    int r = row+ver[nextij[row][col][0]],c=col+hor[nextij[row][col][0]];  
  
    for(int i=1;i < accessible[row][col];i++)  
        if(min >= accessible[row+ver[nextij[row][col][i]]][col+hor[nextij[row][col][i]]])  
        {  
            min =accessible[row+ver[nextij[row][col][i]]][col+hor[nextij[row][col][i]]];  
            r = row + ver[nextij[row][col][i]];  
            c = col + hor[nextij[row][col][i]];
        }  
          
         
        board[r][c]=l;  
        row = r;  
        col = c;  
  
}
Last edited by LuciWiz : 13-Oct-2008 at 11:11. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 15-Oct-2008, 02:18
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: knights tour


What is your question ?
  #3  
Old 15-Oct-2008, 04:45
wawachi776 wawachi776 is offline
New Member
 
Join Date: Oct 2008
Posts: 6
wawachi776 can only hope to improve

Re: Knights tour


i got a problem in the void exit(int l) part...i dont know what to do with it...i want to solve it mathematically...
 
 

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
Knights tour problem (using brute force) harry1617 C++ Forum 5 21-Apr-2008 16:11
Help with Knight's Tour jowalsh9 C++ Forum 3 13-Mar-2008 08:06
Problem with getting multiple solutions from Knight's Tour... amarufox C++ Forum 3 22-Apr-2006 14:08
Knight's Tour - now with heuristics earachefl C++ Forum 0 09-Apr-2006 11:19
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 13:15

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

All times are GMT -6. The time now is 22:15.


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