Question: I was solving the following problem using Dijkstra's shortest path: https://open.kattis.com/problems/blockcrusher The code seems to work well for the test problems, but I get wrong

I was solving the following problem using Dijkstra's shortest path:

https://open.kattis.com/problems/blockcrusher

The code seems to work well for the test problems, but I get wrong solution when I submit the file. Any idea what I'm doing wrong here?

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class frac2 { private static String in_; private static String[] input; private static ArrayList nodeList; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(!((in_ = in.readLine()).charAt(0) == '0')){ input = in_.split(" "); int rows = Integer.parseInt(input[0]); int cols = Integer.parseInt(input[1]); /**  * Reading input and constructing a string  * O(n)  *  **/  String allnums = ""; for (int i = 0; i < rows; i++) { String adder = in.readLine(); allnums+= adder; } int V = allnums.length(); /**  * Iterating string and creating nodes  * O(n)  *  * */  nodeList = new ArrayList<>(V+1); for (int i = 0; i < V; i++) { int val = Character.getNumericValue(allnums.charAt(i)); gNode g = new gNode(); g.value = val; g.index=i; nodeList.add(g); } /**  * Assigning neighbors / edges  * O(n)  *  * */  int rowCount = 0; for (int i = 0; i < V; i++) { gNode n = nodeList.get(i); if(i % cols == 0) rowCount++; if(i%cols>0){ n.addNeighbor(nodeList.get(i-1)); } //Adding to the right if(inodeList.get(i+1)); } //Adding to the top if(i>=cols){ n.addNeighbor(nodeList.get(i-cols)); //Top left if(i % cols > 0){ n.addNeighbor(nodeList.get(i-cols-1)); } //Top right if(inodeList.get(i-cols+1)); } } //Adding to the bottom if(inodeList.get(i+cols)); //Bottom left if(i%cols>0){ n.addNeighbor(nodeList.get(i+cols-1)); } //Bottom left if(inodeList.get(i+cols+1)); } } } //Adding a top node to do Dijkstra's from, so we don't need to do the search n times. gNode top = new gNode(); top.value = 0; top.index=V; for (int i = 0; i < cols; i++) { top.neighbors.add(nodeList.get(i)); } nodeList.add(top); /**  * Doing the Dijkstra on upper row of nodes (NodeList)  * O(cols * (V+E)*logV)  *  * */  int mindist = 9999999; ArrayList list; list = Dijkstra(nodeList, cols, V); /**  * Print solution  * O(n)  *  * */  ArrayList indexes = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { indexes.add(list.get(i).index); } Collections.sort(indexes); System.out.println(""); for (int i = 0; i < V; i++) { if(indexes.contains(i)){ System.out.print(' '); indexes.remove(0); } else System.out.print(allnums.charAt(i)); if (i>1 && (i+1) % cols == 0) System.out.println(""); } } } /**  * Dijkstra's  * O((V+E)*log(V))  *  * */   public static ArrayList Dijkstra(ArrayList allNodes, int cols, int start){ int n = allNodes.size(); //PQ for Dijkstra's PriorityQueue pq = new PriorityQueue(n); //Array containing the path taken ArrayList prev = new ArrayList(); //Instantiating infinite distances and no previous nodes for (gNode gnode:allNodes) { gnode.dist = 9999999; gnode.prev = null; pq.add(gnode); } //Remove one to add start node again. pq.poll(); gNode startNode = allNodes.get(start); startNode.setDist(startNode.value); pq.add(startNode); while (!(pq.isEmpty())){ gNode u = (gNode) pq.poll(); //System.out.println(u.value); for (gNode k: u.neighbors) { if(k.dist > u.dist + k.value){ k.dist = u.dist + k.value; k.prev = u; pq.add(k); } } } //Finding best path to bottom int minDistindex = n-1; int minDist = 9999999; for (int i = n-cols-1; i < n-1; i++) { int dist = allNodes.get(i).dist; if(dist { int value; int dist; gNode prev; int index; public ArrayList neighbors; public gNode() { this.neighbors = new ArrayList<>(); this.prev = null; } public void setValue(int value) { this.value = value; } public void setDist(int d) { this.dist = d; } public void addNeighbor(gNode n) { this.neighbors.add(n); } @Override public int compareTo(gNode o) { if (this.dist > o.dist) { return 1; } if (this.dist == o.dist) { return 0; } else return -1; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!