Results 1 to 2 of 2
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default Need an explanation regards the following recursion

    Hi, need help understanding the calculation aspect of: if ((q[i] - q[n]) == (n - i)) return false; // same major diagonal
    in the following code:

    Java Code:
    public class Queens {
    
       /***********************************************************************
        * Return true if queen placement q[n] does not conflict with
        * other queens q[0] through q[n-1]
        ***********************************************************************/
        public static boolean isConsistent(int[] q, int n) {
            for (int i = 0; i < n; i++) {
                if (q[i] == q[n])             return false;   // same column
                if ((q[i] - q[n]) == (n - i)) return false;   // same major diagonal
                if ((q[n] - q[i]) == (n - i)) return false;   // same minor diagonal
            }
            return true;
        }

    Thanks :-)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Need an explanation regards the following recursion

    Draw an eight x eight grid on a piece of paper; the top row of your grid contains the coordinates (0, 0) (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) (0, 6) and (0, 7); the next row contains the coordinates (1, 0) (1, 1) (1, 2) ... etc. Supose your array q contains eight elements, one for each column in your grid and q[i] contains the second coordinate in the coordinate pair (i, q[i]).

    If q[i] ==q[n] there are two queens in columns i and n that are positioned in the same row. Check your grid and try to find a regularity for two values in the same upgoing diagonal and similar for a down going diagonal.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. need explanation
    By marie in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 04:35 PM
  2. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  3. explanation of this loop?
    By glopez09 in forum New To Java
    Replies: 4
    Last Post: 11-15-2009, 03:36 AM
  4. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-14-2007, 12:39 AM
  5. I need didactic explanation
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:37 AM

Posting Permissions

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