BFS映射在C#中
如何在 C# 中的未加权、无向图中使用 BFS,使用 adj 列表实现对地图进行染色,使用四种颜色,以便相邻的 2 个国家具有不同的颜色。
这就是我实现图表的方式,我将添加一个数组,将四种颜色表示为字符串,另一个数组表示国家/地区,例如每个顶点代表一个国家/地区。
internal class Graph
{
private int vertices;
private LinkedList<int>[] adj;
public Graph(int v)
{
vertices = v;
adj = new LinkedList<int>[v];
for(int i = 0; i < v; i++)
{
adj[i] = new LinkedList<int>();
}
}
public void addEdge(int c, int v)
{
adj[c].AddLast(v);
}
public void BFS(int s)
{
bool[] visited = new bool[vertices];
for(int i=0; i<vertices; i++)
{
visited[i] = false;
}
LinkedList<int> queue = new LinkedList<int>();
visited[s] = true;
queue.AddLast(s);
while (queue.Any())
{
s = queue.First();
Console.WriteLine(s + " ");
LinkedList<int> list = adj[s];
foreach(var val in list)
{
if (!visited[val])
{
visited[val] = true;
queue.AddLast(val);
}
}
}
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TrifIonut_MapDrawing
{
internal class Graph
{
private int vertices;
private LinkedList<int>[] adj;
private string[] color;
public Graph(int v)
{
vertices = v;
adj = new LinkedList<int>[v];
color = new string[v];
for (int i = 0; i < v; i++)
{
adj[i] = new LinkedList<int>();
color[i] = "";
}
color[0] = "blue";
}
public void addEdge(int c, int v)
{
adj[c].AddLast(v);
}
public void BFS(int s)
{
bool[] visited = new bool[vertices];
for (int i = 0; i < vertices; i++)
{
visited[i] = false;
}
LinkedList<int> queue = new LinkedList<int>();
visited[s] = true;
queue.AddLast(s);
while (queue.Any())
{
s = queue.First();
queue.RemoveFirst();
LinkedList<int> list = adj[s];
void visitor()
{
List<string> possibleColors = new List<string>{"blue", "green", "yellow", "red" };
foreach (var val in list)
{
if (visited[val])
{
for(int i=0; i<possibleColors.Count; i++)
{
if (color[val] == possibleColors[i])
{
possibleColors.RemoveAt(i);
}
}
}
}
color[s] = possibleColors[0];
}
visitor();
foreach (var val in list)
{
if (!visited[val])
{
visited[val] = true;
queue.AddLast(val);
}
}
}
}
}
}
我真的不知道如何修改 BFS 函数来帮助我解决问题。
How do I use BFS in an unweighted, undirected graph in C#, implemented using adj list to dye a map, using four colors so that 2 countries that are neighbours to have different colors.
This is how I implemented the graphs, I will add, an array that represents the four colors as strings, and another one for countries, such as every vertex to represent a country.
internal class Graph
{
private int vertices;
private LinkedList<int>[] adj;
public Graph(int v)
{
vertices = v;
adj = new LinkedList<int>[v];
for(int i = 0; i < v; i++)
{
adj[i] = new LinkedList<int>();
}
}
public void addEdge(int c, int v)
{
adj[c].AddLast(v);
}
public void BFS(int s)
{
bool[] visited = new bool[vertices];
for(int i=0; i<vertices; i++)
{
visited[i] = false;
}
LinkedList<int> queue = new LinkedList<int>();
visited[s] = true;
queue.AddLast(s);
while (queue.Any())
{
s = queue.First();
Console.WriteLine(s + " ");
LinkedList<int> list = adj[s];
foreach(var val in list)
{
if (!visited[val])
{
visited[val] = true;
queue.AddLast(val);
}
}
}
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TrifIonut_MapDrawing
{
internal class Graph
{
private int vertices;
private LinkedList<int>[] adj;
private string[] color;
public Graph(int v)
{
vertices = v;
adj = new LinkedList<int>[v];
color = new string[v];
for (int i = 0; i < v; i++)
{
adj[i] = new LinkedList<int>();
color[i] = "";
}
color[0] = "blue";
}
public void addEdge(int c, int v)
{
adj[c].AddLast(v);
}
public void BFS(int s)
{
bool[] visited = new bool[vertices];
for (int i = 0; i < vertices; i++)
{
visited[i] = false;
}
LinkedList<int> queue = new LinkedList<int>();
visited[s] = true;
queue.AddLast(s);
while (queue.Any())
{
s = queue.First();
queue.RemoveFirst();
LinkedList<int> list = adj[s];
void visitor()
{
List<string> possibleColors = new List<string>{"blue", "green", "yellow", "red" };
foreach (var val in list)
{
if (visited[val])
{
for(int i=0; i<possibleColors.Count; i++)
{
if (color[val] == possibleColors[i])
{
possibleColors.RemoveAt(i);
}
}
}
}
color[s] = possibleColors[0];
}
visitor();
foreach (var val in list)
{
if (!visited[val])
{
visited[val] = true;
queue.AddLast(val);
}
}
}
}
}
}
I don't really know how to modify the BFS function to help me in my matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加一个“访问者”,这是一个小函数,每次搜索到达新节点时都会调用该函数。将新访问节点的索引传递给访问者。在访问者中查看新节点的所有相邻节点。如果相邻节点已被访问过,则从可能的颜色中删除相邻节点的颜色。使用访问过的相邻节点未使用的颜色为新节点着色。
You can add a "visitor", which is a small function that is called each time a new node is reached in the search. Pass into the visitor the index of the newly visited node. In the visitor look at all the adjacent nodes of the new one. If adjacent node has been visited, remove adjacent node's color from possible colors. Color the new node with a color not being used by the visited adjacent nodes.