/
Program.cs
84 lines (66 loc) · 1.77 KB
/
Program.cs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
public class Frog
{
public enum Way { step = 1, jump = 2 };
public static List<int> getOneStep(int n)
{
var path = new List<int>();
for (var count = 0; count < n; count++)
{
path.Add(1);
}
return path;
}
public static List<int> addJump(List<int> path, int n)
{
var position = path.LastIndexOf((int)Way.step);
if (position < 0)
{
return null;
}
path[position] = (int)Way.jump;
if (position > 0)
{
path.RemoveAt(position - 1);
}
return path.Sum() == n ? path : null;
}
public static void walking(List<int> path, ref int accum)
{
var first = path.IndexOf((int)Way.jump);
if (first > 0)
{
accum += 1;
path[first - 1] = (int)Way.jump;
path[first] = (int)Way.step;
walking(path.Skip(first).Take(path.Count - first).ToList<int>(), ref accum);
walking(new List<int>(path), ref accum);
}
}
public static int NumberOfWays(int n)
{
var path = getOneStep(n);
var accum = 1;
while (path != null)
{
path = addJump(path, n);
if (path != null)
{
accum++;
walking(new List<int>(path), ref accum);
}
}
return accum;
}
public static void Main(String[] args)
{
Console.WriteLine(NumberOfWays(3));
Console.WriteLine(NumberOfWays(1));
Console.WriteLine(NumberOfWays(2));
Console.WriteLine(NumberOfWays(5));
Console.WriteLine(NumberOfWays(11));
Console.Read();
}
}