GithubHelp home page GithubHelp logo

louiscaixuran / n-queens-problem Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 9 KB

trying to solve the codewar problem N queens problem (with one mandatory queen position) - challenge version

Python 88.23% Rust 11.77%

n-queens-problem's Introduction

n-queens-problem

trying to solve the codewar problem N queens problem (with one mandatory queen position) - challenge version

question link:https://www.codewars.com/kata/5985ea20695be6079e000003/

description

The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column or diagonal. The eight queens puzzle is an example of the more general N queens problem of placing N non-attacking queens on an N×N chessboard. You can read about the problem on its Wikipedia page: Eight queens puzzle.

You will receive a (possibly large) number N and have to place N queens on a N×N chessboard, so that no two queens attack each other. This requires that no two queens share the same row, column or diagonal. You will also receive the mandatory position of one queen. This position is given 0-based with 0 <= row < N and 0 <= col < N. The coordinates {0, 0} are in the top left corner of the board. For many given parameters multiple solutions are possible. You have to find one of the possible solutions, all that fit the requirements will be accepted.

You have to return the solution board as a string, indicating empty fields with '.' (period) and Queens with 'Q' (uppercase Q), ending each row with '\n'.

If no solution is possible for the given parameters, return None.

Notes on Rust version:

input parameters are size and (mandatory_column, mandatory_row)
there are 8 tests for very small boards (N <= 10)
there are 8 tests for cases without solution
there are 5 tests for small boards (10 < N <= 50)
there are 5 tests for medium boards (100 < N <= 500)
there are 5 tests for large boards (500 < N <= 1000)

Example:

For input of size=8, mandatory column=3 and mandatory row=0, your solution could return:

"...Q....\n......Q.\n..Q.....\n.......Q\n.Q......\n....Q...\nQ.......\n.....Q..\n"

giving the following board:

...Q....
......Q.
..Q.....
.......Q
.Q......
....Q...
Q.......
.....Q..

(Other solutions to this example are possible and accepted. The mandatory queen has to be in its position, in the example in the first row at col=3, row=0.)

partial solution

simple backtracking

At first , I just want to use simple backtracking, but from the row with the fixed queen. by searching the solution line by line until we find one solution or go through all the situation to show it has no solution. It gives correct result but timeout for large size tests

bitoperation backtracking

Similar to normal backtracking, but use bit operation to improve the speed.It is still not fast enough

miniconflict

The main idea is find the queen with most conflict and change its position. This way is much faster than normal backtracking but for this problem, it is hard to find the situation with no solution, and with one fixed posion queen, it is much less efficient than normal situation

solution

It is still using miniconflict. I found the idea in https://mp.weixin.qq.com/s/hH8FayUx8Gh_pUq2-FT9tg . The main change with my origin miniconflict algorithm is it uses many magic number to make the speed faster to get the final answer. However, I am not sure if this is the best solution as this could pass the kata but need some luck. I tried three times and then pass it, as it uses some random way to do that and sometimes it would return None for cases with answer.

n-queens-problem's People

Contributors

louiscaixuran avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.