2015-05-04 50 views
2

我寫了一個遞歸(實際上我找到了遞歸在線)來獲取一組數字的所有可能的排列,但在某些情況下,由於大量的可能排列,我想添加一個If語句在經歷所有排列之前終止遞歸。我試過輸入一個return語句,但它似乎不起作用。手動終止遞歸

我在編碼方面有點新手,所以如果答案對每個人都是顯而易見的,我就是無法得到它。

class EntryPoint 
{   
    static void Main() 
    {   
     //input an initial sequence of the trains to schedule 
     Console.Write("Input train permutation"); 
     string inputLine = Console.ReadLine(); 

     GenerateTrainOrder GTO = new GenerateTrainOrder(); 
     GTO.InputSet = GTO.MakeCharArray(inputLine); 
     GTO.CalcPermutation(0); 
    } 
} 


class GenerateTrainOrder 
{ 
    private int elementLevel = -1; // elements examined iterates immediately after the CalcPermutation initiates so we must set it equal -1 to start from 0 
    private int[] permutationValue = new int[0]; 

    public int[,] Paths = new int[ParametersClass.timetableNumber, ParametersClass.trainsToSchedule]; 

    private char[] inputSet; 
    public char[] InputSet 
    { 
     get { return inputSet; } 
     set { inputSet = value; } 
    } 

    private int permutationCount = 0; 
    public int PermutationCount 
    { 
     get { return permutationCount; } 
     set { permutationCount = value; } 
    } 

    //transform the input from the console to an array for later use 
    public char[] MakeCharArray(string InputString) 
    { 
     char[] charString = InputString.ToCharArray(); 
     Array.Resize(ref permutationValue, charString.Length); 

     return charString; 
    } 

    public void CalcPermutation(int k) 
    {    
      elementLevel++; 
      permutationValue.SetValue(elementLevel, k); 

      //if we have gone through all the elements which exist in the set, output the results 
      if (elementLevel == ParametersClass.trainsToSchedule) 
      { 
       OutputPermutation(permutationValue); // output TrainOrder by passing the array with the permutation 
      } 

      //if there are elements which have not been allocated a place yet 
      else 
      { 
       for (int i = 0; i < ParametersClass.trainsToSchedule; i++) 
       { 
        //iterate until we come upon a slot in the array which has not been allocated an elements yet 
        if (permutationValue[i] == 0) 
        { 

         CalcPermutation(i); //rerun the code to allocate an element to the empty slot. the location of the empty slot is given as a parameter (this is how k increments) 

        } 
       } 
      } 

      elementLevel--; 
      permutationValue.SetValue(0, k); 
    } 

    private void OutputPermutation(int[] value) 
    { 
     int slot = 0; 

     foreach (int i in value) 
     { 
      Paths[permutationCount, slot] = Convert.ToInt16(Convert.ToString(inputSet.GetValue(i-1))); 

      slot++; 
     } 

     PermutationCount++; 
    } 
} 
+0

什麼是終止條件,你試圖把'return'放在哪裏?你是什​​麼意思「似乎不工作」? 'return'將始終終止函數並返回; –

回答

0

這是有點麻煩停止遞歸計算。最好的方法是使用全局變量(如果有對象,則使用私有類變量)。

這個變量表示,如果遞歸應停止與否:

bool stopRecursion = false; 

現在你計算你內心的每步之後更新這個變量:

if(_my_condition_is_met) stopRecursion = true; 

在你的遞歸調用方法開始你檢查這個變量的狀態:

public void CalcPermutation(int k) 
{ 
    if(stopRecursion) return; 
    ... 

而你檢查這個變量在尾部呃每次調用CalcPermutation

for (int i = 0; i < ParametersClass.trainsToSchedule; i++) 
{ 
    //iterate until we come upon a slot in the array which has not been allocated an elements yet 
    if (permutationValue[i] == 0) 
    { 
     CalcPermutation(i); 
     if(stopRecursion) return; 
     .... 

有了這個方法,你儘快放鬆甚至深呼水平滿足您的停止條件

+0

首先,如果遞歸函數只是返回結果來通知它,則不需要全局或字段必須停止。這是特別的。在多線程代碼中很重要。其次,.NET 4.5具有CancellationToken類來表明它應該取消的任何類型的函數。 –

+0

@PanagiotisKanavos OP說:「我有點新手」,所以取消令牌在這裏可能有點太多了。返回值當然是一個非常好的主意。 – DrKoch

+0

他談到「停止遞歸」而不是「退出方法」。單詞很重要 – Hellraiser

-1
for (int i = 0; i < ParametersClass.trainsToSchedule; i++) 
      { 
       //iterate until we come upon a slot in the array which has not been allocated an elements yet 
       if({your condition}){ 
        break; 
       } 
       if (permutationValue[i] == 0) 
       { 

        CalcPermutation(i); //rerun the code to allocate an element to the empty slot. the location of the empty slot is given as a parameter (this is how k increments) 

       } 
      } 
0

感謝大家的回覆。我設法讓它做的工作如下:

下CalcPermutation(我)線,我寫

if (permutationCount == ParametersClass.timetableNumber) 
{ 
     return; 
} 

我試着用不同的輸入運行了幾次,似乎工作得很好。