C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 25th, 2005, 08:27 PM
CaptainARG CaptainARG is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 1 CaptainARG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 46 sec
Reputation Power: 0
Knight's Tour problem

OK...new to this forum, but in need of a little help. I am working on the Night's Tour problem using a recursive, backtracking solution, but my program will never finish. It just locks up the shell evertime, even with a known good solution. It's due tomorrow and I'm a not sure where to go from here:

Code:
#include<iostream>
using namespace std;



class Knight
{
public:

  bool valid_pos (int row, int col, int size);
  void display_board(int size);
  void recursive (int row, int col, int size, int count);
  int check_board(int size);

};

//Global vars                                                                                                                                                                     
const int size=8;//Board size                                                                                                                                                     
int board[size][size];  //Create board                                                                                                                                            



int main()
{
  Knight check;
  int row, col, count=0;


  for(int a=0;a<=size;a++) //clear board to 0                                                                                                                                     
    {
      for(int b=0;b<=size;b++)
        {
          board[a][b]=0;
        }
    }

  //Take input                                                                                                                                                                    
  cout<<"Enter the start row of the knight: ";
  cin>>row;
  cout<<"Enter the starting column of the knight: ";
  cin>>col;

  check.valid_pos (row, col, size);

  //Output results                                                                                                                                                                

  check.recursive(row, col, size, count);
  check.display_board(size);

  return 0;
}

bool Knight::valid_pos (int row, int col, int size){
  if(row>=size || col>=size ||  row<0 || col<0){//Test for chosen location being within board dimensions                                                                          
    return 1;

  }
  else{//Check to see if location was not previously visited                                                                                                                      
    if((board[row][col]) == 0){
      return 0;

    }
    else{//returns 1 if location previously visited                                                                                                                               
      return 1;

    }
  }
}

void Knight::display_board(int size)
{
  for(int c=0;c<size;c++)
    {
      for(int d=0;d<size;d++)
        {
          if(d==size-1)
            {
              cout<<"| "<<board[c][d]<<" |"<<endl;
            }
          else
{
              cout<<"| "<<board[c][d]<<" ";
            }
        }
    }
}


int Knight::check_board(int size)
{
  int rows, cols, checker;

  for(rows=0;rows<size;rows++)
    {
      for(cols=0;cols<size;cols++)
        {
          if(valid_pos(rows,cols,size)!=1)
            {
              checker=0;
            }
        }
    }
  return(checker);
}

void Knight::recursive(int row, int col, int size, int count)
{

  int checked;

  board[row][col]=count;

  if(valid_pos (row+2, col+1, size)!=1){
    count++;
    recursive(row+2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col+1]=0;
      count--;
    }
  }
  if(valid_pos (row+2, col-1, size)!=1){
    count++;
    recursive(row+2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col-1]=0;
      count--;

    }
  }
if(valid_pos (row+1, col+2, size)!=1){
    count++;
    recursive(row+1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row+1, col-2, size)!=1){
    count++;
    recursive(row+1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col-2]=0;
      count--;
    }
  }
  if(valid_pos(row-2, col+1, size)!=1){
    count++;
    recursive(row-2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col+1]=0;
      count--;
    }
  }

if(valid_pos (row-2, col-1, size)!=1){
    count++;
    recursive(row-2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col-1]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col+2, size)!=1){
    count++;
    recursive(row-1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col-2, size)!=1){
    count++;
    recursive(row-1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col-2]=0;
      count--;
    }
  }

checked=check_board(size);
  if(checked==0){
    board[row][col]=0;
    count--;
  }
}

Last edited by B-Con : September 26th, 2005 at 06:31 PM. Reason: don't forget your [code] tags ;)

Reply With Quote
  #2  
Old September 26th, 2005, 07:03 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
I don't think anybody can help you at this short notice.
It also should be handy if you explained the program some more, and when you'd posted it in CODE (making it a lot more readable)...

A few remarks:
In check_board() the variable checker is not initialized. This means it can have every possible value, but almost never the one you want. I guess you want it to start with 1.
This function can also be improved by returning 0 when a empty space is found, this way you can skip the rest of the grid.

For the rest, your program looks fine to me.... isn't it just that the program takes a LONG time to determine the solution?
I don't see any deadlocks, but who am I

Reply With Quote
  #3  
Old September 26th, 2005, 07:31 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
I just compiled and ran it...
With a size of 4x4, it takes almost no time to finish.
With a size of 5x5, it took 14.5s, 5.6s, 2.1s, 3.6s, etc.
With a size of 6x6, it took 31s (with solution), 31.2s (with solution), 520.7s=8.5min(with solution!!!!)

Some times a solution is found, and sometimes not (seems to be correct).
In other words, your program works. There is an exponential time increase when the grid gets bigger!
My advice: don't worry, and just submit your solution. ;-)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Knight's Tour problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT