0

我试图以 Yew 应用程序解决数独问题。使用 yew="0.17.4" 版本。但低于错误

Uncaught RangeError: Maximum call stack size exceeded
    at dlmalloc::dlmalloc::Dlmalloc::malloc::hb6b25cc27fa2f08c (wasm_bg.wasm:wasm-function[55]:0x5f0d)
    at __rdl_alloc (wasm_bg.wasm:wasm-function[327]:0x23108)
    at __rust_alloc (wasm_bg.wasm:wasm-function[362]:0x23480)
    at alloc::raw_vec::RawVec<T,A>::reserve::h02d54539a997f0ac (wasm_bg.wasm:wasm-function[211]:0x207e4)
    at <&mut W as core::fmt::Write>::write_str::he2cb1047a173d57a (wasm_bg.wasm:wasm-function[288]:0x22a02)
    at core::fmt::Formatter::pad_integral::hfd6532b3a41ee584 (wasm_bg.wasm:wasm-function[104]:0x16c73)
    at core::fmt::num::imp::fmt_u64::h5081cd6222065ff2 (wasm_bg.wasm:wasm-function[156]:0x1cecb)
    at core::fmt::num::imp::<impl core::fmt::Display for u32>::fmt::hd7239ff7b4d279a9 (wasm_bg.wasm:wasm-function[388]:0x23617)
    at <&T as core::fmt::Display>::fmt::h8b339f7b56577d63 (wasm_bg.wasm:wasm-function[392]:0x2364d)
    at core::fmt::write::h8b996d8af01475c9 (wasm_bg.wasm:wasm-function[103]:0x16b15)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=16e0219070bd7245a50a209de0ce2cd4

4

1 回答 1

1

让我们回顾一下您的solve功能:

fn solve(&mut self)-> bool{
    match self.getemptycell(){
        Some((row,col)) => {
            for value in 1..10{
                if self.IsValidValue(row,col,value){
                    let index = (row*8 + row) + col ;
                    self.cellule[(row*8+row)+col].value = value;
                    log::info!("value = {}",value);
                    if self.solve(){ // <----------------------------- Recursion starts here
                        return true

                    }
                    self.cellule[(row*8+row)+col].value = 0;
                }

            }
            return false
        },
        _      =>   { 
            // log::info!("{:?}",self.cellule);
            return true
        },

    }

}
  • 该函数检查一些值,如果这些值符合条件,则函数再次调用自身(第 90 行)。
  • 然后它会在相同的条件下检查相同的值并再次调用自己一次。
  • 还有一次。
  • 还有一次。
  • ...
  • 直到堆栈溢出。

您应该更加小心递归调用。

于 2021-01-05T17:51:32.150 回答