Access diagonals of a particular cell in matrix - java

Solving problem of 8 queens i am actually stuck in how to access the diagonals of a particular cell to set it to 1 when placing a queen. There is simple logic for the cells which are in vertical or horizontal of the selected cell where queen has to be placed.
Here is the code of my function to set the cells for the queen to 1 which that queen can attack.
static int[][] SetPos(int csp[][],int row,int col){
int count = 0, n = csp.length;
for (int i = 0; i < csp.length; i++) {
for (int j = 0; j < csp.length; j++) {
if(i==row || j==col){
csp[i][j]=1;
}
if(row==col && i==j){
//csp[row][col]=1;
csp[i][j]=1;
}
if(row+count==i && col+count==j){
csp[i][j]=1;
}
}
count++;
}
return csp;
}
How can this condition be improved:
if(row+count==i && col+count==j){
csp[i][j]=1;
}
So that i get result for cell(5,5) as :
1 0 0 0 0 1 0 0
0 1 0 0 0 1 0 0
0 0 1 0 0 1 0 0
0 0 0 1 0 1 0 1
0 0 0 0 1 1 1 0
1 1 1 1 1 1 1 1
0 0 0 0 1 1 1 0
0 0 0 1 0 1 0 1
not like this :
1 0 0 0 0 1 0 0
0 1 0 0 0 1 0 0
0 0 1 0 0 1 0 0
0 0 0 1 0 1 0 0
0 0 0 0 1 1 0 0
1 1 1 1 1 1 1 1
0 0 0 0 0 1 1 0
0 0 0 0 0 1 0 1
Backtracking is not the concern right now.

Your condition
if(row==col && i==j){
//csp[row][col]=1;
csp[i][j]=1;
}
should only work if row==col.
Otherwise you will only get the horizontal and vertical numbers.
If you want to mark the diagonals starting from row and col, this code should work:
if(i-j==row-col){ //diagonal up_left to down_right
csp[i][j]=1;
}
if(i+j==row+col) { //diagonal down_left to up_right
csp[i][j]=1;
}

Related

DFS Adjacency matrix wrong traversal (when backtracking)

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 + " ");
}

Iterative Deepening using recursion bug

This is iterative deepening search and I want to keep track of the nodes generated before the goal state. I am using RUNNING_TIME as a counter and then assign it to each node in the tree. The thing is, because o recursion RUNNING_TIME is incremented more than once and the value is not valid anymore. Hence, I get a RUNNING_TIME for BFS which is better than this one which theoretically doesn't make sense (only worst case scenario). Do you have any idea how I should increment this value in my methods? I sincerely have no idea, thanks.
public void IDS() {
Grid startNode = new Grid(initState, A, B, C, agent);
for(int depth = 0; depth < Integer.MAX_VALUE; depth ++) {
Grid found = DLS(startNode, depth);
if(found != null) {
retracePath(found);
return;
}
}
}
public Grid DLS(Grid current, int depth) {
RUNNING_TIME ++;
current.nodesGeneratedBefore = RUNNING_TIME;
if(depth == 0 && current.checkGoalState()) {
return current;
}
if(depth > 0) {
for(Grid neighbor : current.getNeighbors(current)) {
Grid found = DLS(neighbor, depth - 1);
if(found != null) {
return found;
}
}
}
return null;
}
EDIT: Also I forgot to mention that I get the correct path to the destination, but only the "RUNNING_TIME" variable is wrongly incremented.
0 0 0 0
0 0 0 0
0 0 0 -1
1 2 3 0
Number of nodes generated by now 4680622
0 0 0 0
0 0 0 0
0 0 -1 0
1 2 3 0
Number of nodes generated by now 6297726
0 0 0 0
0 0 0 0
0 -1 0 0
1 2 3 0
Number of nodes generated by now 7106272
0 0 0 0
0 0 0 0
0 2 0 0
1 -1 3 0
Number of nodes generated by now 7760396
0 0 0 0
0 0 0 0
0 2 0 0
-1 1 3 0
Number of nodes generated by now 7837602
0 0 0 0
0 0 0 0
-1 2 0 0
0 1 3 0
Number of nodes generated by now 7837603
0 0 0 0
0 0 0 0
2 -1 0 0
0 1 3 0
Number of nodes generated by now 7842162
0 0 0 0
0 0 0 0
2 1 0 0
0 -1 3 0
Number of nodes generated by now 7848122
0 0 0 0
0 0 0 0
2 1 0 0
0 3 -1 0
Number of nodes generated by now 7849095
0 0 0 0
0 0 0 0
2 1 -1 0
0 3 0 0
Number of nodes generated by now 7849096
0 0 0 0
0 0 -1 0
2 1 0 0
0 3 0 0
Number of nodes generated by now 7849097
0 0 0 0
0 -1 0 0
2 1 0 0
0 3 0 0
Number of nodes generated by now 7849111
0 0 0 0
0 1 0 0
2 -1 0 0
0 3 0 0
Number of nodes generated by now 7849125
0 0 0 0
0 1 0 0
-1 2 0 0
0 3 0 0
Number of nodes generated by now 7849127

