Comment detail

魔方分割数 (Nested Flatten)

N=5のとき、答えは3245664通り。PemmtiumM 1GHz, メモリ768MBのノートPCでは、TimeSpan = 00:03:07.3363606でした。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
List<List<int>> list = new List<List<int>>();
int counter = 0;

int Solve(int n)
{
  List<int> numbers = new List<int>();
  for (int i = 0; i < n * n; i++) numbers.Add(i + 1);
  int sum = (1 + n * n) * n / 2;
  List<int> empty = new List<int>();
  MakeList(numbers, empty, sum, n);
  Count(numbers, list);
  return counter;
}

void MakeList(List<int> src, List<int> dst, int sum, int c)
{
  if (c == 1)
  {
    if (src.Contains(sum))
    {
      dst.Add(sum);
      list.Add(dst);
    }
    return;
  }
  for (int i = 0; i < src.Count; i++)
  {
    List<int> src2 = src.GetRange(i + 1, src.Count - i - 1);
    List<int> dst2 = dst.GetRange(0, dst.Count);
    dst2.Add(src[i]);
    MakeList(src2, dst2, sum - src[i], c - 1);
  }
  return;
}

void Count(List<int> s, List<List<int>> src)
{
  for (int i = 0; i < src.Count; i++)
  {
    List<int> s2 = s.GetRange(0, s.Count);
    src[i].ForEach(delegate(int n) { s2.Remove(n); });
    if (s2.Count == 0)
    {
      counter++;
    }
    else
    {
      List<List<int>> src2
        = src.GetRange(i + 1, src.Count - i - 1)
        .FindAll(delegate(List<int> l)
      {
        return !l.Exists(delegate(int n)
        {
          return src[i].Contains(n);
        });
      });
      Count(s2, src2);
    }
  }
}

Index

Feed

Other

Link

Pathtraq

loading...