用Java表示图形
我正在开发一个迷宫系统,但不是图形迷宫系统。我认为迷宫实际上被称为无向图,因为它们彼此不直接相连。迷宫从文本文件中看起来如下所示:用Java表示图形,java,graph,graph-algorithm,maze,adjacency-list,Java,Graph,Graph Algorithm,Maze,Adjacency List,我正在开发一个迷宫系统,但不是图形迷宫系统。我认为迷宫实际上被称为无向图,因为它们彼此不直接相连。迷宫从文本文件中看起来如下所示: 11 3 2 3 0 3 1 4 5 4 5 7 6 7 7 8 8 9 9 10 0 5 我不知道用图表来表示这一点是否正确。如果你检查我的代码,它似乎是正确的,不是吗 import java.io.*; import java.util.Scanner; class Arc { public int nodeNo; public Arc ne
11 3
2 3
0 3
1 4
5 4
5 7
6 7
7 8
8 9
9 10
0 5
我不知道用图表来表示这一点是否正确。如果你检查我的代码,它似乎是正确的,不是吗
import java.io.*;
import java.util.Scanner;
class Arc {
public int nodeNo;
public Arc next;
public Arc(int nodeNo, Arc next) {
this.nodeNo = nodeNo;
this.next = next;
}
public String toString(){
return "" + nodeNo;
}
}
class Node {
int index;
Arc adjList;
Node(int index, Arc adjList) {
this.index = index;
this.adjList = adjList;
}
public String toString() {
return "" + index;
}
}
public class Maze {
Node[] stack;
private Scanner scan;
private static Scanner scan2;
public Maze(String mazeFile) throws FileNotFoundException {
scan = new Scanner(new File(mazeFile));
stack = new Node[12];
for (int n = 0; n < stack.length; n++) {
stack[n] = new Node(n, null);
}
while (scan.hasNext()) {
int v1 = indexForNode(scan.next());
int v2 = indexForNode(scan.next());
stack[v1].adjList = new Arc(v2, stack[v1].adjList);
stack[v2].adjList = new Arc(v1, stack[v2].adjList);
}
}
int indexForNode(String index) {
for (int n = 0; n < stack.length; n++) {
if (stack[n].index == Integer.parseInt(index)) {
return n;
}
}
return -1;
}
public void print(){
System.out.println();
for(int n = 0; n < stack.length; n++){
System.out.print(stack[n]);
for(Arc arc = stack[n].adjList; arc != null; arc = arc.next){
System.out.print(" --> " + stack[arc.nodeNo].index);
}
System.out.println("\n");
}
}
public static void main(String[] args) throws FileNotFoundException {
scan2 = new Scanner(System.in);
System.out.print("Enter maze file name: ");
String file = scan2.nextLine();
Maze maze = new Maze(file);
maze.print();
}
}
import java.io.*;
导入java.util.Scanner;
类弧{
诺德诺公共图书馆;
公共Arc下一步;
公共Arc(int nodeNo,Arc next){
this.nodeNo=nodeNo;
this.next=next;
}
公共字符串toString(){
返回“”+nodeNo;
}
}
类节点{
整数指数;
弧形调整列表;
节点(整数索引,弧调整列表){
这个指数=指数;
this.adjList=adjList;
}
公共字符串toString(){
返回“”+索引;
}
}
公共类迷宫{
节点[]堆栈;
私人扫描仪扫描;
专用静态扫描仪scan2;
公共迷宫(字符串迷宫文件)引发FileNotFoundException{
扫描=新扫描仪(新文件(mazeFile));
堆栈=新节点[12];
对于(int n=0;n”+stack[arc.nodeNo].index);
}
System.out.println(“\n”);
}
}
公共静态void main(字符串[]args)引发FileNotFoundException{
scan2=新扫描仪(System.in);
System.out.print(“输入迷宫文件名:”);
String file=scan2.nextLine();
迷宫=新迷宫(文件);
maze.print();
}
}
一般来说,我会说不:这不是一个好方法。一个基本图,其中边没有权重,被实现为一系列具有多个节点的节点↔ 许多
关系
因此,基本上您的节点如下所示:
public class Node {
private List<Node> out;
}
公共类节点{
私人名单;
}
有关更多信息,请参见。一般来说,我会说不:这不是一个好方法。基本图(其中边没有权重)被实现为一系列带有
多个节点的节点↔ 许多
关系
因此,基本上您的节点如下所示:
public class Node {
private List<Node> out;
}
公共类节点{
私人名单;
}
有关详细信息,请参见。因此它没有节点的任何其他属性,这就是您的意思吗?我可以用数组代替列表吗?如果没有,为什么?当然可以向节点添加属性。您也可以使用数组,但是没有一个认真的程序员会在这里使用数组。使用列表意味着列表为您保持了大小,并使其易于迭代。哦,我明白了。。。我现在明白了。谢谢我学到了一些新东西。您已经可以看出我的编程逻辑不好,对此深表歉意。:)所以它没有节点的任何其他属性,这就是你的意思吗?我可以用数组代替列表吗?如果没有,为什么?当然可以向节点添加属性。您也可以使用数组,但是没有一个认真的程序员会在这里使用数组。使用列表意味着列表为您保持了大小,并使其易于迭代。哦,我明白了。。。我现在明白了。谢谢我学到了一些新东西。您已经可以看出我的编程逻辑不好,对此深表歉意。:)