Java DFS for adjacency matrix

I am working on a project to implement a DFS for a given adjacency matrix. I have the file reading in correctly and I have the program outputting a list of first encountered vertices, however, I am not getting the correct list. I am getting 1 2 6 5 7 3 4 8 but I need to get 1 2 6 7 4 3 5 8. I am not sure where I am going wrong, but any suggestions would help. Below I have attached the part of code the outputs the encountered vertices and the file with the adjacency matrix.
Code with the output:
public static void dfs(int vertex, boolean[] visitArray, ArrayList<Integer> DFSvertexList, ArrayList<Integer> DFSdeadEndList, int[][] DFStreeEdgeGraph, ArrayList<Integer> vertexList, ArrayList<Integer> iList, boolean[] visitArray2){
int ver = 0;
DFSvertexList.add(vertex);
visitArray[vertex] = true;
for(int i = 0; i <= dim-1; i++){
if(graph[vertex][i] == 1){
if(!visitArray[i]){
ver = i;
DFStreeEdgeGraph[vertex][i] = 1;
dfs(i, visitArray, DFSvertexList, DFSdeadEndList, DFStreeEdgeGraph, vertexList, iList, visitArray2);
DFSdeadEndList.add(i);
}
else if(i >= vertex && i != ver){
vertexList.add(vertex+1);
iList.add(i+1);
}
}
}
}
Here is the adjacency matrix:
0 1 0 0 1 1 0 0
1 0 0 0 0 1 1 0
0 0 0 1 0 0 1 0
0 0 1 0 0 0 0 1
1 0 0 0 0 1 0 0
1 1 0 0 1 0 0 0
0 1 1 0 0 0 0 1
0 0 0 1 0 0 1 0
Please let me know if more code is needed.

Copying 2d array elements from one to the other

