Related
So I have been searching these two algorithms on the Internet and was able to, after days of working on it, get Dijkstra's Algorithm working. From what I have read the two Algorithms are not that different. So I am going to post what I did for Dijkstra's and I was hoping that you can show me or point me into the direction on how to modify it to the A* algorithm.
public Integer processGraph(int graph[][], int algorithm, int s, int d) {
if(algorithm == 1) {
// shortestDist[index] will hold the distance from s to the index
int shortestDist[] = new int[graph.length];
// added[i] is true if vertex index is included / the shortest distance from the source to the index is finalized
boolean added[] = new boolean[graph.length];
// establish all distances as Integer.Max_Value and added[] as false
for (int vertexIndex = 0; vertexIndex < graph.length; vertexIndex++) {
shortestDist[vertexIndex] = Integer.MAX_VALUE;
added[vertexIndex] = false;
}
// Distance of the source from itself is always 0
shortestDist[s] = 0;
// Parent array to store shortest the shortest path -- example: [num, num, num]
int[] parents = new int[graph.length];
// Find shortest path for all vertices
for (int i = 1; i < graph.length; i++) {
// Pick the minimum distance vertex
// from the set of vertices not yet
// processed. nearestVertex is
// always equal to the source in
// first iteration.
int u = minDistance(shortestDist, added);
added[u] = true;
for (int vertexIndex = 0; vertexIndex < graph.length; vertexIndex++) {
if (!added[vertexIndex] && graph[u][vertexIndex] != 0 && shortestDist[u] != Integer.MAX_VALUE && shortestDist[u] + graph[u][vertexIndex] < shortestDist[vertexIndex]) {
shortestDist[vertexIndex] = shortestDist[u] + graph[u][vertexIndex];
parents[vertexIndex] = u;
}
}
}
if(shortestDist[d] == Integer.MAX_VALUE) {
System.out.println(" ");
System.out.println("ERROR MESSAGE -- No path present from " + s + " to " + d);
System.out.println("ERROR MESSAGE --no path present from " + s + " to " + d);
System.out.println(" ");
return null;
}
//pathPrint(s, shortestDist, d); this only works for the first test
return shortestDist[d];
} // end algorithm 1
TO add more context to this, I have a driver class in which with several graphs upon which this finds the shortest path.
Example of graph[][]:
int regValid[][] = {{0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0,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 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0}};
Again, how can I modify what I have to work with the A* algorithm instead of Dijkstras. Thank you and hopefully this makes sense.
You have full information about all nodes, edges and destination node, so Dijkstra is the best solution for your problem. A* would be useful if you hadn't known complete graph (most often for huge node space) and were forced to prioritize decisions according to some heuristics.
If you really want to simulate A* on your graph, you have to think out some artificial criterion (heuristics) which describes the estimated distance between given node and target node (in other words, pretend you don't know the path). Since your data set doesn't contain such additional information (and thinking out any satisfying admissibility condition is not trivial), your question doesn't make much sense to me.
I have a following 2 dimensional array:
int[][] array = new int[][]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
};
and I would like to trim all the surrounding zeroes, so my output would be like this (removing "zeros" outside and preserving the zeroes that are surrounded by "ones"):
{0, 1, 1, 1, 0},
{0, 1, 1, 1, 1},
{1, 1, 0, 1, 1},
{1, 1, 0, 1, 0},
{0, 1, 1, 1, 1},
{0, 1, 1, 1, 1},
{0, 0, 0, 1, 0}
I'm looking for an efficient way of doing this.
Possible solution (dunno if it is the most efficient way):
public static int[][] trim(int[][] mtx, int rmin, int rmax, int cmin, int cmax) {
int[][] result = new int[rmax-rmin+1][];
for (int r = rmin, i = 0; r <= rmax; r++, i++) {
result[i] = Arrays.copyOfRange(mtx[r], cmin, cmax+1);
}
return result;
}
public static int[][] trim(int[][] mtx, int trimmed) {
int cmin = mtx[0].length;
int rmin = mtx.length;
int cmax = -1;
int rmax = -1;
for (int r = 0; r < mtx.length; r++)
for (int c = 0; c < mtx[0].length; c++)
if (mtx[r][c] != trimmed) {
if (cmin > c) cmin = c;
if (cmax < c) cmax = c;
if (rmin > r) rmin = r;
if (rmax < r) rmax = r;
}
return trim(mtx, rmin, rmax, cmin, cmax);
}
public static void main (String[] args) {
int[][] array = new int[][]{
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 1, 0, 0, 0},
{0, 0, 1, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0}
};
int[][] trim = trim(array, 0);
System.out.println(Arrays.deepToString(trim));
}
I am creating a map system for my game which requires me at a few stages to replace the old 2D array map with another. What I wanted to do is add all the 2D arraylist maps into a List of these 2D arraylists. However, what I found difficult was that when I wanted to retrieve said map, the .get method would not give me the correct data, as it you would try to pull the first item of that arraylist which in this case would be null. Thus the program threw me nullpointerexceptions.
private int[][] mapArray;
private List<int[][]> maps = new ArrayList<>();
mapArray = new int[][]{
{0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 9, 1, 1, 7, 7, 7, 7, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{7, 7, 7, 7, 5, 8, 8, 8, 4, 6, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{4, 4, 4, 4, 4, 8, 8, 8, 4, 4, 6, 7, 7, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{4, 4, 4, 4, 4, 8, 8, 8, 4, 4, 4, 4, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
maps.add(mapArray);
When I sout maps.get(0) I get the following:
[[I#58838f3f
If I have two items, it will show up like this:
[[[I#58838f3f, [[I#59a60155]
Ideally I want it to be added like this:
[I#58838f3f, I#58838f3f]
I am just unsure how to do this. Any help would be appreciated!
I might have overlooked something obvious, probably is the case!
Much obliged
You seem to be confusing the output of converting your map (or the list of maps) to string (which is what happens when you try to print them) - with what actual value there are there.
Because ArrayList like most Java container formats have nice toString() implementations that get automatically called when trying to print them, you'd get something that "looks like an array" in the output. So for example when printing an array list containing two strings, it might look like this: [a, b].
Arrays don't have such nice formatters, because they aren't really objects (they are "primitive types") and so print them gets you their address and some Java byte code that describes their type (if you can understand Java byte code), so an array of integers will look like this: [I#<address> while an two dimensional array of bytes will look like this: [[B#<address>, and so on.
In regards to your original question, the code seems to work well for me. Here is an example program:
import java.util.ArrayList;
public class Test2D {
public static void main(String... args) {
ArrayList<int[][]> maps = new ArrayList<>();
maps.add(new int[][] {
{1,2,3},
{4,5,6},
{7,8,9},
});
maps.add(new int[][] {
{10,20,30},
{40,50,60},
});
int[][] map = maps.get(0);
System.out.println(map[0][0]);
}
}
The output will print correctly 1 , showing that there is no null pointer exception.
I've been following a tutorial about creating a basic tile-based tower defense game in Java and have encountered a piece of code I cannot wrap my brain around and would like some help. (My main question is at the bottom after the code)
At this point we are iterating through a multidimensional array of 0's and 1's that we pass to a constructor which has a method that assigns a grass tile for 0's and stone tile for 1's and then another method to draw them to the screen creating our game screen. Simple enough, right?
Here is the class:
package data;
import static helpers.Artist.*;
public class TileGrid {
public Tile[][] map;
public TileGrid(int[][] newMap){
map = new Tile[20][15];
for(int i = 0; i < map.length; i++){
for(int j = 0; j <map[i].length; j++){
switch(newMap[j][i]){
case 0:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.GRASS);
break;
case 1:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.STONE);
break;
case 2:
map[i][j] = new Tile(row * 64, col * 64, 64, 64, TileType.WATER);
break;
}
}
}
}
public void Draw(){
for(int i = 0; i < map.length; i++){
for(int j = 0; j < map[i].length; j++){
Tile t = map[i][j];
DrawQuadTex(t.getTexture(), t.getX(), t.getY(), t.getWidth(), t.getHeight());
}
}
}
}
And here is the array we are passing in:
int[][] map = { //20 tiles wide, 15 tiles high
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0},
};
So my main question has to do with the switch statement in the constructor for the TileGrid.
Why do the i and the j get switched newMap[j][i] when checking what it equals? I get that this code works, well, because it does and I understand nested for loops to iterate through a multidimensional array.
But why wouldn't newMap[i][j] work?
As well at the very beginning of that same constructor why do we create an array (of type Tile) with the dimension of map = new Tile[20][15] when we are passing in an array with the dimensions of map[15][20]?
I have tried to figure this out and study this on my own and will continue to do so until I understand it but any help would be soooo appreciated!
You are passing to the TileGrid constructor a 2D array of 15 rows and 20 columns, but inside the constructor you create a 2D array of 20 rows and 15 columns. That's why map[i][j] corresponds with newMap[j][i].
If the input int[][] map was also of 20 rows and 15 columns, you wouldn't have to switch the order of the indices inside the constructor.
This would give an index out of bounds error if it was really switched. However, there is a col variable in the second for loop that is not defined anywhere so it's hard to tell what's going on. This code will not compile.
If it is some code pulled off a a tutorial on some web page, I would just assume that it was a typo and continue on with that in mind. OR better yet, contact the author.
I'm running into problems trying to assign a 2d array to a 3d array, so I thought i'd ask a question about 3d and 2d arrays.
Say I have a masterArray[][][] and wanted to put childArray1[][] and childArray2[][] into it.
This is how I have done it and was wondering if that is the correct way of applying it:
private int[][][] masterArray;
private int[][] childArray1 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 1, 0, 1, 8, 1, 0, 1},
{1, 0, 7, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 9, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
private int[][] childArray2 = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 7, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
{1, 1, 1, 9, 1, 1, 8, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};
Ok, so in my init method I use these some methods to set the child arrays into the master array. What I was curious about was how this exactly works. I assumed the following:
masterLevel = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
Would that work?
In my application things aren't working so I picking out code that I am not 100% sure on.
It doesn't really matter how you organize a 3d array as long as you put things in the same way as you take them out.
From your comment on another answer it seems that you are having problem with element order ([currentLevel][x][y] = childArray[y][x];)
It seems you mixed MAP_HEIGHT and MAP_WIDTH. It should be:
masterLevel = new int[MAX_LEVELS][MAP_HEIGHT][MAP_WIDTH];
then you can use:
master[currentLevel][x][y] = childArray[x][y];
In Java multi-d arrays are actually arrays of arrays. So they can even be disjoint.
In the code you posted you refer to a variable called currentLevel that you did not define. I am sure that is defined in some code you did not post. Also don't forget that arrays are zero index. This code should work.
masterArray = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int currentLevel = 0; currentLevel < MAX_LEVELS; currentLevel++) {
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
masterArray[currentLevel][x][y] = childArray1[x][y];
}
}
}
If you ever work with massive arrays and need speed then you could look at System.arrayCopy();
String[] arr1D;
String[][] arr2D;
String[][][] arr3D;
arr1D = new String[] { "1", "2", "3" };
//////////////////////////////////////////////////////////////////////////
//assign 1D array to element of 2D array
//////////////////////////////////////////////////////////////////////////
arr2D = new String[][] {
arr1D ,
arr1D ,
arr1D
};
/*
// OR
arr2D = new String[3][];
arr2D[0] = arr1D;
arr2D[1] = arr1D;
arr2D[2] = arr1D;
// OR
arr2D = new String[][] {
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" } ,
new String[] { "1", "2", "3" }
};
*/
//////////////////////////////////////////////////////////////////////////
//assign 2D array to element of 3D array
//////////////////////////////////////////////////////////////////////////
arr3D = new String[][][] {
arr2D ,
arr2D ,
arr2D
};
/*
// OR
arr3D = new String[3][][];
arr3D[0] = arr2D;
arr3D[1] = arr2D;
arr3D[2] = arr2D;
*/