mchirico / zDaily

Playground to hack and test ideas with Zoe
1 stars 2 forks source link

Day 93: Show work on Queen's Problem #102

Open mchirico opened 3 years ago

mchirico commented 3 years ago

Work on Queen's ... End Jan 18th

tacomonkautobot[bot] commented 3 years ago

mchirico, Thanks for opening this issue!

ZoeChiri commented 3 years ago

oof



package nqueens

import "fmt"
//true if it is safe
func Safe(board[][] int, row int, col int)bool{
    var x,y int = 0,0
    for (x = 0; x < col; x++) {
    if (board[row][x] == 1)
    return false;

    for (x = row, y = col; x >= 0 && y >= 0; x--, y--)
    if board[x][y] == 1
    return false;

    for (x = row, y = col; y >= 0 && x < 4; x++, y--)
    if board[x][y] == 1
    return false;

    }

}

func Solve(board[][]int, col int){

}
mchirico commented 3 years ago

Great... but, maybe valid Go with tests?

func Safe(board [][]int, row int, col int) bool {
    var x, y int = 0, 0
    for x := 0; x < col; x++ {
        if board[row][x] == 1 {
            return false
        }
    }

    for x, y := row, col; x >= 0 && y >= 0; x, y = x-1, y-1 {
        if board[x][y] == 1 {
            return false
        }

       ...

Note above for x, y := row, col; x >= 0 && y >= 0; x, y = x-1, y-1. Go doesn't support multiple x--,y-- in for loop.