I vaguely felt something wrong. I tried to add a statistic for the can_put function. Theoretically, the number of can_put function executions is O (t)
CNT: 352, times: 72378
Real 0m0. 008 s
User 0m0. 004 s
Sys 0m0. 000 s
CNT: 352, times: 1222703298
Real 0m18. 207 s
User 0m18. 193 s
Sys 0m0. 004 s
CNT: 352, times: 459314
Real 0m0. 022 s
User 0m0. 016 s
Sys 0m0. 000 s
The first type is recursion. Besides stack consumption, it should be economical enough.
The second is traversal, which is very economical in space and time-consuming. It is O (n ^ N), that is, the worst.
The third is optimized traversal, and pruning is added (which is probably the case ?), The efficiency is greatly improved, but it is still very bad compared with recursion. I don't know if the algorithm is faulty or the price paid for the stack space?
When I finish my work and take a look at it at rest, I always feel that the third algorithm should not be so time-consuming. Where should I write it wrong.
Actual comparison of the time complexity of the eight queens three algorithms