__________     __ __     __  _______    ________
  / ____/ __ \   / // /    / / / /  _/ |  / / ____/
 / / __/ / / /  / // /_   / /_/ // / | | / / __/
/ /_/ / /_/ /  /__  __/  / __  // /  | |/ / /___
\____/\____/     /_/    /_/ /_/___/  |___/_____/

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

JAVA coding problems solved -- The N-Queens Puzzle

BY: @gaottantacinque | CREATED: Sept. 29, 2018, 5:13 a.m. | VOTES: 29 | PAYOUT: $2.28 | [ VOTE ]

Here is the first of a series of coding exercises that I will share with you after solving and testing them.

Title: N-Queens Puzzle
Difficulty: hard

You can find the description of the problem here on leetcode.com .

In a nutshell: you have to find all the ways to place N queens on a N×N chess board so that the queens do not attack each other.

Example:
Input: 8
Output: All combinations for 8 queens on a 8x8 chess board

https://upload.wikimedia.org/wikipedia/commons/1/1f/Eight-queens-animation.gifwikimedia commons image

INDEX:
- QUESTIONS
- HINTS (to solve it on your own)
- MY SOLUTION

-----

QUESTIONS

  1. How would you improve it / how would you have solved it instead?
    I'm aware that performance wise it's not a great algorithm. Better approaches that could bring to better time complexity are:
    - Use of recursion
    - Distribution of the workload on multiple threads
    (probably makes sense only for big Ns - with a classic chessboard there shouldn't be any significant improvement)
    - Dynamic Programming?

  2. How would you have solved it in the worst way possible?
    I'll post an example in the comments later on.   : )

-----

HINTS

Always analyse the properties of the system. Each row need to have one and only one queen. This means that:
- Once you manage to add to a row a queen that does not attack the others, you can stop trying to place a new queen on the same row
- If you find yourself in a situation in which one of the rows is empty you can discard that solution right away

-----

MY SOLUTION

[IMAGE: https://cdn.steemitimages.com/DQmWfrspZRm4n8L4W4jSjgD8twrWtLYQFFgf4R5EthPxsDx/NQueens-notes.png]

TAGS: [ #coding ] [ #programming ] [ #java ] [ #developer ] [ #software ]

Replies

@resteembot | Sept. 30, 2018, 2:41 a.m. | Votes: 0 | [ VOTE ]

Resteemed by @resteembot! Good Luck!
The resteem was paid by @marcocasario
Check @resteembot's introduction post or the other great posts I already resteemed.

@abasinkanga | Sept. 30, 2018, 4:51 a.m. | Votes: 0 | [ VOTE ]

@smartbot tip 1
Your post has been resteemed. Thanks for using my resteem service
Get more rewards in my discord server

@smartbot | Sept. 30, 2018, 4:51 a.m. | Votes: 4 | [ VOTE ]

Σ$$$ Tipped @gaottantacinque Σ1 SMART! Comment @smartbot help to claim. Currently the price of SmartCash in the market is $0.027 USD per SMART. Current value of the tip is $0.03 USD. To find out more about SmartCash, please visit @smartcash.

@resteemr | Sept. 30, 2018, 5:51 a.m. | Votes: 0 | [ VOTE ]

This post was upvoted and resteemed by @resteemr!
Thank you for using @resteemr.

@resteemr is a low price resteem service.
Check what @resteemr can do for you. Introduction of resteemr.

@minnowsupporter | Sept. 30, 2018, 12:57 p.m. | Votes: 2 | [ VOTE ]

Resteemed by @minnowsupporter . Check on @minnowsupporter to know how i can support you

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>