I want to find adjugate matrix from a n x n matrix,
e.g the matrix is 4x4
{0, 11, 15, 11},
{7, 0, 1 , 8},
{4, 19, 0 , 6},
{2, 3, 5, 0}
This is my output
Matrix One
Loop: 1
0 1 8 19 0 6 3 5 0
Loop: 2
7 1 8 4 0 6 2 5 0
Loop: 3
7 0 8 4 19 6 2 3 0
Loop: 4
7 0 1 4 19 0 2 3 5
Loop: 5
11 15 11 19 0 6 3 5 0
Loop: 6
0 15 11 4 0 6 2 5 0
Loop: 7
0 11 11 4 19 6 2 3 0
Loop: 8
0 11 15 4 19 0 2 3 5
Loop: 9
11 15 11 0 1 8 3 5 0
Loop: 10
0 15 11 7 1 8 2 5 0
Loop: 11
0 11 11 7 0 8 2 3 0
Loop: 12
0 11 15 7 0 1 2 3 5
Loop: 13
11 15 11 0 1 8 19 0 6
Loop: 14
0 15 11 7 1 8 4 0 6
Loop: 15
0 11 11 7 0 8 4 19 6
Loop: 16
0 11 15 7 0 1 4 19 0
I stored the values into another 2darray(matrixTwo) to calculate the determinant to find the inverse matrix but found out that what is stored in my matrix two isn't the same as the output above.why is value stored different? please help
Matrix Two
Loop: 1
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 2
7 0 1 8 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 3
4 19 0 6 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 4
2 3 5 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 5
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 6
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 9
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 11
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 12
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 13
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 14
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Loop: 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
My Codes
public class Matrix {
public static void main(String argv[]) {
int matrix[][]= { {0, 11, 15, 11},
{7, 0, 1 , 8},
{4, 19, 0 , 6},
{2, 3, 5, 0}
};
int matrixTwo[][] = new int[(matrix.length) * (matrix.length)][(matrix.length) * (matrix.length)];
int ctr = 0;
System.out.println("Matrix One");
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix.length; j++) {
ctr++;
System.out.println("\nLoop: " + ctr);
for(int row = 0;row < matrix.length; row++) {
for(int col = 0; col < matrix[row].length; col++) {
if(row != i && col != j) {
System.out.print(matrix[row][col] + " ");
matrixTwo[row][col] = matrix[row][col];
}
}
}
}
}
int ctrTwo = 0;
System.out.println("\n\nMatrix Two");
for(int row = 0; row < matrixTwo.length; row++) {
ctrTwo++;
System.out.println("Loop: " + ctrTwo);
for(int col = 0; col < matrixTwo.length; col++) {
System.out.print(matrixTwo[row][col] + " ");
}
System.out.println();
}
}
These is different because they are printed in different orders
For a example,
From the matrix one
Loop: 1
0 1 8 19 0 6 3 5 0
prints
Loop: 1
matrix[1][1] = 0 matrix[1][2] = 1 matrix[1][3] = 8 matrix[2][1] = 19 matrix[2][2] = 0 matrix[2][3] = 6 matrix[3][1] = 3 matrix[3][2] = 5 matrix[3][3] = 0
From the matrixTwo
Loop: 1
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0
prints
Loop: 1
matrixTwo[0][0] = 0 matrixTwo[0][1] = 11 matrixTwo[0][2] = 15 matrixTwo[0][3] = 11 matrixTwo[0][4] = 0 matrixTwo[0][5] = 0 matrixTwo[0][6] = 0 matrixTwo[0][7] = 0 matrixTwo[0][8] = 0 matrixTwo[0][9] = 0 matrixTwo[0][10] = 0 matrixTwo[0][11] = 0 matrixTwo[0][12] = 0 matrixTwo[0][13] = 0 matrixTwo[0][14] = 0 matrixTwo[0][15] = 0
Use System.out.print("matrix[" + row + "][" + col + "] = " + matrix[row][col] + " ");. You can see the difference
right now, you are simply copying the first matrix into the second.
here is the testclass where you can see it:
public class test2 {
int matrix[][] = { { 0, 11, 15, 11 }, { 7, 0, 1, 8 }, { 4, 19, 0, 6 },
{ 2, 3, 5, 0 } };
int matrixTwo[][] = new int[(matrix.length) * (matrix.length)][(matrix.length)
* (matrix.length)];
public test2() {
int ctr = 0;
System.out.println("Matrix One");
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix.length; col++) {
ctr++;
System.out.println("\nLoop: " + ctr);
for (int row1 = 0; row1 < matrix.length; row1++) {
for (int col1 = 0; col1 < matrix[row1].length; col1++) {
if (row1 != row && col1 != col) {
System.out.print(matrix[row1][col1] + " ");
matrixTwo[row1][col1] = matrix[row1][col1];
}
}
}
}
}
int ctrTwo = 0;
System.out.println("\n\nMatrix Two");
for (int row = 0; row < matrixTwo.length; row++) {
ctrTwo++;
System.out.println("Loop: " + ctrTwo);
for (int col = 0; col < matrixTwo.length; col++) {
System.out.print(matrixTwo[row][col] + " ");
}
System.out.println();
}
printArray(matrix);
System.out.println();
printArray(matrixTwo);
}
public void printArray(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.printf("%3d", matrix[i][j]);
}
System.out.println();
}
}
public static void main(String argv[]) {
new test2();
}
}
the result:
matrix:
0 11 15 11
7 0 1 8
4 19 0 6
2 3 5 0
matrixTwo:
0 11 15 11 0 0 0 0 0 0 0 0 0 0 0 0
7 0 1 8 0 0 0 0 0 0 0 0 0 0 0 0
4 19 0 6 0 0 0 0 0 0 0 0 0 0 0 0
2 3 5 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 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 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
this is not what you wanted to do.
think about what is done in this line: matrixTwo[row][col] = matrix[row][col];

