输入一些二元组,每个表示两个节点之间有一条通路。再输入一个顶点,一个终点,求出一共有多少种走法(不包括回路)。用C#或者Java实现
7条回答 默认 最新
threenewbee 2015-01-04 05:12关注来个田字格的
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; } } } }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报