3

有 1 个候选人要接受 n 个面试官的面试,因此总共需要 n 个连续的位置,例如下表说明了四个面试官(I1、I2、I3、I4)在四次时间的可用性插槽(S1、S2、S3、S4)。表中的 1 表示对应的面试官在对应的时间段有空。

例如,第一个采访者 I1 在时间段 S1 和 S2 可用。

每位面试官只能接受一次面试,所有四个时段都应依次进行,如 S1->S2->S3->S4

在每个位置找到面试官的所有可能组合。图中就是这样一个例子,

在此处输入图像描述

算法

让我们拿三组(挖掘和算法是不同的)

s1 =  array("I1","I2","I3")
s2 = array("I1","I2")
s3 = array("I2","I3")
interviewr_slot = array('slot1'=>s1,'slot2'=>s2,'slot3'=>s3,'slot4'=>null)

count = 3 //it can be any

stack = array()

possibility = array() 

traced = array();

myoutput = rec_function(interviewr_slot)

function rec_func($interviewr_slot){    
        static slot = 0;        

        slot++;

        possibility = interviewr_slot['slot']

        if(possibility != null)
        {   
            push(stack,traced)

            reset(our_input);

            our_input =  array();
               for( i = slot; i<= n+1, i++)
               {
                    our_input[sloti] = si;
               }

            foreach(possibility as k=>v)
            {

                if(!in_array(v, trace))
                {
                    array_push(traced, v)
                    rec_func(our_output)
                }
            }
        }
        else
        {
            push(output_array,traced)
        }       
        slot--
        traced = pop(stack)

        our_output = json.stringify(output_array)
        return our_output
    }
4

2 回答 2

1

我认为您必须使用搜索树。这是 C# 中的一个实现

class InterviewSequence
{
    List<string> Slots;
    List<string> Sequences;
    int iNumbers; // numbers of interviews/slots

    public InterviewSequence()
    {
        this.Slots = new List<string>();
        this.Slots.Add("ABCD"); // Available interviewers
        this.Slots.Add("AB"); // do..
        this.Slots.Add("BC");
        this.Slots.Add("BCD");
        this.iNumbers = this.Slots.Count;
        this.Sequences = new List<string>();
        string slotAviability = this.Slots[0];
        foreach (char c in slotAviability)
        {
            string sequence = "" + c;
            NextInterview(0, sequence);
        }
        foreach (string seq in this.Sequences)
        {
            Console.WriteLine(seq);
        }
    }

    private void NextInterview(int slot, string sequence)
    {
        string slotAviability = this.Slots[slot++];
        foreach (char c in slotAviability)
        {
            // Interviewer not booked in the sequence?
            if (sequence.IndexOf(c) < 0)
            {
                string val = sequence + c;
                if (slot == this.iNumbers - 1) // Last slot?
                {
                    this.Sequences.Add(val);
                }
                else
                {                       
                    NextInterview(slot, val); // goto next slot
                }
            }
        }
    }
}
于 2014-11-13T21:32:41.680 回答
1

使用 PHP 循环最终数组并以相反的顺序跟踪每个数组元素以获得结果

<?php
    $s1 = array("I1","I2","I3");
    $s2 = array("I1","I2","I3");
    $s3 = array("I1","I2","I3");
    $interviewr_slot = array('1'=>$s1,'2'=>$s2,'3'=>$s3);   
    $flag = 0;
    $len = count($interviewr_slot);
    for($i = $len; $i>= 1; $i--){
        if($flag == 0){
            foreach ($interviewr_slot[$i] as $key => $value) {
                $myarray[$key] = array($value);             
            }
            $flag = 1;
        }else{
            $checkarray = $myarray;
            unset($myarray);
            $myarray = array();
            foreach ($interviewr_slot[$i] as $key => $value) {
                foreach($checkarray as $k=>$v){
                    if(!in_array($value, $v)){
                        array_push($v, $value);
                        array_push($myarray, $v);
                    }   
                }
            }
        }       
    }
    var_dump($myarray);
?>

输出:

array (size=6)
  0 => 
    array (size=3)
      0 => string 'I3' (length=2)
      1 => string 'I2' (length=2)
      2 => string 'I1' (length=2)
  1 => 
    array (size=3)
      0 => string 'I2' (length=2)
      1 => string 'I3' (length=2)
      2 => string 'I1' (length=2)
  2 => 
    array (size=3)
      0 => string 'I3' (length=2)
      1 => string 'I1' (length=2)
      2 => string 'I2' (length=2)
  3 => 
    array (size=3)
      0 => string 'I1' (length=2)
      1 => string 'I3' (length=2)
      2 => string 'I2' (length=2)
  4 => 
    array (size=3)
      0 => string 'I2' (length=2)
      1 => string 'I1' (length=2)
      2 => string 'I3' (length=2)
  5 => 
    array (size=3)
      0 => string 'I1' (length=2)
      1 => string 'I2' (length=2)
      2 => string 'I3' (length=2)
于 2014-11-14T08:53:42.860 回答