Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jojorancu/NQueen
Basically it is a solution for "N-Queen" Problem. I added mix of humor to the code. Sounds like "The King" wanted date all "The Queen" :)
https://github.com/jojorancu/NQueen
algorithm go golang nqueens-problem
Last synced: 3 months ago
JSON representation
Basically it is a solution for "N-Queen" Problem. I added mix of humor to the code. Sounds like "The King" wanted date all "The Queen" :)
- Host: GitHub
- URL: https://github.com/jojorancu/NQueen
- Owner: jojorancu
- Created: 2018-07-20T02:48:40.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-22T15:03:28.000Z (over 6 years ago)
- Last Synced: 2024-08-02T23:27:30.672Z (6 months ago)
- Topics: algorithm, go, golang, nqueens-problem
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-golang-repositories - NQueen - Queen" Problem. I added mix of humor to the code. Sounds like "The King" wanted date all "The Queen" :) (Repositories)
README
[![Build Status](https://travis-ci.org/jojorancu/NQueen.svg?branch=master)](https://travis-ci.org/jojorancu/NQueen)
# NQueen - ProblemBasically this is a way of N-Queen problem could be solved by using **`DFS-Backtracking Algorithm`** and of course recursively.
Author added mix of humor to the code e.g. `firstLady` until `eigthBaby`is a named variable for testing purposes that points to the **first queen** until the **last queen** to be added (since currently the N is 8 for my testing code).
Another example is `Board` struct has `AbleToDate` function like this:
```go
func (b *Board) AbleToDate(homewrecker Queen) bool {
for _, queen := range b.Queens {
if homewrecker.MadnessPotential(queen) {
return false
}
}
return true
}
```
This function check whether the `homeWrecker` as a `Queen` could be able to fill up the list `Queens` that `Board` has or not.I know right, you might found another one function that `Queen` has. `MadnessPotential` function contain all the constraints **OR** you could say the ***requirements*** that fulfill to date **`The King`**.
And... ***Ba Dum Tss*** here are the ***requirements*** :
* There should not be Queens on the same **row**.
* And Queens are not allowed on the same **column** too.
* Queens are also not allowed on the same **diagonal**.*Voila!* Here is the function:
```go
func (q1 *Queen) MadnessPotential(q2 Queen) bool {
return q1.X == q2.X || q1.Y == q2.Y || math.Abs(float64(q1.X-q2.X)) == math.Abs(float64(q1.Y-q2.Y))
}
```## Usage
This repository doesn't have any dependency to any library. Create new directory:
```sh
cd /to/your/dev/dir
mkdir nameyouwanted
cd nameyouwanted
```
Using `git clone` to pull the repository:
```sh
git clone https://github.com/jojorancu/NQueen.git
```
There will be **`NQueen`** directory contains the code.## Test
Wanted to test ? Change the directory to the **`NQueen`** directory:
```sh
cd NQueen/
```Use this syntax at the root directory:
```sh
go test -v ./...
```## Author
[@jojorancu](https://twitter.com/jojorancu "jojorancu / CJSparrow / Jonathan Surya Laksana")