Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 2D Arrays
Index -> Programming, Ruby -> Ruby Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cervantes




PostPosted: Sun Aug 07, 2005 6:11 am   Post subject: (No subject)

That shouldn't be a problem, so long as the position I pass in as (x,y), which is (col, i), is on the grid. I think it is. i must be between 0 and 7, and col must also be between 0 and 7 (if it were bigger than 7, the method returns 0 [Actually, that line (49) should be changed to
code:
if col > @N - 1

instead of
code:
if col >= @N - 1

This change does not make any difference though, in that I'm still getting the same errors message]).

Hooah! I just fixed it. Turns out it I needed to add or subtract one from i inside my clearSpot method. Say I was on the bottom left corner of the board. I need to check the far right corner. The counter, i, goes from 0 to 7. x and y are both 0. The furthest along the board I can get is (N - 1, N - 1). I can understand why a thing like this would prevent the program from giving the correct output, but I'm baffled as to why it would crash the program. Must be something that occurs later on, after a piece has been placed in a spot that it shouldn't be.

I've also optimized the clearSpot procedure a little bit by taking out the checks for pieces on the right side of the piece I'm placing. We know everything on that side of the grid is empty, because we haven't got that far yet (or we've recursed back, in which case the grid spot is made false each time we recurse back).

I've also discovered that it makes no difference what I do with the following bit of code:
code:

                if col > @N - 1
                        return 0
                end if

I can comment it out and it will still work fine. It doesn't seem to speed the program any, either.

Here's the working code. I'd like to know how I could shorten it, however. Any thoughts there? Thanks in advance.

I had to change the extension of the file to .txt because .rb files are not allowed. Tony or Dan, please allow us to upload .rb files. Smile



n_queens.txt
 Description:

Download
 Filename:  n_queens.txt
 Filesize:  926 Bytes
 Downloaded:  180 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Sun Aug 07, 2005 4:31 pm   Post subject: (No subject)

Cervantes wrote:
I've also discovered that it makes no difference what I do with the following bit of code:
code:

                if col > @N - 1
                        return 0
                end if

I can comment it out and it will still work fine. It doesn't seem to speed the program any, either.

Here's the working code. I'd like to know how I could shorten it, however. Any thoughts there? Thanks in advance.

I had to change the extension of the file to .txt because .rb files are not allowed. Tony or Dan, please allow us to upload .rb files. Smile


lol that is strange. that means there is no place in you code preveting it from calling solve(N)... but i guess that doesnt matter because your program prevents any peices from being placed off the boad Laughing. im guessing that will make you program a little less efficient with larger Ns.
wtd




PostPosted: Wed Aug 10, 2005 7:59 pm   Post subject: (No subject)

Perhaps my code, that does work could help. Smile

code:
Queen = Struct.new(:row, :column)

def conflict(queen1, queen2)
   queen1.row == queen2.row or
   (queen2.column - queen1.column).abs == (queen2.row - queen1.row).abs
end

def solutions(board_size = 8, column = 0, previous_solutions = [])
   if column >= board_size
      previous_solutions
   elsif column == 0
      previous_solutions = Array.new(board_size) { |row| [Queen.new(row, column)] }

      solutions(board_size, column + 1, previous_solutions)
   else
      previous_solutions = previous_solutions.collect do |previous_solution|
         (0 ... board_size).reject do |row_number|
            previous_solution.detect { |queen| conflict(queen, Queen.new(row_number, column)) }
         end.collect do |candidate_row|
            previous_solution + [Queen.new(candidate_row, column)]
         end
      end.inject [] do |a, b|
         a + b
      end

      solutions(board_size, column + 1, previous_solutions)
   end
end

def draw_solution(board_size, solution)
   (0 ... board_size).each do |row|
      current_queen = solution.find { |queen| queen.row == row }
      (0 ... board_size).each do |column|
         if column == current_queen.column
            print "Q"
         else
            print "_"
         end
      end

      puts
   end
end
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 18 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: