αβγδεζηθικ 2019-03-25 20:09 采纳率: 0%
浏览 307
已结题

请求各位大神帮忙 java语言 一道迪杰斯特拉找最短路径的题目求问代码bug

oj上的一道在二维坐标图中的迪杰斯特拉找最短路径的题,我用许多种测试数据跑过我的代码,但是评测机说有很多组数据runtime error,实在是找不到问题所在了,所以请求各位大神帮个忙,谢谢了

题目如下

图片说明

图片说明

2S时限 , 256空间限制 , N和M都不大于2000

import java.io.*;
import java.util.*;

class InputReader {
    public BufferedReader br;
    public StringTokenizer tokenizer;
    public InputReader(InputStream stream) throws FileNotFoundException {
        br = new BufferedReader(new InputStreamReader(stream), 327680);
        tokenizer = null;
    }
    public boolean hasNext(){
        while(tokenizer == null || !tokenizer.hasMoreElements())
        {
            try
            {
                tokenizer = new StringTokenizer(br.readLine());
            }
            catch(Exception e)
            {
                return false;
            }
        }
        return true;
    }

    public String next() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        try {
            int c = br.read();
            while (c <= 32) {
                c = br.read();
            }
            boolean negative = false;
            if (c == '-') {
                negative = true;
                c = br.read();
            }
            int x = 0;
            while (c > 32) {
                x = x * 10 + c - '0';
                c = br.read();
            }
            return negative ? -x : x;
        }catch(IOException e){
            return  -1;
        }
    }
    public long nextLong() {
        try {
            int c = br.read();
            while (c <= 32) {
                c = br.read();
            }
            boolean negative = false;
            if (c == '-') {
                negative = true;
                c = br.read();
            }
            long x = 0;
            while (c > 32) {
                x = x * 10 + c - '0';
                c = br.read();
            }
            return negative ? -x : x;
        }catch(IOException e){
            return  -1;
        }
    }

}//快速读入和输出

class Node{
    int row;
    int col;
    int w;

    public Node(int row , int col) {
        this.row = row;
        this.col = col;
    }
}//节点类
class nodeHeap{
    Node []Heap;
    int size;
    int tmpSize;
    public nodeHeap(int max) {
        size = max;
        Heap = new Node [size];
        tmpSize = 0;
        Heap[0] = new Node(0 , 0);
        Heap[0].w = -1;
    }

    public void swap(int a , int b) {
      Node tmp;
      tmp = Heap[a];
      Heap[a] = Heap[b];
      Heap[b] = tmp;
    }

    public void up(int n) {
        while(n / 2 >= 1) {
            if(Heap[n].w < Heap[n / 2].w) {
                swap(n , n / 2);
                n = n / 2;
            }
            else break;
        }
    }

    public void push(Node i) {
        tmpSize ++;
        Heap[tmpSize] = i;
        int tmp = tmpSize;
        up(tmp);
    }

    public Node peek() {
        return Heap[1];
    }

    public void down(int i) {
        while(i * 2 <= tmpSize || i > tmpSize) {
            int I = 2 * i;
            if(I < tmpSize && Heap[I].w > Heap[I + 1].w)
                I ++;
            if(Heap[I].w > Heap[i].w){
                return;
            }
            else {
                swap(i , I);
                i = I;
            }
        }
    }

    public Node pop() {
        swap(1 , tmpSize);
        tmpSize --;
        if(tmpSize > 0) {
          down(1);
        }
        return Heap[tmpSize + 1];
    }
}//自己写的堆

public class Main {
    static PrintWriter out;
    static InputReader in;
    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub
        out = new PrintWriter(System.out);
        in = new InputReader(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[][] map = new int[n][m];//用于储存数据
        Node root = null;//根节点
        nodeHeap heap = new nodeHeap(200000);//初始化堆
        boolean[][] visited = new boolean[n][m];//用于判断某个点是否遍历到过

        for(int i = 0 ; i < n ; i ++) {
            String A = in.next();
            for(int j = 0 ; j < m ; j ++) {
                char c = A.charAt(j);
                if(c == 'H') {
                    map[i][j] = 1;
                    visited[i][j] = true;
                    root = new Node(i , j);
                    heap.push(root);
                }
                else if(c == 'B')
                    map[i][j] = 2;
                else if(c == 'R')
                    map[i][j] = 3;
                else if(c == 'W')
                    map[i][j] = 4;
                else if(c == 'S')
                    map[i][j] = 5;
            }
        }//储存数据

        while(heap.tmpSize > 0) {
            Node tmp = heap.pop();
            if(map[tmp.row][tmp.col] == 5) {
                out.println(tmp.w);
                break;
            }//若当前节点为S 则结束循环并输出距离
            if(tmp.row - 1 >= 0)
            if(visited[tmp.row - 1][tmp.col] == false && map[tmp.row - 1][tmp.col] != 4) {
                visited[tmp.row - 1][tmp.col] = true;
                Node node = new Node(tmp.row - 1 , tmp.col);
                if(map[tmp.row - 1][tmp.col] == 1 || map[tmp.row - 1][tmp.col] == 3 || map[tmp.row - 1][tmp.col] == 5)
                    node.w = tmp.w + 1;
                else if(map[tmp.row - 1][tmp.col] == 2)
                    node.w = tmp.w + 2;
                heap.push(node);
            }
            if(tmp.row + 1 <= n - 1)
            if(visited[tmp.row + 1][tmp.col] == false && map[tmp.row + 1][tmp.col] != 4) {
                visited[tmp.row + 1][tmp.col] = true;
                Node node = new Node(tmp.row + 1 , tmp.col);
                if(map[tmp.row + 1][tmp.col] == 1 || map[tmp.row + 1][tmp.col] == 3 || map[tmp.row + 1][tmp.col] == 5)
                    node.w = tmp.w + 1;
                else if(map[tmp.row + 1][tmp.col] == 2)
                    node.w = tmp.w + 2;
                heap.push(node);
            }
            if(tmp.col - 1 >= 0)
            if(visited[tmp.row][tmp.col - 1] == false && map[tmp.row][tmp.col - 1] != 4) {
                visited[tmp.row][tmp.col - 1] = true;
                Node node = new Node(tmp.row , tmp.col - 1);
                if(map[tmp.row][tmp.col - 1] == 1 || map[tmp.row][tmp.col - 1] == 3 || map[tmp.row][tmp.col - 1] == 5)
                    node.w = tmp.w + 1;
                else if(map[tmp.row][tmp.col - 1] == 2)
                    node.w = tmp.w + 2;
                heap.push(node);
            }
            if(tmp.col + 1 <= m - 1)
            if(visited[tmp.row][tmp.col + 1] == false && map[tmp.row][tmp.col + 1] != 4) {
                visited[tmp.row][tmp.col + 1] = true;
                Node node = new Node(tmp.row , tmp.col + 1);
                if(map[tmp.row][tmp.col + 1] == 1 || map[tmp.row][tmp.col + 1] == 3 || map[tmp.row][tmp.col + 1] == 5)
                    node.w = tmp.w + 1;
                else if(map[tmp.row][tmp.col + 1] == 2)
                    node.w = tmp.w + 2;
                heap.push(node);
            }
        }//周围四个节点生成并更新距离

       out.close();
    }

}


  • 写回答

1条回答

  • dabocaiqq 2019-03-25 23:55
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频