Java 2d game tiled map clipping?

Well I've created a simple tile map loading, from .txt file & then drawing.
25
15
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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 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 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 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 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 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 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
So if the current tile is 1, it will be a green block.
Now the game window looks like this (ignore yellow dots at top):
(source: gyazo.com)
Okay everything is fine and great, but now can you see the red box? that's the character, and when it walks, I don't want it to go over the gates (any green block).
How can I do that?
My attempt:
private void makeClips() {
int[][] tiledMap = this.map.getMap();
for (int i = 0; i < this.map.getHeight(); i++) {
for (int j = 0; j < this.map.getWidth(); j++) {
if (tiledMap[i][j] == 1) {
clips.add(new Clip(j * 30, i * 30, 0, 0));
System.out.println("Added clip: " + j + " " + i + " " + (j + 30) + " " + (i + 30));
}
}
}
}
That should create an array of clips, so we can check if player's next walk equals to the clips coordinates, but I've had problems with setting the clip x, y like what will it's position be.
The 30 is the tile size, so each block will be 30 width 30 height sized.
And then in the walking method i've done this:
for (Clip clip : clips) {
if (myPlayer.getX() + x >= clip.getFirstX() && myPlayer.getX() + x <= clip.getSecX()
&& myPlayer.getY() + y >= clip.getFirstY() && myPlayer.getY() + y <= clip.getSecY()) {
System.out.println("Bad");
return;
}
}
But I don't know, this is 100% incorrect, mostly the coordinate calculating part.
What would you do in this case?
This is the drawing part for map:
private void renderMap(Graphics2D g) {
int[][] tiledMap = this.map.getMap();
for (int i = 0; i < this.map.getHeight(); i++) {
for (int j = 0; j < this.map.getWidth(); j++) {
int currentRow = tiledMap[i][j];
if (currentRow == 1) {
g.setColor(Color.green);
}
if (currentRow == 0) {
g.setColor(Color.black);
}
g.fillRect(0 + j * map.getTileSize(), 0 + i * map.getTileSize(),
map.getTileSize(), map.getTileSize());
g.setColor(Color.yellow);
for (Clip clip : clips) {
g.fillRect(clip.getFirstX(), clip.getFirstY(), 2, 2);
}
}
}
}
What can I do?
Why not do a check every time you move your player on the tile you want to move him onto.
Lets assume the player is at (0,0) on this small map.
0 0 1
0 0 0
0 0 0
We will try and move him to the right twice. The first time will work as at the position (1,0) the tile is equal to 0. The second time we try this the tile at (2,0) will return 1 and the player won't move.
if (tiledMap[player.getX() + 1)[player.getY()] == 1) {
//do nothing
} else {
//move player
}

Categories