第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用廣度優(yōu)先搜索:如何到達(dá)終點(diǎn)頂點(diǎn)?

使用廣度優(yōu)先搜索:如何到達(dá)終點(diǎn)頂點(diǎn)?

月關(guān)寶盒 2022-09-14 17:30:23
我不確定為什么我的代碼沒(méi)有返回正確的路徑頂點(diǎn)。它返回[a b c]而不是[a c f],我不知道為什么。我在這里是否遺漏了什么或在我的算法中做錯(cuò)了什么?注: getNeighbors(字符串頂點(diǎn))在其參數(shù)中返回頂點(diǎn)的連接邊。這是測(cè)試:我的代碼停止在“斷言等式(”c“,route.next())”,因?yàn)樗祷亍癰”而不是“c”。我的代碼的當(dāng)前輸出是 [a b c],預(yù)期的是 [a c f]public class PathingTest {    @Test    public void testPathing(){        Graph cycle = new Graph("graphs/cycle.json");        Iterator<String> route = cycle.getRoute("d", "b").iterator();        assertEquals("d",route.next());        assertEquals("b",route.next());        assertFalse(route.hasNext());        Graph tree = new Graph("graphs/tree.json");        route = tree.getRoute("a", "f").iterator();        assertEquals("a",route.next());        assertEquals("c", route.next());        assertEquals("f", route.next());        assertFalse(route.hasNext());        Graph disconnected = new Graph("graphs/disconnected.json");        assertEquals(null, disconnected.getRoute("a", "f"));    }}
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

變量和變量具有不同的用途,但在您的情況下,它們以相同的方式進(jìn)行更新,這是不正確的。queuevisited


很快,您將在處理其父節(jié)點(diǎn)時(shí)將節(jié)點(diǎn)添加到 (這意味著在將來(lái)的某個(gè)時(shí)間點(diǎn),該節(jié)點(diǎn)也希望得到處理)。同時(shí),只有在處理完節(jié)點(diǎn)(將其子節(jié)點(diǎn)添加到隊(duì)列)后,才將其添加到 該節(jié)點(diǎn)。queuevisited


您的循環(huán)應(yīng)如下所示(請(qǐng)注意插入的位置)。whilevisited


while (!queue.isEmpty()) {

    String current = queue.remove();

    path.add(current);


    visited.add(current);


    if (current == end) {

        return path;

    }


    Iterator<String> neighbors = getNeighbors(start).iterator();

    while (neighbors.hasNext()) {

        String n = neighbors.next();


        if (!visited.contains(n)) {

            queue.add(n);

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-09-14
  • 1 回答
  • 0 關(guān)注
  • 75 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)