BFS搜索2D阵列中的每个项目并查找最短路径

时间:2014-08-22 13:21:30

标签: java algorithm

我想访问所有char @ living并找到从s到t的最短路径。我正在使用BFS算法,但我的代码只访问第一行。 o / p我的代码

3 3

ABH

ISH

心电图

访问:ABH 请帮助我如何访问每个顶点;

class Queue {

    private final int SIZE = 100;
    private int[] queArray;
    private int front;
    private int rear;

    // Queue constructor
    public Queue() {
        queArray = new int[SIZE];
        front = 0;
        rear = -1;
    }

    // Insert into the Queue
    public void insert(int item) { // put item at rear of the queue
        if (rear == SIZE - 1)
            rear = -1;
        queArray[++rear] = item;
    }

    public int remove() {
        int temp = queArray[front++];
        if (front == SIZE)
            front = 0;
        return temp;
    }

    public boolean isEmpty() {
        return (rear + 1 == front || (front + SIZE - 1 == rear));
    }
}

class Vertex {

    public char label;
    public boolean wasVisited = false;

    public Vertex(char label) {
        this.label = label;
        wasVisited = false;
    }
}// end of class Vertex

class Graph {

    private final int MAX_VERTS = 100;
    private Vertex vertexList[]; // list of the vertices
    private char adjMat[][];
    private int nVerts;
    private Queue theQueue;

    public Graph() {
        vertexList = new Vertex[MAX_VERTS];
        adjMat = new char[MAX_VERTS][MAX_VERTS];
        nVerts = 0;
        for (int j = 0; j < MAX_VERTS; j++) {
            for (int k = 0; k < MAX_VERTS; k++) {
                adjMat[j][k] = 0;
                theQueue = new Queue();
            }
        }
    }

    public void addVertex(char lab) {
        vertexList[nVerts++] = new Vertex(lab);
    }

    public void addEdge(int start, int end) {
        adjMat[start][end] = 1;
        adjMat[start][end] = 1;
    }

    public void displayVertex(int v) {
        System.out.print(vertexList[v].label);
    }

    // breadth first search
    public void bfs() {
        vertexList[0].wasVisited = true;
        displayVertex(0);
        theQueue.insert(0);
        int v2;
        while (!theQueue.isEmpty()) {
            int v1 = theQueue.remove();
            while ((v2 = getAdjUnvisitedVertex(v1)) != -1) {
                vertexList[v2].wasVisited = true;
                displayVertex(v2);
                theQueue.insert(v2);
            }
        }
        for (int j = 0; j < nVerts; j++) {
            vertexList[j].wasVisited = false;
        }
    }

    public int getAdjUnvisitedVertex(int v) {
        for (int j = 0; j < nVerts; j++)
            if (adjMat[v][j] == 1 && vertexList[j].wasVisited == false)
                return j;
        return -1;
    }

}

class Orienteering {

    public static void main(String[] args) throws IOException {
        System.out.println("Enter W and H");
        String input = getString();
        String wh[] = input.split(" ");
        int w = Integer.parseInt(wh[0]);
        int h = Integer.parseInt(wh[1]);
        char[][] ch = new char[w][h];
        int j;
        Graph theGraph = new Graph();
        for (int i = 0; i < w; i++) {
            String rows = getString();
            for (j = 0; j < h; j++) {
                ch[i][j] = rows.charAt(j);
                theGraph.addVertex(ch[i][j]);
                theGraph.addEdge(i, j);
            }
        }
        System.out.print("Visits:");
        theGraph.bfs();
        System.out.println("");

    }

    public static String getString() throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String strLine = br.readLine();
        return strLine;
    }
}

0 个答案:

没有答案