Queens Problem: Only Works Up to 5

Hello there,
I've been searching through the forums for help on this problem, and can't find someone who has done this similarly to me. I am working on the N queens problem, and have it working for 1-5. Once it hits 6 and above, it always returns false. This is my coding for it: package ass2;
public class Queens {
     // squares per row or column
     public int BOARD_SIZE = 6;
     private int board[];
     public Queens() {
          board = new int[BOARD_SIZE];
     public void clearBoard() {
          //Clears the board
          //Precondition: None
          //Postcondition: Sets all squares to EMPTY
          board = new int[BOARD_SIZE];
     public void displayBoard() {
          //Displays the board
          //Precondition: None
          //Postcondition: Board is written to standard output; zero is an empty
          //square, one is a square containing a queen.
          for (int counter = 0; counter < BOARD_SIZE; counter++) {
               for (int counter2 = 0; counter2 < BOARD_SIZE; counter2++) {
                    if (board[counter2] == counter) {
                         System.out.print("Q");
                    } else {
                         System.out.print("*");
               System.out.println("");
     public boolean placeQueens(int column) {
          //Places queens in columns of the board beginning at the column specified
          //Precondition: Queens are placed correctly in columns 1 through Column - 1
          //Postcondition: If a solution is found, each column of the board contains
          //one queen and the method returns true; otherwise, returns false (no solution
          //exists for a queen anywhere in column specified)
          if (column > BOARD_SIZE) {
               return true;
          } else {
               boolean queenPlaced = false;
               int row = 1; // number of square in column
               while (!queenPlaced && (row <= BOARD_SIZE)) { //if squares can be attacked
                    if (isUnderAttack(row, column)) {
                         ++row;
                    } else {
                         setQueen(row, column);
                         queenPlaced = placeQueens(column+1);
                         // if no queens is possible in next column
                         if (!queenPlaced) {
                              // backtrack: remove queen placed earlier and try next column
                              removeQueen(row, column);
                              ++row;
               return queenPlaced;
     public void setQueen(int row, int column) {
          //Sets a queen at square indicated by row and column
          //Precondition: None
          //Postcondition: Sets the square on the board in a given row and column to QUEEN
          board[column-1] = (row-1);
     public void removeQueen(int row, int column) {
          //Removes a queen at square indicated by row and column
          //Precondition: None
          //Postcondition: Sets the square on the board in a given row and column to EMPTY
          board[column-1] = 0;
     public boolean isUnderAttack(int row, int column) {
          //determines whether the square on the board at a given row and column is under attack
          //by any queens in the columns 1 through column-1
          //Precondition: Each column between 1 and column-1 has a queen placed in a square
          //at a specific row.  None of these queens can be attacked by any other queen
          //Postcondition: If the designated square is under attack, returns true; otherwise,
          //returns false.
          boolean underAttack = false;
          int start = 1;
          int result = 0;
               while ((!underAttack) && (start < column)) {
                    if (board[start-1] == (row-1)) {
                         underAttack = true;
                    start++;
          start = 1;
               while ((!underAttack) && (start < column)) {
                    try
                    result = (board[start-1] - (row - 1)) / ((start -1) - (column-1));
                    // result calculates the slope between the two points.
                    } catch(ArithmeticException e) {
                         result = 0;
                    if ((result == -1) || (result == 1)) {
                         underAttack = true;
                    start++;
          if (underAttack) {
               // System.out.println("Column: " + column + "Row: " + row);
               return true;
          } else {
          // System.out.println("Column: " + column + "Row: " + row);
          return false;
} The main method running it is simple and is: package ass2;
public class Assignment2 {
      * @param args
     public static void main(String[] args) {
          Queens aQueen = new Queens();
          System.out.println(aQueen.placeQueens(1));
          aQueen.displayBoard();
}Any suggestions would be greatly appreciated.
Steven
P.S. I know how annoying it is to see this topic again, sorry.

Hi, try in isUnderAttack(..)
double result = 0;
result = (double)(board[start-1] - (row - 1)) / (double)((start -1) - (column-1));

Similar Messages

  • Front Row tiger problem [only works when iTunes already open]

    Ok this has never happened before and don't know why its started to happen;
    When I open up front row and go to movies,playlists or artists etc and click select, the little circling buffer symbol comes up and stays there. Nothing happens so I just have to force quit out of it.
    But, if I already have iTunes open, and then open up front row, and can select and play anything from within front row.
    Before I was always able to open front row without itunes already being open.
    Then only thing that I thought could be contributing was that a few days ago I installed you control: tunes which is an iTunes controller that stays in the menu bar.
    But even if i have this running or not, the problem with front row still persists.
    I've repaired any permissions I had but still no luck.
    Any suggestions??
    running MacBook C2D 2.16GHz with OS 10.4.11
    thanks, b.

    I apologise but there IS a new .plist file in ~/user/library/preferences. Still no fix though.
    I'm assuming its the first link I need because the second one is for leopard.
    When I try and install the .pkg I get an alert that says:
    'This software update requires a Macintosh with a built in IR receiver running Mac OS X 10.4.5'. I am running 10.4.11 and my apple remote is working just fine???
    I had Remote Buddy installed, thinkng that that may be causing the problem, but after uninstalling that, the problem still persists.
    Message was edited by: b_sheridan

  • Cross dissolve problem - only works as fade in with graphics clips

    I inserted three graphics clips with transitions (cross dissolve default) at the beginning of a sequence. The transitions only seem to work as fade-ins I have plenty of extra frames beyond the edit section of the clips. I tried to adjust the transitions in the transition editor - it shows the transition aligned to start at the edit, but won't allow mw to choose to center it on the edit or end on the edit.
    Thanks for any help you can provide

    Thank you - no, there seems to be a handle only on the left side of the edit (to the left of the transition - which sits only to the right of the edit). On the right side,(in the zone of hte transition) the cursor shows the symbol for the roll tool - r. Excuse my limited understanding of the interface and tools - I am just beginning to learn FCE.
    I will be away from the workstation until tomorrow morning.

  • With my i phone 4 , the Push notifications doesn't work for apps like (fb viber , whatsapp etc ) it only works for the official apps like message  even when im using the phone, has  this probleme with the iOs 6.0.1 and also with the iOs 6.1

    With my i phone 4 , the Push notifications doesn't work for apps like (fb viber , whatsapp etc ) it only works for the official apps like message  even when im using the phone, has  this probleme with the iOs 6.0.1 and also with the iOs 6.1

    This isn't an issue. Notice the screen prior to the one that shows usage has an iCloud section and a Manage Storage button. For this button to activate ios needs to download a few kb from icloud. Switching back to this screen forces ios to download those few kb.

  • Working with my 2 FireWire HD i had a crash. the only way to start was a safe start. then id maged to start from a DVD and install a MAC OS 10.3 wich ran 4 some time then the same problem only safeMode. kernel panic inserted the tech tool DVD

    working with my 2 FireWire HD i had a crash. the only way to start was a safe start. then id maged to start from a DVD and install a MAC OS 10.3 wich ran 4 some time then the same problem only safeMode. kernel panic by inserting the tech tool DVD, wich produces the following highligted text:
    panic(cpu 0 caller =xffff0003): 0x300 - Data access
    latest stack backtrace for CPU 0:
    Backtrace:
    0x0009CC88 0x00D63C 0x00029DAO  0x000BOA10 0x000B4270
    proceeding back via exception chain:
    Exception state (sv=0x26acd700)
    PC=0x2717E900; MSR=0x00009030; DAR=0x00000000; DSISR=0x40000000; LR=0x2717EBE4; R1=0x26AD3AE0; XCP=0x0000000C (0x300 - Data access)
    Backtrace:
    0x27178B0 0x00344ACB 0x00345970 0x2717EC08 0x2718458 0x00340590
    0x003496EC 0x0034B59C 0x0034A6BC 0x000B1DD4
    Kernel Loadable modules in backtrace (with dependencies):
    com.apple.driver.AppleFWDHC1(3.9.5)00x2717c000->0x271aafff
    dependency: com.apple.iokit.10PCIFamily(2.6)00x1e840000
    dependency: com.apple.iokit.I0FireWireFamily(3.4.7)00x270dd000
    Exception state (sv=0x26abd280)
    PC=0x00000000; MSR=0x0000D030; DAR=0x00000000; DSISR=0x00000000; LR=0x00000000;R1=0x00000000; XCP=0x00000000 (unknown)
    BSD process name corresponding to current thread: kernel_task
    so what
    greetings from berlin

    Greetings,
    It appears to be a FW problem, possibly flaky case, or drive problem.
    One way to test is to Safe Boot from the HD, (holding Shift key down at bootup), run Disk Utility in Applications>Utilities, then highlight your drive, click on Repair Permissions, Test for problem in Safe Mode...
    PS. Safe boot may stay on the gray radian for a long time, let it go, it's trying to repair the Hard Drive
    Reboot, test again.
    If it only does it in Regular Boot, then it could be some hardware problem like Video card, (Quartz is turned off in Safe Mode), or Airport, or some USB or Firewire device, or 3rd party add-on, Check System Preferences>Accounts (Users & Groups in later OSX versions)>Login Items window to see if it or something relevant is listed.
    Check the System Preferences>Other Row, for 3rd party Pref Panes.
    Also look in these if they exist, some are invisible...
    /private/var/run/StartupItems
    /Library/StartupItems
    /System/Library/StartupItems
    /System/Library/LaunchDaemons
    /Library/LaunchDaemons

  • Back button only works about 50% of the time. Have to hit it twice, sometimes three times before it works. Problem is intermittent. Firefox 3.6.10 on Mac OS 10.6.4 (same on both MacBook and iMac machines).

    Back button only works about 50% of the time. Have to hit it twice, sometimes three times before it works. Problem is intermittent. Firefox 3.6.10 on Mac OS 10.6.4 (same on both MacBook and iMac machines).

    You can try http://kb.mozillazine.org/Bookmarks_history_and_toolbar_buttons_not_working_-_Firefox

  • I have an email account causes the machine to hang up after being in use for a day.  It only occurs on my Macbook Pro and only one particular account.  The problem account works fine on my iMac and Windows Machines.  Any ideas what is causing this?

    I have an email account causes the machine to hang up after being in use for a day.  It only occurs on my Macbook Pro and only one particular account.  The problem account works fine on my iMac and Windows Machines.  I have been to the local Genius bar and through trial and error we have determined that Mail is working ok but somehow the account is causing the problem.  I have used this account for years.  Any ideas what is causing this?

    No idea why, but one thing you may try if you have the time, is create a new user, and set up the problem mail account in the new user space. See if it causes your MBP to hang as well.  That will tell you if it is something wrong with your main User, or something wrong with MBP.
    Assuming this works, I'd nuke the problem account in your main user space, re-start, and reinititate the account and see if that helps.
    Depening on how many email accounts you have setup, it may be necessary to nuke the whole Mail folder in your User/Library....   Hard way to get it to work, but just my idea.

  • What should i do my sleep/wake button on iphone 5 does not work properly it only works if i press on the left side of it and my iphone still on warranty i heard its a common problem

    what should i do my sleep/wake button on iphone 5 does not work properly it only works if i press on the left side of it and my iphone still on warranty i heard its a common problem

    If there's a hardware defect and your phone is under warranty, bring it to Apple for replacement.

  • Problem: Bookmark sync only works in one direction

    I have 2 computer, computer A has vista and computer B is a netbook with XP. The problem is that the bookmark sycn only works in one direction.
    For example: both my computer has the same book marks (previously sync with X-mark which is now disabled). I decided to do some clean up on computer B. If Y sync computer A, nothing happens, but when I sync computer B, all the cleanup I made has been undone since computer A override all the changes I made to B.
    In other words, the changes I made to B can never be applied to A.

    When you're doing a "clean-up" like that you need to use '''Reset Sync''' in Tools > Options > Sync - Current Account = Manage Account and use the ''' ''Replace all other devices with this computer's data'' ''' option. See the attached screenshot - use the third option.

  • My home button only works occasionally. Has anyone had this problem? Solution?

    my home button only works occasionally. Has anyone had this problem? Solution?

    Fix a broken, unresponsive or sticky iPhone Home Button

  • I'm using iTunes 9 and I recently just bought AirPort Express. I have used it before in the past put the problem I have now is that I cannot select the Express network for my speakers. Does Airport Express only work with iTunes 10 or 11?

    I'm using iTunes 9 and I recently just bought AirPort Express. I have used it before in the past put the problem I have now is that I cannot select the Express network for my speakers. Does Airport Express only work with iTunes 10 or 11?

    Hello Hornet12,
    Indeed, iTunes 10.2 or later will be required to utilize AirPlay.
    Using AirPlay
    http://support.apple.com/kb/HT4437
    AirPlay requirements and capabilities
    To get full AirPlay features, make sure your AirPlay-enabled devices are running the latest software updates. The table below identifies minimum AirPlay requirements and capabilities:
    Stream content from
    Requirement
    Notes
    iPad
    iOS 4.3 or later.1
    From the Videos, iPod, Photos, Music, and YouTube apps on iOS devices, stream videos, music, and photos to an Apple TV (2nd and 3rd generation), or stream music to an AirPort Express or compatible third-party device.
    With iOS 4.3 and later, you can also stream video and audio from a website or a third-party app installed on your iOS device if the developer for the app or website has added AirPlay functionality.
    iPhone (3GS or later)
    iPod touch
    (2nd generation or later)
    Computer
    iTunes 10.2 or later.
    Stream videos and music from your iTunes library to an Apple TV (2nd and 3rd generation), or music to an AirPort Express or compatible third-party device.
    Apple TV
    Apple TV software version 5.1 or later.
    Stream music from your Apple TV to another Apple TV (2nd and 3rd generation), to an AirPort Express or compatible third-party device.
    Cheers,
    Allen

  • WiFi only works close to router, DNS fail, Signal Bars present but Safari says no Internet, I've deleted and readded through to reset and full reset, I had no problem with 4.3.2 only 4.3.3 both my iPhone4 and my iPad2. I am now beside router - no problem

    Help
    WiFi only works close to router, DNS fail, Signal Bars present but Safari says no Internet, I've deleted and re-added through to reset and full reset, I had no problem with 4.3.2 only 4.3.3 both my iPhone4 and my iPad2. I am now beside router - no problem, but go 20 metres and WiFi becomes unstable. Only way to get either to work is disable WiFi either as a Hot Spot or turn it off and use the 3G connection. This worked with 4.3 &amp; 4.3.2 what has gone wrong? I raised it at the Apple Store in Belfast as a problem but was told that there was no problem with Apple but I must have a faulty router - yeah - that's a lot of faulty routers, my home, my office, my holiday home, Starbucks, my friends house, my son's house. I've tried all the 'fixes'. I think it is a plan to force us to buy Apple Routers

    Two ways out, I added two more Belkin Access Points in my house in Spain so that I'm not more than 10Mtrs away, (Reinforced Concrete & solar glass 2010)
    I bought 3 Apple Airports, 1 x Extreme and 2 x Express (1 as a hard wired AP, 1 as an extender) for my other house (Brick construction 1930's)
    Everthing now works perfectly, as I said 'an apple plot', so either blast them with signal or visit your nearest apple store.
    Early beta iOS5 (iPad) is also much inproved in Beta 3, I have not had any problem with 'Lion'
    Happy hunting!
    john

  • Good day. I have a problem is a procedure to change the firmware? I have 5,0,1 but with our SIM only works 5,0,0

    good day. I have a problem is a procedure to change the firmware? I have 5,0,1 but with our SIM only works 5,0,0

    onix_76 wrote:
    how come? Documents also have on hand to buy as photo and check the box
    So what?  The second-hand shop you bought it from didn't follow the right procedures.  You also didn't follow the right procedures to make sure that iPhone was not in Activation Lock.  It's like buying a car for cash but not getting the title signed over to you.  It's not your car if you don' have the title.  And that's not legally your iPhone if the previous owner's Apple ID is still registered to the device for Find My iPhone/Activation Lock.
    Only a proof of purchase from the ORIGINAL retail store would do any good at all.  Return the device to where you bought it and demand your money back.  If they refuse, either get a lawyer and/or turn in the device to local police.  If you're in possession of stolen property, then the cops won't care about where you got it from.

  • Problem: Native method only works one

    Hi, I have this class that calls the GetShortName method from the windows API.I have created the asociated DLL and everything went quite smooth.
    My problem is that when I make a new object of the class GetShortNameA and use the method TranslateIntoShort(String) this only works the fist time.
    The rest of the times I try to use this method with other arguments I am getting the same answer that I was getting the first time I call it.
    Why can this be and how can I fix it??
    I have tried declaring the TranslateIntoShort method non-static, but then the whole thing doesn't work (evena fter regenerating the h file and the DLL).
    package alfagui_1;
    public class GetShortNameA {
    public GetShortNameA() {
    static{
    System.loadLibrary("GetShortPathNameA");
    public static native String TranslateIntoShort(String LongName);
    GetShortNameA ShortName = new GetChortNameA();
    First time:
    ShortName.TranslateIntoShort("C:\Mina Dokument\alfa")-->"C:\Minado~1\alfa"
    Sucessive calls:
    ShortName.TranslateIntoShort(WHATEVER)-->"C:\Minado~1\alfa"
    Thanks for your help

    Perhaps if you posted your code we could point out the problem?

  • I have problems with outgoing call no audio, only works with speaker. I bought my iphone on december 2011.

    I have problems with outgoing call no audio, only works with speaker. I bought my iphone on december 2011.

    Had the same problem, no audio on incoming or outgoing calls and only could hear the conversation of the call if I put on speaker. It seemed to start after I did a recent IOS update this past month. I tried restart, reset and restore of my iPhone but nothing changed. It appears not to be a software issue but a hardware problem. I got my phone this past summer so it was still under warranty and got it replaced.

Maybe you are looking for

  • Mail app - sent items do not appear in Sent mailbox?

    When i send emails they do not automatically appear in the Sent mailbox - is this a user setting? Surely all sent items should autimatically appear in there?

  • Re: This week's Mobile offers: Week of 3/3/2013

    Looking to upgrade my existing VZN DroidX to Galaxy Note 2. Does the $50 GIFT CARD Reservation apply to upgrades too. For instance, with current $199 price on Note 2, should the gift card reservation reduce the price to $149. TIA

  • In Yosemite, how to keep all documents in Dock from popping up when one is opened

    If I have several Illustrator documents in the dock, and open one of them, they all pop up. Is there a way to prevent this? [Moved from the generic Cloud forum to the specific Program forum... Mod]

  • Finding jsp file for the OA framework

    hi all, i working in the OA framework, and i need to do some changes in the application and i need to get the jsp files for the shopping carts, i connected to the server through ftp. from the about link in the shopping cart it says that the OA_HTML f

  • Time Machine backs up regularly, but shows up as Empty

    Time Machine is doing it's thing, backing up on a regular basis. When I look at the T.M. drive, it's loaded with current files. BUT... When I try and enter Time Machine, I'm unable to move back in time at all. It indicates that there's no backups of