Results 1 to 6 of 6

Thread: Weekly Programming Challenge, 1 April 2007

  1. #1
    Join Date
    Sep 2006
    Beans
    Hidden!

    Weekly Programming Challenge, 1 April 2007

    This installment of the (semi) Weekly Programming Challenge has the participant, you, solving the old problem of the Knight's Tour:

    Quote Originally Posted by Wikipedia
    The Knight's Tour is a mathematical problem involving a knight on a chessboard. The knight is placed on the empty board and, moving according to the rules of chess, must visit each square exactly once.
    This challenge will assume a standard chess board size of 8x8. As usual, any language allowed, but no libchess please!

    points++ for solving the problem given a board of size nxn.

  2. #2
    Join Date
    Mar 2007
    Beans
    28

    Re: Weekly Programming Challenge, 1 April 2007

    This is my first crack at the problem, I made an algorithm that can solve the tour for a board size nxn using back-tracing.
    Code:
    #!/usr/bin/env python
    def find_tour(size, start_x, start_y):
        moves = [[1, 2], [2, 1], [2, -1], [1, -2], [-1, -2], [-2, -1], [-2, 1], [-1, 2]]
        board = []
        trace = []
        n_move = 1
    
        for i in range(0, size):
            board.append([-1] * size)
            
        board[0][0] = 0
         
        cur_x = start_x
        cur_y = start_y
    
        while n_move < size * size:
            p = []
            
            for move in moves:
                move_x = cur_x + move[0]
                move_y = cur_y + move[1]
    
                if (move_x >= 0) and (move_y >= 0) and (move_x < size) and (move_y < size) and (board[move_x][move_y] == -1):
                    p.append((move_x, move_y))
    
            while not len(p):          
                cur_x, cur_y, p = trace.pop()
                board[cur_x][cur_y] = -1
                n_move -= 1;
    
            cur_x, cur_y = p.pop()
    
            board[cur_x][cur_y] = n_move
            trace.append((cur_x, cur_y, p))
            n_move += 1
    
        return board
    
    b = find_tour(8, 0, 0)
    print b
    This is pretty slow but it works so far. I plan to create a GUI to better visualize the tour tomorrow.

    Here is the output for 5x5:
    Code:
    jmccaffrey@windy:~/mywork/knights-tour$ time ./pytour.py
    [[0, 3, 8, 17, 20], [9, 16, 19, 2, 7], [4, 1, 12, 21, 18], [15, 10, 23, 6, 13], [24, 5, 14, 11, 22]]
    
    real    0m0.113s
    user    0m0.112s
    sys     0m0.000s

  3. #3
    Join Date
    Nov 2006
    Location
    Roselle Park, New Jersey
    Beans
    25
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Weekly Programming Challenge, 1 April 2007

    i once made one that solved an nxn board using backtracing for one of my classes here at NJIT. If i have time too later today maybe I'll see if my memory still lets me throw one together!
    - Gorlith
    -----------------------------
    Ubuntu Since: 6.10 Edgy Eft

  4. #4
    Join Date
    Aug 2006
    Beans
    366

    Re: Weekly Programming Challenge, 1 April 2007

    This installment of the (semi) Weekly Programming Challenge
    Point of order, are there now two challenges per week? Or should it be semi-monthly?
    Linux Counter entry # 99383 (since 1995), Feisty Xbuntu 64 bit
    Folders! We don't need no stinking folders. "I don't have anything on my machine that needs folding" -- Unknown

  5. #5
    Join Date
    Sep 2006
    Beans
    Hidden!

    Re: Weekly Programming Challenge, 1 April 2007

    dwblas,

    "semi", because they haven't exactly been happening on a weekly basis (if you haven't noticed). I sometimes don't find time or even an idea for a challenge to post every week, which is why some weeks don't have one. I will work on more regularly posting new challenges, at least weekly.

    Certainly, more frequent challenges could be posted if the demand is high.

  6. #6
    Join Date
    Aug 2006
    Beans
    366

    Re: Weekly Programming Challenge, 1 April 2007

    I been down for the last month with the flu that's been going around, so didn't know if the timing had changed or not. Thanks for the effort put forth coming up with the challenges.
    Linux Counter entry # 99383 (since 1995), Feisty Xbuntu 64 bit
    Folders! We don't need no stinking folders. "I don't have anything on my machine that needs folding" -- Unknown

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •