本文介绍了递归的真实例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是现实世界问题,其中递归方法是除深度优先搜索 (DFS) 之外的自然解决方案?

What are real-world problems where a recursive approach is the natural solution besides depth-first search (DFS)?

(我不考虑河内塔斐波那契数,或阶乘现实世界问题.在我看来,它们有点做作.)

(I don't consider Tower of Hanoi, Fibonacci number, or factorial real-world problems. They are a bit contrived in my mind.)

推荐答案

这里有很多数学例子,但你想要一个真实世界的例子,所以稍加思考,这可能是我能提供的最好的:

There are lots of mathy examples here, but you wanted a real world example, so with a bit of thinking, this is possibly the best I can offer:

您发现一个人感染了特定的传染性感染,这种感染是非致命性的,并且可以快速自我修复(A 型),除了五分之一的人(我们将其称为 B 型)永久感染它并没有表现出任何症状,只是充当传播者.

You find a person who has contracted a given contageous infection, which is non fatal, and fixes itself quickly( Type A) , Except for one in 5 people ( We'll call these type B ) who become permanently infected with it and shows no symptoms and merely acts a spreader.

当 B 型感染大量 A 型时,这会造成非常恼人的破坏浪潮.

This creates quite annoying waves of havoc when ever type B infects a multitude of type A.

您的任务是追踪所有 B 型并对其进行免疫以阻止疾病的发生.不幸的是,你不能在全国范围内对所有人进行治疗,因为 A 型人也对适用于 B 型的治疗方法过敏.

Your task is to track down all the type Bs and immunise them to stop the backbone of the disease. Unfortunately tho, you cant administer a nationwide cure to all, because the people who are typeAs are also deadly allergic to the cure that works for type B.

您要这样做的方式是社会发现,假设一个受感染的人(A 型),选择他们上周的所有联系人,在堆上标记每个联系人.当您测试一个人被感染时,将他们添加到跟进"队列中.当一个人是B型的时候,把他们加到头部的跟进"中(因为你想这么快停下来).

The way you would do this, would be social discovery, given an infected person(Type A), choose all their contacts in the last week, marking each contact on a heap. When you test a person is infected, add them to the "follow up" queue. When a person is a type B, add them to the "follow up" at the head ( because you want to stop this fast ).

处理完指定人员后,从队列前面选择该人员并根据需要进行免疫接种.获取他们之前未访问过的所有联系人,然后测试他们是否被感染.

After processing a given person, select the person from the front of the queue and apply immunization if needed. Get all their contacts previously unvisited, and then test to see if they're infected.

重复直到感染者队列变为0,然后等待再次爆发..

Repeat until the queue of infected people becomes 0, and then wait for another outbreak..

(好吧,这有点迭代,但它是解决递归问题的迭代方法,在这种情况下,广度优先遍历人口基数试图发现问题的可能路径,此外,迭代解决方案通常更快并且更有效,我强迫性地消除了无处不在的递归,使其成为本能......该死的!)

( Ok, this is a bit iterative, but its an iterative way of solving a recursive problem, in this case, breadth first traversal of a population base trying to discover likely paths to problems, and besides, iterative solutions are often faster and more effective, and I compulsively remove recursion everywhere so much its become instinctive. .... dammit! )

这篇关于递归的真实例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 04:40