五皇后問題

function placeQ (col: number) {

    if (col == 5) {

        basic.pause(100)

        return 1

    } else {

        for (let row = 0; row <= 4; row++) {

            if (isValid(row, col) == 1) {

                led.plot(col, row)

                basic.pause(0)

                placeQ(col + 1)

                led.unplot(col, row)

            }

        }

    }

    return 0

}

function isValid (row: number, col: number) {

    for (let index = 0; index <= 4; index++) {

        if (led.point(index, row)) {

            return 0

        }

        if (led.point(col, index)) {

            return 0

        }

    }

    list = [

    [-1, -1],

    [-1, 1],

    [1, -1],

    [1, 1]

    ]

    for (let value of list) {

        nowRow = row

        nowCol = col

        while (nowRow >= 0 && nowRow < 5 && (nowCol >= 0 && nowCol < 5)) {

            if (led.point(nowCol, nowRow)) {

                return 0

            }

            nowRow += value[0]

            nowCol += value[1]

        }

    }

    return 1

}

let nowCol = 0

let nowRow = 0

let list: number[][] = []

while (true) {

    placeQ(0)

}



def placeQ(col: number):

    if col == 5:

        basic.pause(100)

        return 1

    else:

        for row in range(5):

            if isValid(row, col) == 1:

                led.plot(col, row)

                basic.pause(0)

                placeQ(col + 1)

                led.unplot(col, row)

    return 0

def isValid(row2: number, col2: number):

    global list2, nowRow, nowCol

    for index in range(5):

        if led.point(index, row2):

            return 0

        if led.point(col2, index):

            return 0

    list2 = [[-1, -1], [-1, 1], [1, -1], [1, 1]]

    for value in list2:

        nowRow = row2

        nowCol = col2

        while nowRow >= 0 and nowRow < 5 and (nowCol >= 0 and nowCol < 5):

            if led.point(nowCol, nowRow):

                return 0

            nowRow += value[0]

            nowCol += value[1]

    return 1

nowCol = 0

nowRow = 0

list2: List[List[number]] = []

while True:

    placeQ(0)