I have a large array of arbitrary size. It's a square array. I'm trying to grasp how to traverse it diagonally like a / rather than a \ (what I already know how to do). I have the following code thus far:
char[][] array = new char[500][500];
//array full of random letters
String arrayLine = "";
for (int y = 0; y < array.length; y++) {
for (int x = 0; x < array.length; x++) {
for (???) {
arrayLine = arrayLine + array[???][???];
}
}
System.out.println(arrayLine);
}
I have three loops, because this is how I did the other diagonal:
for (int y = 0; y < array.length; y++) {
for (int x = 0; x < array.length; x++) {
for (int z = 0; z < array.length-y-x; z++) {
arrayLine = arrayLine + array[y+z][x+z];
}
}
System.out.println(arrayLine);
}
In my attempts, I keep going outside the boundaries and get an ElementOutOfBounds exception. Say the array is as below (a 3x3 instead of 500x500):
A B C
D E F
G H I
I want to print out the following as strings:
A
BD
CEG
FH
I
A previous SO question had a similar problem with integer arrays, and the solution is based off of the sum of array elements. But I'm working with chars, so I can't think of a methodology to get it.
Think about the coordinates of the cells:
. 0 1 2
0 A B C
1 D E F
2 G H I
For any diagonal, all of the elements have something in common: the sum of an element's coordinates is a constant. Here are the constants:
0 = 0+0 (A)
1 = 1+0 (B) = 0+1 (D)
2 = 2+0 (C) = 1+1 (E) = 0+2 (G)
3 = 2+1 (F) = 1+2 (H)
4 = 2+2 (I)
The minimum constant is the smallest coordinate sum, 0. The maximum constant is the largest coordinate sum. Since each coordinate component can go up to array.length - 1, the maximum constant is 2 * (array.length - 1).
So the thing to do is iterate over the constants. For each constant, iterate over the elements whose coordinates sum to the constant. This is probably the simplest approach:
for (int k = 0; k <= 2 * (array.length - 1); ++k) {
for (int y = 0; y < array.length; ++y) {
int x = k - y;
if (x < 0 || x >= array.length) {
// Coordinates are out of bounds; skip.
} else {
System.out.print(array[y][x]);
}
}
System.out.println();
}
However, that will end up iterating over a lot of out-of-bounds coordinates, because it always iterates over all possible y coordinates, even though only one diagonal contains all possible y coordinates. Let's change the y loop so it only visits the y coordinates needed for the current k.
One condition for out-of-bounds coordinates is x < 0. Substitute the definition of x and solve:
x < 0
k - y < 0
k < y
y > k
So when y > k, x will be negative. Thus we only want to loop while y <= k.
The other condition for out-of-bounds coordinates is x >= array.length. Solve:
x >= array.length
k - y >= array.length
k - array.length >= y
y <= k - array.length
So when y <= k - array.length, x will be too large. Thus we want to start y at 0 or k - array.length + 1, whichever is larger.
for (int k = 0; k <= 2 * (array.length - 1); ++k) {
int yMin = Math.max(0, k - array.length + 1);
int yMax = Math.min(array.length - 1, k);
for (int y = yMin; y <= yMax; ++y) {
int x = k - y;
System.out.print(array[y][x]);
}
System.out.println();
}
Note: I have only proven this code correct. I have not tested it.
Much simpler way is to check the sum if indexes are equals to the array.length = 1; for diagonalRight and for diagonalLeft just check if i is equals j
Example:
digonalLeft sums \ of matrix, because (0,0) (1,1) (2,2) makes the diagonal.
diagonalRight sums / of matrix, because (0+2) = (1+1) = (2+0) = 2 and 2 is the array.length - 1.
long diagonalLeft = 0;
long diagonalRight = 0;
for (int i = 0; i < array.lenth - 1; i++) {
for (int j = 0; j < array.length -1; j++) {
if (i == j) digonalLeft += array[i][j];
if (i + j == array.length - 1) diagonalRight += array[i][j];
}
}
Related
I am trying to solve the Dynamic Problem question to find unique paths between origin and the last cell of a board. The problem is present on leetcode here https://leetcode.com/problems/unique-paths/.
The problem statement is -
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
I am stuck on a test case with m = 23 and n = 12. Can you please explain the inconsistency in my code?
public int uniquePaths(int m, int n) {
return getPath(m,n,1,1);
}
HashMap<String,Integer> map = new HashMap<>();
private int getPath(int m, int n, int x, int y){
if(x == n && y == m) return 1;
else if(x > n || y > m) return 0;
String s = ""+x+y;
if(map.get(s) != null) return map.get(s);
int max =0;
if(x < n)
max = max + getPath(m,n,x+1,y);
if(y < m)
max = max + getPath(m,n,x,y+1);
map.put(s,max);
return max;
}
}
On m = 23 and n = 12, I am getting 192184665 as output whereas 193536720 is the expected result.
Look into my comment. You can solve this in Sigma (m * n) with bottom-up dp.
Create an int matrix of size [m * n].
int[] a = new int[n][m];
for (int i = 0; i < n-1; ++i) {
a[i][m-1] = 1;
}
for (int j = 0; i < m-1; ++j) {
a[n-1][j] = 1;
}
for (int i = n-2; i >= 0; --i) {
for (int j = m-2; j >= 0; --j) {
a[i][j] = a[i+1][j] + a[i][j+1];
}
}
System.out.print(a[0][0]);
I'm trying to check the neighboring values of each element in a 2D array but am getting an IndexOutOfBoundsException when I reach the sides of the array or a corner. For example if my array is:
|2|4|2|7|8|
|8|1|0|5|6|
|0|3|1|5|2|
|1|9|7|2|0|
I know that all the neighbors of 8 are 7,5 and 6, but my if statements don't check the bounds properly. The code I have for this is:
int numOfRows = imageArray.length;
int numOfColumns = imageArray[0].length;
for(int i = 0; i < numOfRows; i++)
for(int j = 0; j < numOfColumns; j++)
if((j+1) < numOfColumns-1)
if((i+1) < numOfRows-1)
if((j-1) > 0 )
if((i-1) > 0 )
if((i+1) < numOfColumns-1 && (j+1) < numOfRows-1)
if((i-1) >= 0 && (j-1) >= 0)
if((i+1) < numOfColumns-1 && (j-1) >= 0)
if((i-1) >= 0 && (j+1) < numOfRows-1)
I've been working on this for a while and have gone through many different techniques to solve this. Any help would be great. Thanks.
If you're trying to get all the neighbor cells and do something with them, for example add them, then you need to do some sort of bounds checking, for example something modified from this could work:
for (int i = 0; i < numOfRows; i++) {
for (int j = 0; j < numOfCols; j++) {
// check all bounds out of range:
int iMin = Math.max(0, i - 1);
int iMax = Math.min(numOfRows - 1, i + 1);
int jMin = Math.max(0, j - 1);
int jMax = Math.min(numOfCols - 1, j + 1);
// loop through the above numbers safely
for (int innerI = iMin; innerI <= iMax; innerI++) {
for (int innerJ = jMin; innerJ <= jMax; innerJ++) {
if (i != innerI && j != innerJ) {
// do what needs to be done
}
}
}
}
}
Caveat: code has not been compiled nor tested and is mainly to show you the idea of what can be done rather than a copy-paste solution
So at this point I have no idea why my for loop doesn't work. It's probably just an minor mistake but I cant find it so I'd appreciate your help. This is how it looks like:
int[] values = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
for (int x = 0; x >= array1[i][0] && x <= array1[i][2]; x++) {
for (int y = 0; y >= array1[i][1] && y <= array1[i][3]; y++) {
values[i] += array2[x][y];
}
}
}
Numbers I'm using to test it on and some context:
Both arrays are 2D,
array1 looks like this:
(array1.length = 8) and array2 like this:
array1 holds coordinates of left-top and right-bottom corner of the area in array2 so for example 0 0 2 1 are coordinates for array2[0][0] and array2[2][1] and I'm trying to get my for loop to add all numbers in the area. So for this example it would add those four numbers:
My output is: 3 0 0 0 0 0 0 0
You should use explicit variable names not to do errors in meaning.
Moreover, start your loop at expected coordinates. So your loop can be written like this:
int[] values = new int[array1.length];
for (int i = 0; i < values.length; i++) {
int fromX = array1[i][0];
int fromY = array1[i][1];
int toX = array1[i][2];
int toY = array1[i][3];
for (int x = fromX; x <= toX; x++) {
for (int y = fromY; y <= toY; y++) {
values[i] += array2[x][y];
}
}
}
You can check the result:
System.out.println( Arrays.toString( values) );
// Output is [3, 0, 4, -2, 0, 1, -2, 3]
I think you should do this (add <= and >=) and change statring value. Also consider filling "values" with zeros since you're summing it up.
for (int x = array1[i][0]; x >= array1[i][0] && x =< array1[i][2]; x++) {
for (int y = array1[i][1]; y >= array1[i][1] && y =< array1[i][3]; y++) {
values[i] += array2[x][y];
}
}
I don't understand what is wrong with this loop, it normaly would check the surrounding cells of (x,y).
When calling this function, there is only 4 passages in the loop and not 8
This is probably a stupid mistake but I cant see it !
private int countAliveNeighbors(int x, int y) {
int count = 0;
for (int i = Math.max(0, x - 1); i < Math.min(grid.getWidth()-1, x + 1); i++) {
for (int j = Math.max(0, y - 1); j < Math.min(grid.getHeight()-1, y + 1); j++) {
if (!(i == x && j == y)) {
if (grid.isAlive(i, j))
count++;
}
}
}
return count;
}
Your upper limit in your for statements should by checking for <= not just less-than. By testing for less-than you stop before getting to x+1 or y+1. It should look like this:
for (int i = Math.max(0, x - 1); i <= Math.min(grid.getWidth()-1, x + 1); i++) {
for (int j = Math.max(0, y - 1); j <= Math.min(grid.getHeight()-1, y + 1); j++) {
You are given a 2D array as a string and a word via keyboard. The word
can be in any way (all 8 neighbors to be considered) but you can’t use
same character twice while matching. Return word's first and last
character's index as (x,y). If match is not found return -1.
That's the question. I'm having trouble with searching. I tried that:
int x=0,y=0;
for(int f=0; f<WordinArray.length; f++){
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[0].length; j++){
if(matrix[i][j].equals(WordinArray[f])){
x=i; y=j;
System.out.print("("+x+","+y+")");
}
}
}
}
But, That code is not working as it is supposed to. How else I can write this searching code?
Referring to Sixie's code
Assuming this is a valid input/output to your program?
Size:
4x4
Matrix:
a b c d
e f g h
i j k l
m n o p
Word: afkp
(0,0)(3,3)
I edited your code, so that it should work for input on this form (it is case sensitive at the moment, but can easily be changed by setting .toLowerCase()
Scanner k = new Scanner(System.in);
System.out.println("Size: ");
String s = k.nextLine();
s.toUpperCase();
int Xindex = s.indexOf('x');
int x = Integer.parseInt(s.substring(0, Xindex));
int y = Integer.parseInt(s.substring(Xindex + 1));
System.out.println("Matrix:");
char[][] matrix = new char[x][y];
for (int i = 0; i < x; i++) {
for (int p = 0; p < y; p++) {
matrix[i][p] = k.next().charAt(0);
}
}
System.out.print("Word: ");
String word = k.next();
int xStart = -1, yStart = -1;
int xEnd = -1, yEnd = -1;
// looping through the matrix
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
// when a match is found at the first character of the word
if (matrix[i][j] == word.charAt(0)) {
int tempxStart = i;
int tempyStart = j;
// calculating all the 8 normals in the x and y direction
// (the 8 different directions from each cell)
for (int normalX = -1; normalX <= 1; normalX++) {
for (int normalY = -1; normalY <= 1; normalY++) {
// go in the given direction for the whole length of
// the word
for (int wordPosition = 0; wordPosition < word
.length(); wordPosition++) {
// calculate the new (x,y)-position in the
// matrix
int xPosition = i + normalX * wordPosition;
int yPosition = j + normalY * wordPosition;
// if the (x,y)-pos is inside the matrix and the
// (x,y)-vector normal is not (0,0) since we
// dont want to check the same cell over again
if (xPosition >= 0 && xPosition < x
&& yPosition >= 0 && yPosition < y
&& (normalX != 0 || normalY != 0)) {
// if the character in the word is not equal
// to the (x,y)-cell break out of the loop
if (matrix[xPosition][yPosition] != word
.charAt(wordPosition))
break;
// if the last character in the word is
// equivalent to the (x,y)-cell we have
// found a full word-match.
else if (matrix[xPosition][yPosition] == word
.charAt(wordPosition)
&& wordPosition == word.length() - 1) {
xStart = tempxStart;
yStart = tempyStart;
xEnd = xPosition;
yEnd = yPosition;
}
} else
break;
}
}
}
}
}
}
System.out.println("(" + xStart + "," + yStart + ")(" + xEnd + ","
+ yEnd + ")");
k.close();
I think you need to plan your algorithm a bit more carefully before you start writing code. If I were doing it, my algorithm might look something like this.
(1) Iterate through the array, looking for the first character of the word.
(2) Each time I find the first character, check out all 8 neighbours, to see if any is the second character.
(3) Each time I find the second character as a neighbour of the first, iterate along the characters in the array, moving in the correct direction, and checking each character against the word.
(4) If I have matched the entire word, then print out the place where I found the match and stop.
(5) If I have reached the edge of the grid, or found a character that doesn't match, then continue with the next iteration of loop (2).
Once you have your algorithm nailed down, think about how to convert each step to code.
If I understood your question right. This is a quick answer I made now.
int H = matrix.length;
int W = matrix[0].length;
int xStart = -1, yStart = -1;
int xEnd = -1, yEnd = -1;
String word = "WordLookingFor".toLowerCase();
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (matrix[i][j] == word.charAt(0)) {
int tempxStart = i;
int tempyStart = j;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int k = 0; k < word.length(); k++) {
int xx = i+x*k;
int yy = j+y*k;
if(xx >= 0 && xx < H && yy >= 0 && yy < W && (x != 0 || y != 0)) {
if(matrix[xx][yy] != word.charAt(k))
break;
else if (matrix[xx][yy] == word.charAt(k) && k == word.length()-1) {
xStart = tempxStart;
yStart = tempyStart;
xEnd = xx;
yEnd = yy;
}
} else
break;
}
}
}
}
}
}
A little trick I used for checking all the 8 neighbors is to use two for-loops to create all the directions to go in:
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
if(x !=0 || y != 0)
System.out.println(x + ", " + y);
}
}
This creates
-1, -1
-1, 0
-1, 1
0, -1
0, 1
1, -1
1, 0
1, 1
Notice: All but 0,0 (you don't want to revisit the same cell).
The rest of the code is simply traversing though the matrix of characters, and though the whole length of the word you are looking for until you find (or maybe you don't find) a full match.
This time the problem is that how could I print word's first and last
letter's indexes. I tried various ways like printing after each word
was searched. But, all of them didn't work. I am about to blow up.
int[] values = new int[2];
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[0].length; j++){
if(Character.toString(word.charAt(0)).equals(matrix[i][j]) == true || Character.toString(ReversedWord.charAt(0)).equals(matrix[i][j]) == true ){
System.out.print("("+ i + "," +j+")");
//First letter is found.Continue.
for(int p=1; p<word.length(); p++){
try{
for (int S = -1; S <= 1; S++) {
for (int SS = -1; SS <= 1; SS++) {
if(S !=0 || SS != 0)
if(matrix[i+S][j+SS].equals(Character.toString(word.charAt(p))) && blocksAvailable[i+S][j+SS] == true ||
matrix[i+S][j+SS].equals(Character.toString(ReversedWord.charAt(p))) && blocksAvailable[i+S][j+SS] == true) {
values[0] = i+S;
values[1] = j+SS;
blocksAvailable[i+S][j+SS] = false;
}
}
}
}catch (ArrayIndexOutOfBoundsException e) {}