Below is my attempt at solving the 8 queens problem to print one solution. (place 8 queens on a chessboard such that none of them are attacking each other). However, this solution only places 6 queens. I need another opinion of where I am making a mistake. I'm doing this in more of a BFS style instead of backtracking.
solving the eight queens to produce a solution
276 Views Asked by user3050784 At
1
There are 1 best solutions below
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in N-QUEENS
- Choose list of lists element unique to row and column (N Queens)
- N-Queens puzzle, but with all chess pieces
- Eight Queens - Diagonal Movement
- reflection and symmetry in back tracking queens
- How to solve n-queens in scheme
- solving the eight queens to produce a solution
- Confusion about "no three queens are in a straight line" property for Eight Queens
- Queens Puzzle Breadth first
- How to execute/skip certain lines of code using preprocessor in C?
- 8 Queens Not Using the Backtracking Method
- n-queens, checking for a valid board
- How to convert an ArrayList<ArrayList<String>> to List<List<String>> in Java?
- Parallel running of Racket code for N-Queens problem
- How does this n-queens Prolog solution work?
- n-queens solution not working in Prolog
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
It seems your algorithm is malfunctioning at some point. Upon running it, I found the following issues:
You are constantly setting
visited[i][j]to 0 in your for loop in main. This always resets visited to 0 even if a recursion call is made. In fact, when you declare bothvisitedandboardthey are initiated to arrays full of 0s. So you can get rid of both set statements in there. In addition, because you reset the arrays, your recursive function ends up setting both values to 0 and then finds them again."For debugging, in the
!hasQueenstatement, you should output theboard[row][col]coordinates, which show you the coordinates that have been found. The final list before it prints out the grid shows that 2,4 and 1,6 are found and set twice.The actual chessboard that is output ends up with an impossible solution:
(sorry I can't get the numbers to format)
Both layout X and layout Y fail the 8 queens rules.
If you run your program with the setting to 0 commented out, you will see that it grinds to a halt after finding 6 locations.