【问题标题】:find all paths from top left corner to right bottom corner assembly查找从左上角到右下角组件的所有路径
【发布时间】:2022-06-25 02:11:15
【问题描述】:

我是汇编编程的新手,目前正在学习在线课程。

最初的问题是计算从左上角到右下角的路径数。但我在这里找到了一个很好的解决方案:

https://www.geeksforgeeks.org/count-possible-paths-top-left-bottom-right-nxm-matrix/

基于组合学解决方案,我应该能够以二进制方式找到所有路径。 第一个问题,你知道计算路径的更快方法吗?

搜索打印所有路径的解决方案:

https://www.geeksforgeeks.org/print-all-possible-paths-from-top-left-to-bottom-right-of-a-mxn-matrix/

但没有注意到任何使用二进制方法似乎足以组装。 我在网上搜索了一下发现:

https://www.baeldung.com/cs/generate-k-combinations

旋转门算法非常详细,我将其计算为 O(组合数)* O(矩阵的宽度或高度(用于打印)-1)* O(分支循环)时间复杂度和 O(宽度或高度 + 1) 在空间上。第二个问题这是一个正确的假设吗?如果不是,那么正确的复杂度是多少?它是否比为查找此问题的所有路径而发布的其他解决方案更快?据说是 O(2^(width*height))

第三个问题:这个算法是谁写的?在哪里可以找到更多类似的内容?

最后,我将发布我的 fasm 新手 32 位汇编意大利面代码,应该适用于大于 3 x 3 小于 32 x 32 的矩阵(不建议超过 16 x 16,这已经是很多组合了并且只省略打印说明),任何改进都非常受欢迎。谢谢。

    format PE console
entry start



include 'win32a.inc' 

; ===============================================
struct MAT
    h   db  ?       ; X coordinate.
    w   db  ?       ; Y coordinate.
ends



; ===============================================
section '.bss' readable writeable
    ; Declare the uninitialized table in memory:
    path_matrix     MAT  ?
    count           dd  ?
    indices         db path_matrix.w - 1 dup ?
    end_indices:
    

; ===============================================
section '.text' code readable executable

start:

    call    read_hex
    mov     [path_matrix.h], al ; down will be 0
    call    read_hex
    mov     [path_matrix.w], al ; right will be 1
    
    dec     eax
    mov     ecx, eax
    
initialize: 
    mov     ebx, ecx
    dec     ebx
    mov     byte[indices+ecx], bl
    loop    initialize
    movzx   ebx, [path_matrix.h]
    dec     ebx
    add     ebx, eax
    mov     byte[indices+eax+1], bl 
    mov     eax, ebx
    
    

print_combination:
    inc     [count]
    movzx   ebx, [end_indices - indices]
    dec     ebx
    xor     eax, eax
    
    
print_loop:
    xor     esi, esi
    inc     esi
    mov     cl, byte[indices + ebx ]
    shl     esi, cl
    xor     eax, esi
    dec     ebx
    cmp     ebx, 0
    jnz     print_loop
    call    print_eax_binary


    
branch:
    lea     edi, [indices +1]
    movzx   eax, [path_matrix.w] ; check if withd is eaven, if true matrix is odd (w -1)
    shr     eax, 1
    jnc     odd
    

eaven:
    movzx   eax, byte[edi]
    cmp     eax, 0
    jle     eaven_indice
    dec     eax
    mov     byte[edi], al
    jmp     print_combination 
    

eaven_indice:
    inc     edi
        

try_to_increase:
    movzx   ebx, byte[edi]
    inc     ebx
    cmp     bl, [edi+1]
    jl      increase
    lea     ecx, [edi-indices+1]
    cmp     cl, [path_matrix.w]
    jl      increase_indice
    jmp     fin
    
    
increase:
    mov     byte[edi], bl
    dec     ebx
    mov     byte[edi-1], bl
    jmp     print_combination

    
    
odd:
    movzx   eax, byte[edi]
    inc     eax
    cmp     al, [edi+1]
    jge     increase_indice
    mov     byte[edi], al
    jmp     print_combination

    
increase_indice:
    inc     edi
    

try_decrease:
    lea     eax, [edi - indices]
    cmp     byte[edi], al
    jl      eaven_indice    

    
decrease:
    movzx   ebx, byte[edi-1]
    mov     byte[edi], bl
    sub     eax, 2
    mov     byte[edi-1], al
    jmp     print_combination
    
    
fin:
    mov     eax, [count]
    call    print_eax

    ; Exit the process:
    push    0
    call    [ExitProcess]

include 'training.inc'

【问题讨论】:

    标签: algorithm assembly matrix x86 fasm


    【解决方案1】:

    解决方案不是二元的,因为从顶部或左侧的路径可以重叠 - 产生重复。向后工作解决方案是加法的 - 左侧路径和顶部路径的总和。

    这导致了一个简单的解决方案:

    _m      = 16
    _n      = 16
    
    ddp     rq _m
    
            ; initialize first position
            mov [ddp + (_n-1)*8], 1
            mov ecx, _m
    outer:
            push rcx
            mov ecx, _n
            xor eax, eax
    inner:
            add rax, [ddp + (rcx-1)*8]
            mov [ddp + (rcx-1)*8], rax
            loop inner
            pop rcx
            loop outer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      相关资源
      最近更新 更多