Lua程序设计第4版第14章课后练习答案

14.1

function f141(a,b)
    local c ={}
    for i = 1, #a do
        c[i] = {}
        for j = 1, #a[i] do
            c[i][j]  = a[i][j]+b[i][j]
        end
    end
    for i = 1, #c do
        for j = 1, #c[i] do
            io.write(c[i][j].."\t")
        end
        print()
    end
end
a = {{1,2,3},{4,5,6},{7,8,9}}
b = {{1,2,3},{4,5,6},{7,8,9}}
f141(a,b)

14.2

--f142
function Queue_New()
    return {first = 0,last = 0}
end
function pushFirst(q,value)
    if Empty(q) then
        q.last = q.last-1
    end
    q.first = q.first-1
    q[q.first] = value
end
function popFirst(q)
    if Empty(q) then
        print("nothing to pop")
        return
    end
    local v = q[q.first]
    if q.first==q.last then
        q.last = 0
        q.first = 0
        return v
    end --只有一个元素
    q.first= q.first+1
    return v
end
function pushLast(q,value)
    if Empty(q) then
        q.first = q.first+1
    end
    q.last = q.last+1
    q[q.last] = value
end
function popLast(q)
    if Empty(q) then
        print("nothing to pop")
        return
    end
    local v = q[q.last]
    if q.last==q.first then
        q.last = 0
        q.first = 0
        return v
    end--只有一个元素
    q.last = q.last-1
    return v
end
function Empty(q)
    return q.last==q.first and q.first==0
end
function displayLst(q)
    while Empty(q)==false do
        local v = popFirst(q)
        io.write(v.." ")
    end
end
q = Queue_New()
--pushLast(q,10)
--pushLast(q,11)
--pushLast(q,12)
--pushLast(q,13)
--displayLst(q)
--print(q.first.."-"..q.last)

14.3 14.4

function name2node(graph,name)
    local node = graph[name]
    if not node then
        node = {name = name,adj = {}}
        graph[name] = node
    end
    return node
end

function readgraph(filename)
    local graph = {}
    for line in io.lines(filename) do
        local namefrom,nameto,Label = string.match(line,"(%S+)%s+(%S+)%s+(%d+)")
        local from = name2node(graph,namefrom)
        local to = name2node(graph,nameto)
        temp = {[1] = true,[2] = Label}
        from.adj[to] = temp
    end
    return graph
end

-- a->b 的最短路径
function Dijksra(graph,a,b)
    local shortDis = {} --各节点到a的最短路径集合
    local parent = {}
    local visited = {} --判断节点是否遍历过,遍历过节点为true代表在S集合中,没有遍历过代表为false在V-S集合中
    local cnt = 1
    local total = 0
    -- 初始化parent都为a
    -- 初始化shortDis都为a到各节点的距离
    -- 初始化visited 除了a结点其他结点都未遍历过
    for name,adj in pairs(graph) do
        if name == a.name then
            shortDis[name] = math.huge-1
            parent[name] = -1
            visited[name] = true
        else
            --初始化为a结点直接到b结点的距离
            local node=  name2node(graph,name)
            if a.adj[node] ~=nil then
                -- get node name
                shortDis[node.name] =math.tointeger(a.adj[node][2])
            else
                shortDis[node.name] = math.huge-1
            end
            parent[node.name] = a.name
            visited[node.name] = false
        end
        total = total+1
    end
    while cnt<total do
        -- 获得V-S中的最短路径结点
        local minNode
        local minDis = math.huge
        for name,dis in pairs(shortDis) do
            if visited[name] == false then
                if shortDis[name]<minDis then
                    minDis = shortDis[name]
                    minNode = name2node(graph,name)
                end
            end
        end
        if minNode~=nil then
            visited[minNode.name] = true
        end
        cnt = cnt+1
        -- 更新以minNode结点为父亲的其他最短路径结点
        for name,dis in pairs(shortDis) do
            if visited[name]== false then
                local newNode = name2node(graph,name)
                if minNode.adj[newNode]~=nil then
                    local newDis = math.tointeger(minNode.adj[newNode][2])+minDis
                    if shortDis[name]>newDis then
                        shortDis[name] = newDis
                        parent[name] = minNode.name
                    end
                end
            end
        end
    end

    s = ""
    tmp = b.name
    while tmp~=-1 do
        s = s..tmp
        tmp = parent[tmp]
    end
    s = string.reverse(s)
    print("路径为:"..s)
end

graph =  readgraph("graph")
anode = name2node(graph,"a")
bnode = name2node(graph,"b")
Dijksra(graph,anode,bnode)

恶心我

发布了81 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Icecoldless/article/details/104092070
今日推荐