穷举排列方法

static Seat data = new Seat();
public static void Run()
{
    for (int a = 0; a < 4; a++)
    {
        if (data.IsSelected(0, a))  //第1局编号0。如果已经被人坐了。
            continue;
        data.Selected(0, a, "A");   //第1局编号0。A坐a椅。
        for (int b = 0; b < 4; b++)
        {
            if (data.IsSelected(0, b))
                continue;
            data.Selected(0, b, "B");
            for (int c = 0; c < 4; c++)
            {
                if (data.IsSelected(0, c))
                    continue;
                data.Selected(0, c, "C");
                for (int d = 0; d < 4; d++)
                {
                    if (data.IsSelected(0, d))
                        continue;
                    data.Selected(0, d, "D");
                    for (int a2 = 0; a2 < 5; a2++)
                    {
                        if (a2 == 1)
                            continue;
                        if (data.IsSelected(1, a2))     //第2局编号1
                            continue;
                        if (data.IsSelected(0, a2, "A"))    //如果第1局A坐了a2椅
                            continue;
                        data.Selected(1, a2, "A");
                        for (int b2 = 0; b2 < 5; b2++)
                        {
                            if (b2 == 1)
                                continue;
                            if (data.IsSelected(1, b2))
                                continue;
                            if (data.IsSelected(0, b2, "B"))
                                continue;
                            data.Selected(1, b2, "B");
                            for (int c2 = 0; c2 < 5; c2++)
                            {
                                if (c2 == 1)
                                    continue;
                                if (data.IsSelected(1, c2))
                                    continue;
                                if (data.IsSelected(0, c2, "C"))
                                    continue;
                                data.Selected(1, c2, "C");
                                for (int d2 = 0; d2 < 5; d2++)
                                {
                                    if (d2 == 1)
                                        continue;
                                    if (data.IsSelected(1, d2))
                                        continue;
                                    if (data.IsSelected(0, d2, "D"))
                                        continue;
                                    data.Selected(1, d2, "D");

                                    data.Count++;       //可能的情况数加1
                                    Console.WriteLine("{0,5} {1}", data.Count, data.Current);

                                    data.UnSelected(1, d2);
                                }
                                data.UnSelected(1, c2);
                            }
                            data.UnSelected(1, b2);
                        }
                        data.UnSelected(1, a2);
                    }
                    data.UnSelected(0, d);
                }
                data.UnSelected(0, c);
            }
            data.UnSelected(0, b);
        }
        data.UnSelected(0, a);  //A起身(释放坐椅)
    }
}

 

posted @ 2016-06-01 15:25  nongfuxu  阅读(241)  评论(0)    收藏  举报