(UVA) Number Maze - Solution
In order to solve this problem, I used Dijkstra's algorithm. Inside the
Dijkstra method, I used a PriorityQueue to order the edges, which are
weighted. However, once the PriorityQueue contains the data type
Element, which has three variables, I had to implement a Comparator to
order the queue according to the weight.
import java.io.*;
import java.util.*;
class Main {
public static int[][] maze;
public static int[][] addedToQueue;
public static int[] vi = {-1,1,0,0};
public static int[] vj = {0,0,-1,1};
public static Comparator<Element> priceComparator = new Comparator<Element>(){
public int compare(Element e1, Element e2) {
return (int) (e1.price - e2.price);
}
};
public static int dijkstra(int rowStart, int colStart, int rowEnd, int colEnd, int numRows, int numCols) {
Queue<Element> queue = new PriorityQueue<Element>(numRows*numCols, priceComparator);
Element e = new Element(rowStart, colStart, maze[rowStart][colStart]);
queue.add(e);
addedToQueue[rowStart][colStart] = 1;
while (queue.size() > 0) {
Element currPos = queue.poll();
int currPosRow = currPos.row;
int currPosCol = currPos.col;
int currPrice = currPos.price;
if (currPosRow == rowEnd && currPosCol == colEnd) {
return currPrice;
}
for (int i = 0; i < 4; i++) {
int nextI = currPosRow+vi[i];
int nextJ = currPosCol+vj[i];
if (nextI >= 0 && nextJ >= 0 && nextI < numRows && nextJ < numCols && addedToQueue[nextI][nextJ] == 0) {
e = new Element(nextI, nextJ, currPrice+maze[nextI][nextJ]);
queue.add(e);
addedToQueue[nextI][nextJ] = 1;
}
}
}
return 0;
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int numMazes = sc.nextInt();
for (int i = 0; i < numMazes; i++) {
int numRows = sc.nextInt();
int numCols = sc.nextInt();
maze = new int[numRows][numCols];
addedToQueue = new int[numRows][numCols];
for (int j = 0; j < numRows; j++) {
for (int k = 0; k < numCols; k++) {
maze[j][k] = sc.nextInt();
}
}
int rowStart = 0;
int colStart = 0;
int rowEnd = numRows-1;
int colEnd = numCols-1;
System.out.println(dijkstra(rowStart, colStart, rowEnd, colEnd, numRows, numCols));
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Element {
int row;
int col;
int price;
public Element(int r, int c, int p) {
row = r;
col = c;
price = p;
}
}
import java.util.*;
class Main {
public static int[][] maze;
public static int[][] addedToQueue;
public static int[] vi = {-1,1,0,0};
public static int[] vj = {0,0,-1,1};
public static Comparator<Element> priceComparator = new Comparator<Element>(){
public int compare(Element e1, Element e2) {
return (int) (e1.price - e2.price);
}
};
public static int dijkstra(int rowStart, int colStart, int rowEnd, int colEnd, int numRows, int numCols) {
Queue<Element> queue = new PriorityQueue<Element>(numRows*numCols, priceComparator);
Element e = new Element(rowStart, colStart, maze[rowStart][colStart]);
queue.add(e);
addedToQueue[rowStart][colStart] = 1;
while (queue.size() > 0) {
Element currPos = queue.poll();
int currPosRow = currPos.row;
int currPosCol = currPos.col;
int currPrice = currPos.price;
if (currPosRow == rowEnd && currPosCol == colEnd) {
return currPrice;
}
for (int i = 0; i < 4; i++) {
int nextI = currPosRow+vi[i];
int nextJ = currPosCol+vj[i];
if (nextI >= 0 && nextJ >= 0 && nextI < numRows && nextJ < numCols && addedToQueue[nextI][nextJ] == 0) {
e = new Element(nextI, nextJ, currPrice+maze[nextI][nextJ]);
queue.add(e);
addedToQueue[nextI][nextJ] = 1;
}
}
}
return 0;
}
public static void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
int numMazes = sc.nextInt();
for (int i = 0; i < numMazes; i++) {
int numRows = sc.nextInt();
int numCols = sc.nextInt();
maze = new int[numRows][numCols];
addedToQueue = new int[numRows][numCols];
for (int j = 0; j < numRows; j++) {
for (int k = 0; k < numCols; k++) {
maze[j][k] = sc.nextInt();
}
}
int rowStart = 0;
int colStart = 0;
int rowEnd = numRows-1;
int colEnd = numCols-1;
System.out.println(dijkstra(rowStart, colStart, rowEnd, colEnd, numRows, numCols));
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Element {
int row;
int col;
int price;
public Element(int r, int c, int p) {
row = r;
col = c;
price = p;
}
}
Comments
Post a Comment