public static List <GraphNode> CreateMazePath() { GraphNode cell1 = new GraphNode(1); GraphNode cell2 = new GraphNode(2); GraphNode cell3 = new GraphNode(3); GraphNode cell4 = new GraphNode(4); GraphNode cell5 = new GraphNode(5); GraphNode cell6 = new GraphNode(6); GraphNode cell7 = new GraphNode(7); GraphNode cell8 = new GraphNode(8); GraphNode cell9 = new GraphNode(9); cell1.AddEdge(2); cell2.AddEdge(3); cell2.AddEdge(1); cell3.AddEdge(8); cell3.AddEdge(4); cell8.AddEdge(3); cell4.AddEdge(3); cell4.AddEdge(7); cell4.AddEdge(5); cell5.AddEdge(4); cell5.AddEdge(6); cell6.AddEdge(5); cell6.AddEdge(7); cell6.AddEdge(9); cell9.AddEdge(6); cell9.SetEnd(true); return(new List <GraphNode> { cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9 }); }
private List <int> Solution1() { Graph g = new Graph(); GraphNode n1 = new GraphNode(1); GraphNode n2 = new GraphNode(2); GraphNode n3 = new GraphNode(3); GraphNode n4 = new GraphNode(4); GraphNode n5 = new GraphNode(5); GraphNode n6 = new GraphNode(6); GraphNode n7 = new GraphNode(7); GraphNode n8 = new GraphNode(8); n1.AddEdge(n2); n1.AddEdge(n3); n1.AddEdge(n8); n2.AddEdge(n7); n3.AddEdge(n4); n3.AddEdge(n5); n7.AddEdge(n6); n7.AddEdge(n8); n4.AddEdge(n5); g.GraphNodes.Add(n2); g.GraphNodes.Add(n1); g.GraphNodes.Add(n3); g.GraphNodes.Add(n4); g.GraphNodes.Add(n5); g.GraphNodes.Add(n6); g.GraphNodes.Add(n7); g.GraphNodes.Add(n8); g.Run(); return(g.Results); }
private Graph2D CreateFullDigraph(FPseudoRandom random) { Graph2D graph = new Graph2D(this.row, this.col); for (int i = 1; i < this.row - 1; i++) { for (int j = 1; j < this.col - 1; j++) { int cur = i * this.col + j; GraphNode node = graph[cur]; if (j < this.col - 2) { node.AddEdge(cur, cur + 1, random.NextFloat()); } if (j > 1) { node.AddEdge(cur, cur - 1, random.NextFloat()); } } } for (int i = 1; i < this.col - 1; i++) { for (int j = 1; j < this.row - 1; j++) { int cur = j * this.col + i; GraphNode node = graph[cur]; if (j < this.row - 2) { node.AddEdge(cur, cur + this.col, random.NextFloat()); } if (j > 1) { node.AddEdge(cur, cur - this.col, random.NextFloat()); } } } return(graph); }
private static Graph BuildGraph(int[][] roads, int[] machines) { Graph g = new Graph(); HashSet <int> machineSet = new HashSet <int>(machines); foreach (int[] road in roads) { GraphNode source = g.AddNode(road[0], machineSet.Contains(road[0])); GraphNode target = g.AddNode(road[1], machineSet.Contains(road[1])); int weight = road[2]; source.AddEdge(target.id, weight); target.AddEdge(source.id, weight); } return(g); }
public void AddNode(Vector3 neighborPoint, ref GraphNode currentNode, ref Queue<GraphNode> q ) { RaycastHit hitInfo; Vector3 rayDirection = Vector3.zero; #if USE_XZ rayDirection = new Vector3(0, -1, 0); #else rayDirection = new Vector3(0, 0, 1); #endif //USE_XZ int layerMask = 1 << 8; layerMask = ~layerMask; if ( Physics.Raycast(neighborPoint, rayDirection, out hitInfo, Mathf.Infinity, layerMask) ) { if (hitInfo.transform.tag == "Ground") { GraphNode newNode = new GraphNode(mNumOfNodes, hitInfo.point); // make a new node for this point GraphEdge newEdge = new GraphEdge(currentNode.GetIndex(), newNode.GetIndex()); // creat a new edge int index = 0; bool nodeFound = false; while ( !nodeFound && index <= mNumOfNodes ) { //Debug.Log (index + " out of " + NavigationGraph.Length + " thinks there's only" + mNumOfNodes); nodeFound = ( NavigationGraph[index] == hitInfo.point ); ++index; } if ( !nodeFound ) // if we have not found this node before, add it { Nodes.Add(newNode); NavigationGraph[mNumOfNodes] = hitInfo.point; ++mNumOfNodes; q.Enqueue(newNode); } else { newEdge.SetToIndex(index-1); } // If the raycast hit then we will always want to add the edge, since we want edges // in both directions and there won't ever be duplicates. // check if there is a clear path to add an edge Vector3 heightOffset = Vector3.zero; #if USE_XZ heightOffset = new Vector3(0, 0.5f, 0); #else heightOffset = new Vector3(0, 0, -0.5f); #endif // USE_XZ if ( !Physics.Linecast(currentNode.GetPosition() + heightOffset, newNode.GetPosition() + heightOffset, out hitInfo, layerMask) ) { Edges.Add(newEdge); currentNode.AddEdge(newEdge); } } } }