0

I have a 2D array in Java like so:

int 2d_arr[5][5];

Take, for example, the board that looks like this:

1 1 1 1 1
0 1 1 1 1
1 1 2 1 0
0 1 1 1 1
0 0 1 1 1

Starting from the 2 in the 3rd row, I want to be able to move in every direction (Up, down, left, right, and diagonals). In code, how do I traverse the array in every direction till I find a 0?

My theoretical idea would be to iterate in every direction sequentially. For example, start by going up, so I would check all the values on the line above 2

1
1
2

Since I didn't find any zeros, check the top right diagonal

 1
 1
 2

Still no 0s, so go onto the right. As soon as I find my first 0, break.

Attempt: I actually know how to do to this with a bunch of if and for loops, but I am looking for a way to edit that code to a simpler and easier to read version

But I'm new to java so I don't know the best way to go about this. Any ideas?

asked Mar 24, 2013 at 21:51
8
  • 2
    Have you tried anything at all? (Helpful links for asking better questions: How to Ask, FAQ) Commented Mar 24, 2013 at 21:53
  • Why are you starting at the 2? Do you need to find the closest 0 to the 2? Commented Mar 24, 2013 at 21:57
  • yah, the two is the important piece. I need to find any zeros in it's 8 directions if there are any. Commented Mar 24, 2013 at 22:00
  • so you need to know the position of the first zero found, need to know how many directions you can go in to find the 0, or need a true/false answer about whether there is a zero in any of the 8 directions? Commented Mar 24, 2013 at 22:09
  • just need a true/false where there are any 0s in any 8 directions. Don't even need to go through all 8 directions, just return true for the first zero found. Commented Mar 24, 2013 at 22:16

1 Answer 1

1

A TwoD Iterator would obviously be a good start. I expect you could do the rest yourself without much effort.

Finding the first zero in your scenario would involve iterating through eachDirection and iterating across the board in that direction until the iteration ends or you find your zero.

Doing a spiral search would involve starting an iterator in each direction and doing a step and check on each one in turn until one returns a point where a zero can be found.

public class TwoDIteratorTest {
 // An ubiquitous Point class
 static class Point {
 final int x;
 final int y;
 public Point(int x, int y) {
 this.x = x;
 this.y = y;
 }
 @Override
 public String toString() {
 return "{" + x + "," + y + "}";
 }
 }
 // All possible directions.
 enum Direction {
 North(0, 1),
 NorthEast(1, 1),
 East(1, 0),
 SouthEast(1, -1),
 South(0, -1),
 SouthWest(-1, -1),
 West(-1, 0),
 NorthWest(-1, 1);
 private final int dx;
 private final int dy;
 Direction(int dx, int dy) {
 this.dx = dx;
 this.dy = dy;
 }
 // Step that way
 public Point step(Point p) {
 return new Point(p.x + dx, p.y + dy);
 }
 }
 static class TwoDIterator implements Iterable<Point> {
 // Where we are now
 Point i;
 // Direction to move in.
 private final Direction step;
 // Limits.
 private final Point min;
 private final Point max;
 // Next position to go to.
 Point next = null;
 // Normal constructor.
 public TwoDIterator(Point start, Direction step, Point min, Point max) {
 i = next = start;
 this.step = step;
 this.min = min;
 this.max = max;
 }
 // Some simplified constructors
 public TwoDIterator(int x, int y, Direction step, Point min, Point max) {
 this(new Point(x, y), step, min, max);
 }
 public TwoDIterator(int x, int y, Direction step, int minx, int miny, int maxx, int maxy) {
 this(new Point(x, y), step, new Point(minx, miny), new Point(maxx, maxy));
 }
 // The iterator.
 @Override
 public Iterator<Point> iterator() {
 return new Iterator<Point>() {
 // hasNext calculates next if necessary and checks it against the stabliched limits.
 @Override
 public boolean hasNext() {
 if (next == null) {
 // Step one.
 next = step.step(i);
 // Stop at limits.
 if (next.x < min.x
 || next.x > max.x
 || next.y < min.y || next.y > max.y) {
 next = null;
 }
 }
 return next != null;
 }
 @Override
 public Point next() {
 if (hasNext()) {
 // Make our move.
 i = next;
 next = null;
 return i;
 }
 return null;
 }
 @Override
 public void remove() {
 throw new UnsupportedOperationException("Not supported.");
 }
 };
 }
 }
 public void test() {
 // Test all directions.
 for (Direction d : Direction.values()) {
 System.out.print(d + " - ");
 for (Point p : new TwoDIterator(0, 0, d, -5, -5, 5, 5)) {
 System.out.print(p + ",");
 }
 System.out.println();
 }
 }
 public static void main(String[] args) throws InterruptedException {
 new TwoDIteratorTest().test();
 }
}
answered Mar 24, 2013 at 22:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.