Поиск в ширину в графах - C#
Формулировка задачи:
В задаче тимуса 1837 попыталась сделать словарь, где ключ - строка (фамилия), а значения - лист из участников команды, с которыми когда-то владелец фамилии играл.
Для решения надо сделать поиск в ширину, чего, к сожалению, я пока не могу добиться.
Надеюсь на Вашу помощь в реализации на c#
Решение задачи: «Поиск в ширину в графах»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1837
{
class Program
{
public static Dictionary<string,int> GetNumber(Dictionary<string, List<string>> graph)
{
var result = new Dictionary<string, int>();
//поиск в ширину
return result;
}
static void Main(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>();
graph = GetGraph(n);
}
public static Dictionary<string, List<string>> GetGraph(int n)
{
Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>();
for (int i = 0; i < n; i++)
{
string str = Console.ReadLine();
string[] names = str.Split(' ');
foreach (var name in names)
{
if (!graph.ContainsKey(name))
graph.Add(name, GetListForNewOne(names, name));
else
//значение ключа = GetList(names, name, то же самое );
}
}
return graph;
}
public static List<string> GetListForNewOne(string[] names, string name)
{
List<string> list = new List<string>();
foreach (var n in names)
if (n != name)
list.Add(n);
return list;
}
public static List<string> GetList(string[] names, string name, List<string> list)
{
foreach (var n in names)
if (n != name && (!list.Contains(n)))
list.Add(n);
return list;
}
}
}