我應該為大學解決 8 個皇后問題,而我現在要做的步驟之一是解決如何讓所有皇后從當前元素正確并上下對角線。任何人都可以幫助我。我到目前為止是這樣的:
const prazno = "P";
const kraljica = "K";
let arr = [
[prazno,prazno,prazno,prazno,prazno,prazno,prazno,prazno],
[prazno,prazno,prazno,prazno,prazno,prazno,prazno,prazno],
[prazno,prazno,prazno,prazno,prazno,prazno,prazno,prazno],
[prazno,prazno,prazno,kraljica,prazno,prazno,prazno,prazno],
[kraljica,prazno,prazno,prazno,kraljica,prazno,prazno,prazno],
[prazno,kraljica,prazno,prazno,prazno,kraljica,prazno,kraljica],
[prazno,prazno,kraljica,prazno,prazno,prazno,kraljica,prazno],
[prazno,prazno,prazno,prazno,prazno,prazno,prazno,prazno]];
arr.forEach(a=>{console.log(a)});
prazno (P) 是空的,kraljica (K) 是皇后。順便說一句,它應該是這樣的:
[ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' ]
[ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' ]
[ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' ]
[ 'P', 'P', 'P', 'K', 'P', 'P', 'P', 'P' ]
[ 'K', 'P', 'P', 'P', 'K', 'P', 'P', 'P' ]
[ 'P', 'K', 'P', 'P', 'P', 'K', 'P', 'K' ]
[ 'P', 'P', 'K', 'P', 'P', 'P', 'K', 'P' ]
[ 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P' ]
嘗試從當前或右側對角添加所有皇后。我嘗試了對角線,但很快發現我只能使用以下代碼在兩個相遇的地方做那些:
const dijagonalniZbir = arr => {
let dijagonalniZbir = 0;
for(let i = 0; i < arr.length; i ){
for(let j = 0; j < arr[i].length; j ){
if(i === j && arr[i][j]=='K'){
dijagonalniZbir ;
};
};
};
return dijagonalniZbir;
};
但遺憾的是,這僅適用于 i 和 j 都匹配的對角線,我需要它用于當前元素,而不是整個陣列。
uj5u.com熱心網友回復:
抱歉,不得不改成“Q”和“E”(例子更小),還添加了一些皇后,所以所有對角線(從 3,3 開始)至少包含一個皇后:
const e = "E"; // Empty
const q = "Q"; // Queen
let arr = [
[q, e, e, e, e, e, e, e],
[e, e, e, e, e, e, e, e],
[e, e, e, e, q, e, e, e],
[e, e, e, q, e, e, e, e],
[q, e, e, e, q, e, e, e],
[e, q, e, e, e, q, e, q],
[e, e, q, e, e, e, q, e],
[e, e, e, e, e, e, e, e]
];
// utility to check if there's a queen
// on the coordinates
const isQueen = ({ x, y, arr }) => {
return arr[y][x] === q
}
// extracted functions to calculate diagonals
const getNext = {
'downRight': ({ x, y }) => [x 1, y 1],
'upLeft': ({ x, y }) => [x - 1, y - 1],
'upRight': ({ x, y }) => [x 1, y - 1],
'downLeft': ({ x, y }) => [x - 1, y 1],
}
// recursive function to gather all coordinates
// of queens in one diagonal direction (actually,
// this would work with any direction - only
// the getNext[something] function sets the
// pattern)
const getDiagonal = (direction) => {
return ({ x, y, arr }) => {
let ret = []
const [xNext, yNext] = getNext[direction]({ x, y })
// checking if next x, y coordinates are "in" the array
if (arr[xNext] == null || arr[yNext] == null) {
// if not, then recursion stops, an empty
// array is returned
return ret
} else {
// if yes, then check if they hold a queen
if (isQueen({ x: xNext, y: yNext, arr })) {
// if there's a queen on the next cordinates
// then push those coordinates to the return array
ret = [...ret, [xNext, yNext]]
}
// recursion goes: call the function with xNext, yNext -
// and the checking starts from "let ret = []" again,
// only with different x and y coordinates
return [...ret, ...getDiagonal(direction)({ x: xNext, y: yNext, arr })]
}
}
}
// creating the actual functions from the general
// getDiagonal
const getAllDownRight = getDiagonal('downRight')
const getAllUpLeft = getDiagonal('upLeft')
const getAllDownLeft = getDiagonal('downLeft')
const getAllUpRight = getDiagonal('upRight')
// setting starting position
const current = { x: 3, y: 3 }
// running the functions to see the queens
const downRight = getAllDownRight({ ...current, arr })
const upLeft = getAllUpLeft({ ...current, arr })
const upRight = getAllUpRight({ ...current, arr })
const downLeft = getAllDownLeft({ ...current, arr })
// output
console.log(downRight)
console.log(upLeft)
console.log(upRight)
console.log(downLeft)
演算法(思考)的步驟:
- 讓我們將 x 和 y 作為起始坐標
- 讓我們選擇最簡單的對角線:向下和向右(最簡單,因為這只是 x 和 y 的 1)。我們分別稱它們為
xNext&yNext - 你如何檢查是否有一個女王一步之遙?很簡單:
arr[y 1][x 1]->arr[yNext][xNext]看看它是否是一個queeny (isQeen()) - 你如何檢查兩步之外是否有女王?很簡單:
arr[y 2][x 2]->arr[yNext 1][xNext 1]看起來和一開始一樣,只是xandy改成了xNextandyNext...hmmm...這個模式可以用嗎?當然!創建一個遞回函式,該函式根據向它們加 1 的規則自動更改xNext&yNext檢查isQueen;然后重復。什么時候應該停止?在“棋盤的邊緣”(如果arr[yNext][xNext]是undefined,則演算法到達棋盤的末端。) - 如何概括這一點,以便我們可以將它用于不同的對角線?其他對角線(對這個函式來說)沒有別的,只是
x和y坐標中的一組不同的變化。
這就是上面片段中的全部內容。
uj5u.com熱心網友回復:
您正確識別了挑戰。您當前的代碼僅適用于大對角線,并且僅計算那里的皇后數量。
您需要將問題分解為較小的挑戰。撰寫一些輔助函式可以解決一些更大的挑戰。
一個檢查當前元素位置右側是否有皇后的函式,所以像這樣的輔助函式
const emptyToRight = (row, col, arr) => {
// logic to check the same row to the right
// and return false if a queen is found
// ...
return true;
}
撰寫這樣的函式應該是一個較小的挑戰,因此更容易完成
然后另一個輔助函式將是
const emptyToUpRight = (row, col, arr) => {
// logic to check the positions to up and right
// and return false if a queen is found
// ...
return true;
}
同樣對于另一個對角線方向,另一個輔助函式將是
const emptyToDownRight = (row, col, arr) => {
// logic to check the positions to down and right
// and return false if a queen is found
// ...
return true;
}
這有幫助嗎?這些輔助函式是您可以實作的嗎?在評論中告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/405306.html
標籤:
