Skip to content

Commit

Permalink
Fix iiitv#535: Add Backtracking algorithm (N-Queen Problem) [Go] (iii…
Browse files Browse the repository at this point in the history
…tv#536)

* n queen solution in golang added

* set n value by default

* variables renamed
  • Loading branch information
vatz88 authored and singhpratyush committed Oct 17, 2017
1 parent 957bb6b commit af4e87f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Longest Palindromic Substring](http://www.geeksforgeeks.org/longest-palindrome-substring-set-1/) | | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.cpp) | [:white_check_mark:](longest_palindromic_substring/LongestPalindromicSubstring.java) | [:white_check_mark:](longest_palindromic_substring/longest_palindromic_substring.py) | | [:white_check_mark:](longest_palindromic_substring/longestPalindromicSubstring.js) | |
| [Merge Sort](https://www.khanacademy.org/computing/computer-science/algorithms/merge-sort/a/overview-of-merge-sort) | [:white_check_mark:](merge_sort/merge_sort.c) | | [:white_check_mark:](merge_sort/MergeSort.java) | [:white_check_mark:](merge_sort/merge_sort.py) | [:white_check_mark:](merge_sort/merge_sort.go) | [:white_check_mark:](merge_sort/mergeSort.js) | [:white_check_mark:](merge_sort/MergeSort.cs) |
| [Modular Exponential](http://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/) | [:white_check_mark:](modular_exponential/modular_exponential.c) | | [:white_check_mark:](modular_exponential/ModularExponential.java) | [:white_check_mark:](modular_exponential/modular_exponential.py) | [:white_check_mark:](modular_exponential/modular_exponential.go) | [:white_check_mark:](modular_exponential/modularExponential.js) | |
| [N-Queen Problem](https://en.wikipedia.org/wiki/Eight_queens_puzzle) | | [:white_check_mark:](n_queen_problem/NQueenProblem.cpp) | [:white_check_mark:](n_queen_problem/NQueenProblem.java) | [:white_check_mark:](n_queen_problem/n_queen_problem.py) | | | |
| [N-Queen Problem](https://en.wikipedia.org/wiki/Eight_queens_puzzle) | | [:white_check_mark:](n_queen_problem/NQueenProblem.cpp) | [:white_check_mark:](n_queen_problem/NQueenProblem.java) | [:white_check_mark:](n_queen_problem/n_queen_problem.py) | [:white_check_mark:](n_queen_problem/n_queen_problem.go) | | |
| [Prime Factor](https://en.wikipedia.org/wiki/Prime_factor) | [:white_check_mark:](prime_factor/prime_factor.c) | | [:white_check_mark:](prime_factor/PrimeFactor.java) | [:white_check_mark:](prime_factor/prime_factor.py) | [:white_check_mark:](prime_factor/prime_factor.go) | [:white_check_mark:](prime_factor/primeFactor.js) | |
| [Prims](https://en.wikipedia.org/wiki/Prim%27s_algorithm) | [:white_check_mark:](prims/prims.c) | | [:white_check_mark:](prims/Prims.java) | | | [:white_check_mark:](prims/prims.js) | |
| [Quick Select](https://en.wikipedia.org/wiki/Quickselect) | [:white_check_mark:](quick_select/quick_select.c) | | [:white_check_mark:](quick_select/QuickSelect.java) | [:white_check_mark:](quick_select/quick_select.py) | | | |
Expand Down
68 changes: 68 additions & 0 deletions n_queen_problem/n_queen_problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Given a chess board having N×N cells
// place N queens on the board in such a way that no queen attacks any other queen.

// There can be a number of possible solutions for a given length of board.
// This implementation prints only one valid solution, it can be extended to print all possible valid solutions.

package main

import "fmt"

func main() {
boardSize := 8 // size of chess board (8 x 8)

// make board
board := make([][]int, boardSize)
for i := range board {
board[i] = make([]int, boardSize)
}

if PlaceQueen(&board, boardSize) {
for i := 0; i < boardSize; i++ {
for j := 0; j < boardSize; j++ {
fmt.Printf("%d ", board[i][j])
}
fmt.Print("\n")
}
} else {
fmt.Println("Not possible")
}
}

func isAttacked(board *[][]int, x, y, boardSize int) bool {
for i := 0; i < boardSize; i++ {
if (*board)[x][i] == 1 {
return true
}
if (*board)[i][y] == 1 {
return true
}
for j := 0; j < boardSize; j++ {
if (i-j == x-y) || (i+j == x+y) {
if (*board)[i][j] == 1 {
return true
}
}
}
}
return false
}

// PlaceQueen place queens on the board and will return true if it is possible to place all the queens else false
func PlaceQueen(board *[][]int, boardSize int) bool {
if boardSize == 0 {
return true
}
for i := 0; i < len(*board); i++ {
for j := 0; j < len(*board); j++ {
if !(isAttacked(board, i, j, len(*board))) {
(*board)[i][j] = 1
if PlaceQueen(board, boardSize-1) {
return true
}
(*board)[i][j] = 0
}
}
}
return false
}

0 comments on commit af4e87f

Please sign in to comment.