I am having the following matrix stored in 2d int array result[][]:
0 1 1 0 0 0
1 0 0 1 0 0
1 0 0 1 1 0
0 1 1 0 0 1
0 0 1 0 0 1
0 0 0 1 1 0
I am trying to compute if there are any nodes that are connected with distance of 2 (there is 1 node in between them and no direct connection)
So far I have the following code:
Size is the size of the matrix (n * n) and dis is the distance I am looking for.
for(int row = 0; row < size; row++){
for(int column = 0; column < size; column++){
if(dis == 2){
if((result[row][column] == dis-1 && result[column][column+1] == 1 && result[row][column+1] == 0)){
if(row != column+1){
result[row][column+dis-1] = dis;
result[column+dis-1][row] = dis;
}
}
}
}
}
However, the code does not always work and is not universal if I try to change the distance to 3 or 4 for example.
Related
I am trying for hours to print DFS trace (including when you get stuck and you have to backtrack) but my output is always missing a 0 and having double numbers
My input matrix is like this
0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 1 0 1 0
1 0 1 0 0 1 0 0
0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 1 0 0
0 0 0 0 0 1 1 0
My output is like this
0 0 1 0 2 2 4 4 5 2 6 3 7
But it should be like this
0 1 0 2 4 5 4 2 6 2 0 3 7
My algoritem is this one
public void DFSDriver(int[][] adjMatrix)
{
int[] visitMatrix = new int[adjMatrix[0].length];
DftRecursive(adjMatrix, visitMatrix, 0); //Visit all nodes you can
//Visit the ones you cannot because they are separate
for(int i = 0; i < adjMatrix.length; i++) {
if(visitMatrix[i] != 1) {
DftRecursive(adjMatrix, visitMatrix, i); //Visit the ones you cannot because they are separate
}
}
}
public void DftRecursive(int[][] srcMatrix, int[] visitMatrix, int vertex)
{
visitMatrix[vertex] = 1;
System.out.print(vertex + " ");
for (int neighbour = 0; neighbour < srcMatrix[0].length; neighbour++)
{
if (srcMatrix[vertex][neighbour] == 1 && visitMatrix[neighbour] == 0)
{
System.out.print(vertex + " "); //If I don't print this DFS by itself works fine, but I want to print this because I want to backtrack when I get stuck
DftRecursive(srcMatrix, visitMatrix, neighbour);
}
}
}
Hope someone can figure out what I am doing wrong here (the problem is that I also need to print the backtracking, printing only DFS is fine, but backtracking is harder to do)
exchanging the print line and the DftRecursive line.
like this:
if (srcMatrix[vertex][neighbour] == 1 && visitMatrix[neighbour] == 0)
{
DftRecursive(srcMatrix, visitMatrix, neighbour);
System.out.print(vertex + " ");
}
I'm writing a method to store a number's prime factor.
I'm asked to use 2-d array to store its prime factor and the factor's number.
public static int[][] getMatrix (long x){
int[][] matrix =new int[10][2];
int count;
for (int i = 2, j = 0; i <=x / 2; i++) {
count=0;
while (x % i == 0) {
x = x/i;
count++;
}
matrix[j][0] = i;
matrix[j][1] = count;
j++;
}
return matrix;
}
But this code only store data into the first row of the array.
Could someone help me correct it or privide other ideas?
if I use the following code to output the result.
for(int row=0;row<b_matrix.length;row++)
{
for(int column=0;column<2;column++)
{
System.out.print(b_matrix[row][column]+" ");
}
}
And x=9 I got this:
2 0 3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
x=6 I got this:
2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
such as :6
matrix[0][0]=2 matrix[0][1]=1
matrix[1][0]=3 matrix[1][1]=1 //can't store
such as :9
matrix[0][0]=2 matrix[0][1]=0//only output the next row when this equals to 0
matrix[1][0]=3 matrix[1][1]=2
Your logic is right except, in the for loop i should go till x instead of x/2 as follows,
for (int i = 2, j = 0; i <= x; i++)
Output for getMatrix(60):
2 2
3 1
4 0
5 1
0 0
0 0
0 0
0 0
0 0
0 0
Hey I'm trying to code a model for the Least Recently Used paging algorithm. I'm relatively new to Java and coding in general and I just can't find the error in my code.
I want to implement the free memory with a 2 dimensional array, and when referencing a row i change the bits in the corresponding row to 1 and in the corresponding column to 0. Then I want to add up the elements in each row and sort them using the bubblesort algorithm.
There lies my Problem: it seems I just can't get the adding up or the sorting right. I hope someone can help me because it seems no matter what I try, it doesn't work.
Here is the part of my code in question since it seems to be in stackoverflows rules that I can't post the whole code.
page [][] is the 2d array which i use to change the bytes.
I would be very grateful for any help because I'm getting demotivated slowly but surely.
// array for bubblesort and adding up of rows
int sort[] = new int[page.length];
int sumtemp = 0;
int sum = 0;
for (int i = 0; i < page.length; i++) {
for (int j = 0; j < page[i].length; j++) {
sumtemp += page[i][j];
sum = sumtemp;
}
for (i = 0; i < page.length; i++)
sort[i] = sum;
sumtemp = 0;
}
// bubblesort
int n1 = sort.length;
int temp = 0;
for (int i = 0; i < n1; i++) {
for (int j = 1; j < (n1 - i); j++) {
if (sort[j - 1] > sort[j]) {
// sort
temp = sort[j - 1];
sort[j - 1] = sort[j];
sort[j] = temp;
}
}
}
My Output: it works until the last output, where the page to be deleted should be 5 not 4:
How many pages shall fit in the memory?
5
Which page do you want to reference? Please choose a number between 1 and 5. Have you finished referencing, please type in
4
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 0 1
0 0 0 0 0
Page to delete = 1
Which page do you want to reference? Please choose a number between 1 and 5.
Have you finished referencing, please type in
3
0 0 0 0 0
0 0 0 0 0
1 1 0 1 1
1 1 0 0 1
0 0 0 0 0
Page to delete = 1
Which page do you want to reference? Please choose a number between 1 and 5.
Have you finished referencing, please type in
1
0 1 1 1 1
0 0 0 0 0
0 1 0 1 1
0 1 0 0 1
0 0 0 0 0
Page to delete = 5
Which page do you want to reference? Please choose a number between 1 and 5.
Have you finished referencing, please type in
2
0 0 1 1 1
1 0 1 1 1
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
Page to delete = 4
Which page do you want to reference? Please choose a number between 1 and 5. Have you finished referencing, please type in
This should be the Bubble Sort logic:
for (int i = 0; i < n1; i++) {
for (int j = 0; j < (n1 - i - 1); j++) {
if (sort[j + 1] > sort[j]) { //ascending order
// sort
temp = sort[j + 1];
sort[j + 1] = sort[j];
sort[j] = temp;
}
}
I want to change a char[][] array (lets call it cA) into an adjacency matrix. An adjacency matrix has columns and rows equal to the number of elements in an array, and each vertex in the adjacency matrix is either true or false depending if the elements in the initial array are adjacent. I want to bend the rules a little and also constrain an adjacency matrix vertex to true iff the elements are adjacent and one of the elements is not a specific value.
Here's what cA array looks like:
z y z
z z z
z y y
The adjacency matrix (lets call it aM) for the cA array will be an int array of size [3*3][3*3]. The conditions for aM(i,j) to be true are that the elements i and j in cA array must be adjacent, but neither i or j can be "y".
Lets number the cA array elements 1 through 9.
1 2 3
4 5 6
7 8 9
aM can be described by doing the below operations:
aM(1,1) //false, no self-adjacency
aM(1,2) //false, 2 is a "y"
aM(1,3) //false, 1 is not adjacent to 3
aM(1,4) //true, 1 is adjacent to 4, neither are "y"
aM(1,5) //false, 1 is not adjacent to 5
aM(1,6) //false, 1 is not adjacent to 6
aM(1,7) through aM(1,9) //false, there is no adjacency between 1 and 7, 8, or 9
aM(2,1) through aM(2,9) //false, 2 is a "y"
...
Hopefully you get the idea. From the above, the adjacency matrix for cA is as below:
1 2 3 4 5 6 7 8 9 (i)
1 0 0 0 1 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 1 0 0 0
4 1 0 0 0 1 0 1 0 0
5 0 0 0 1 0 1 0 0 0
6 0 0 1 0 1 0 0 0 0
7 0 0 0 1 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0
(j)
The rule being aM(i,j) == 1 iff i != j, i != "y" && j != "y", and both i and j are adjacent to each other.
I'm having difficulty concocting an algorithm to create an adjacency matrix provided a char[][] array. I've defined the rules, but finding constraints for iteration is problematic.
Try this:
static void set(boolean[][] aM, int cols, int row0, int col0, int row1, int col1) {
int index0 = row0 * cols + col0;
int index1 = row1 * cols + col1;
aM[index0][index1] = aM[index1][index0] = true;
}
static boolean[][] adjacencyMatrix(char[][] cA) {
int rows = cA.length;
int cols = cA[0].length;
boolean[][] aM = new boolean[rows * cols][rows * cols];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (cA[i][j] == 'y')
continue;
if (i + 1 < rows && cA[i + 1][j] != 'y')
set(aM, cols, i, j, i + 1, j);
if (j + 1 < cols && cA[i][j + 1] != 'y')
set(aM, cols, i, j, i, j + 1);
}
}
return aM;
}
public static void main(String[] args) {
char[][] cA = {
{'z', 'y', 'z'},
{'z', 'z', 'z'},
{'z', 'y', 'y'},
};
boolean[][] aM = adjacencyMatrix(cA);
for (boolean[] row : aM) {
for (boolean cell : row)
System.out.print(cell ? "1" : "0");
System.out.println();
}
}
The result is:
000100000
000000000
000001000
100010100
000101000
001010000
000100000
000000000
000000000
I am making a scrolling game in Java , I would like a clarification on one point.
I do not save the game level in any structure java , I just read a file ( . gif )
which I modified in a way that :
I use the color decryption to parse through every pixel to pixel and place where the
object meets the requirements that I have established .
for example:
.
.
.
int w = image.getWidth(); //store the dimensions of the level image.
int h = image.getHeight();
for(int x = 0; x < w; x++){
for(int y = 0; y < h; y++){ //check every single pixel with this nested loop
int pixel = image.getRGB(x, y); //get the pixel's rgb value
TYPE_INT_ARGB formatint red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
if(red == 255 && green == 255 && blue == 0)
controller.addPlayer((float)x, (float)y);
else if(red == 255 && green == 255 && blue == 255)
controller.addTerrain(x, y);
}
as you can see i don't save the level in any structure, but I just scan the image file that represents it.
is a good idea to do it this way?
Naturally i store all objects with the controller class where i create an arrayList that contains all game 's objects .
You could make a .txt file and create a map like this:
20
10
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 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 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0
the 0 would represent air and the 1 a walkable tile. the first value is the map width and the second the map height.
I also recommend you to use a 2d array to store the map information. Now you can read the txt file with a BufferedReader. Hope this code below helps
private final int TILE_SIZE = 30;
private int[][] blocks;
private void loadMap(File file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
mapWidth = Integer.parseInt(reader.readLine());
mapHeight = Integer.parseInt(reader.readLine());
blocks = new int[mapHeight][mapWidth];
for(int col = 0; col < mapHeight; col ++) {
String line = reader.readLine();
String[] tokens = line.split(" ");
for(int row = 0; row < numBlocksRow; row++) {
blocks[col][row] = Integer.parseInt(tokens[row]);
}
}
reader.close();
} catch(Exception e) {
e.printStackTrace();
}
}
private void render(Graphics2D g) {
for(int col = 0; col < mapHeight; col ++) {
for(int row = 0; row < numBlocksRow; row++) {
int block = blocks[col][row];
Color color;
if(block == 1) {
color = Color.white;
} else {
color = Color.black;
}
g.fillRect(row * TILE_SIZE, col * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
}
}