免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
javascript – 5×5網(wǎng)格中所有可能的移動(dòng)?
s o o o oo o o o oo o o o oo o o o oo o o o e

如何計(jì)算所有可能的路徑,而不使用相同的方格兩次,一個(gè)人可以從s到e?

我創(chuàng)建了一個(gè)網(wǎng)格數(shù)組[[1,1] … [5,5]],但我不知道這是否有效.

我還繪制了可能的方塊,并試圖創(chuàng)建一個(gè)記錄和檢查和大量的垃圾.

我可以在這里使用任何標(biāo)準(zhǔn)配方嗎?

解決方法:

您可以使用相當(dāng)多的標(biāo)準(zhǔn)路徑尋找算法.

這與javascript無(wú)關(guān).

你可以使用一個(gè)沒有啟發(fā)式的algorhythm,你不應(yīng)該停止第一個(gè)解決方案.

以下是如何做到這一點(diǎn):

訣竅是你需要將已經(jīng)訪問的方塊存儲(chǔ)在一個(gè)列表中,并檢查你是否在每一步都重新訪問其中一個(gè)方塊.

另一個(gè)技巧是你需要相鄰方塊之間的明確順序. (像頂部/右側(cè)/底部/左側(cè).這是一個(gè)非常愚蠢的算法,但對(duì)于這種特殊情況很好.)

你還需要能夠識(shí)別正方形(它的位置是可能的)

考慮一個(gè)遞歸函數(shù)(例如將其命名為Visit):

function visit(square) {    add the square to the pathlist //pathlist is not a list of paths but a list of squares which is the current path    if (square is the goal) {        add a copy of the pathlist to the goalslist    }    else {        for (each adjacency in square.adjacencies) { // this can be calculated by adding  1 and -1 to the coordinates, and checking if its overflowing (less then one/more than five)            if (adjacency is in pathlist) {                //do nothing we have already been here            }            else {                visit(adjacency)            }        }    }    remove square from the pathlist!!}

通過訪問(開始)開始這個(gè)algorythm.你可以在goallist中得到你的結(jié)果,這是一個(gè)有希望的路徑列表.

此外,它只有一半的javascript-half偽代碼,但很容易從中編寫javascript.

編輯:享受解決方案:

<script type="text/javascript">var start = [1,1],    goal = [5,5],    pathList = [],    solutionList = [],    solutionCount = 0,    width = 5,    height = 5;function squareInArray(square, array) {    var i = 0,        x = square[0],         y = square[1];    for (i = 0; i < array.length; i  ) {        if (x == array[i][0] && y == array[i][1]) {            return true;        }    }    return false;}function visit(square) {    var i = 0,        x = square[0],         y = square[1],        adjacencies = [[x-1,y],[x 1,y],[x,y 1],[x,y-1]];    pathList.push(square);    if (x == goal[0] && y == goal[1]) {        var solution = pathList.slice(0); //copy trick        solutionList.push(solution);        solutionCount  ;        //alert(solution);    }    else {        for (i = 0; i < adjacencies.length; i  ) {            if (adjacencies[i][0] < 1 || adjacencies[i][0] > width || adjacencies[i][1] < 1 ||adjacencies[i][1] > height) {                //overflow            }            else {                if (squareInArray(adjacencies[i], pathList)) {                    //do nothing we have already been here                }                else {                    visit(adjacencies[i]);                }            }        }    }    pathList.pop();}visit(start);alert(solutionCount);</script>

8512個(gè)進(jìn)球.還有人應(yīng)檢查我的代碼是否正確.

來源:http://www.icode9.com/content-1-217851.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Javascript不斷隨機(jī)生成正方形
TypeScript 入門教程 | 菜鳥教程
圖靈社區(qū) : 閱讀 : 通過對(duì)象圖學(xué)習(xí)JavaScript [之二]
javascript的全局變量和局部變量 | 前端開拓者
深入理解JavaScript系列(8):S.O.L.I.D五大原則之里氏替換原則LSP
IE10 Error.stack 讓腳本調(diào)試更加方便快捷
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服