輸入一些二元組,每個表示兩個節點之間有一條通路。再輸入一個頂點,一個終點,求出一共有多少種走法(不包括回路)。用C#或者Java實現
來個田字格的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input1 = @"a,b
b,c
a,d
b,e
c,f
d,e
e,f
d,g
e,h
f,i
g,h
h,i";
foreach (var item in Solve(input1, "", "a", "i"))
Console.WriteLine(item);
}
static IEnumerable<string> Solve(string input, string prepath, string start, string end)
{
if (start == end)
{
yield return prepath + end;
yield break;
}
var query = input.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(',')[0] + x.Split(',')[1]).Where(x => x.Contains(start) && !prepath.Contains(x.Replace(start, "")));
foreach (var item in query)
{
foreach (var item1 in Solve(input, prepath + start, item.Replace(start, ""), end))
yield return item1;
}
}
}
}