1. Member
Join Date
Sep 2012
Posts
5
Rep Power
0

Given a matrix of elements equal to 0 or 1. Units in such a matrix form
domains, and the domain contains only the neighboring elements in the horizontal and
vertical. The program should determine the number of domains in a given matrix.
input
A string containing the path to the file containing the matrix.
Example of a matrix:
1 0 0 0 1 0
1 1 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1
This matrix can be determined 4 different domain:
{(1,1), (2,1), (2,2)}, {(1,5)}, {(2,6)}, {(4,6), (5,6)}.

2. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
9

## Re: Java Task with matrix.

...and? What have you tried? Where exactly are you stuck? Contributors won't do this for you, but will point you in the right direction. Without knowing the answers to those question, we have no clue which direction to point

3. Member
Join Date
Sep 2012
Posts
5
Rep Power
0

## Re: Java Task with matrix.

Hi, every one! I written code for reading this maxrix from file and displaying all coordinats of ones in matrix. But when I try display only ones without the neighboring elements in the horizontal and vertical:
1 0 0 0 1 0
1 1 0 0 0 1
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 1
And I have got exception - ArrayIndexOutOfBoundsException, because I exceed the length of the array. How to fix it? Thanks!

Java Code:
public class Matrix {

public static int a;

public static void main(String[] args) throws IOException {

File file = new File("src\\array.txt");
int row = 0;
int col = 0;
int[][] data = new int[5][6];
String line = null;

while ((line = bufRdr.readLine()) != null && row < data.length) {
StringTokenizer st = new StringTokenizer(line, " ");
while (st.hasMoreTokens()) {
data[row][col] = Integer.parseInt(st.nextToken());
col++;
}
col = 0;
row++;
};

for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
int nextRow = i + 1;
int nextCol = j + 1;
if (data[i][j] == 1 && data[nextRow][j]!=1 && data[i][nextCol]!=1) {
System.out.println("(" + i + "," + j + ")");
}
}
};
};

};
Last edited by vitaliy1901; 10-25-2012 at 11:32 AM.

4. ## Re: Java Task with matrix.

A flood fill mechanism can do the job; i.e. if a point (x,y) is part of a domain, recursively check its neighbouring points. Check for array bounds when finding the neighbours. The stop criterium is easy: if (x,y) == 0, it is not part of the domain under construction, else if (x,y) == 1 and it is already part of the domain under construction, then stop, else add (x,y) to the new domain and recursively check its neighbours again.

kind regards,

Jos

5. Member
Join Date
Sep 2012
Posts
5
Rep Power
0

## Re: Java Task with matrix.

Hi Jos! Thanks for the help, It's work.
Java Code:
public class Matrix {

private static int quantity;

public static void main(String[] args) throws IOException {

for (int i = 0; i < matrix().length - 1; i++) {
for (int j = 0; j < matrix()[i].length - 1; j++) {

int nextRow = i + 1;
int nextCol = j + 1;
int nextNextRow = i + 2;
int nextNextCol = j + 2;
boolean three0111 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1;
boolean three1011 = matrix()[i][j] == 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1;
boolean three1101 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] == 1;
boolean three1110 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1;
boolean threeRight1110 = matrix()[i][j] == 1 && matrix()[i][nextCol] == 1 && matrix()[i][nextNextCol] == 1 && matrix()[nextRow][j] != 1;
boolean threeDown1110 = matrix()[i][j] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextNextRow][j] == 1 && matrix()[i][nextCol] != 1;

boolean one0100 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] != 1;
boolean one0110 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1;

boolean two0101 = matrix()[i][j] != 1 && matrix()[i][nextCol] == 1 && matrix()[nextRow][j] != 1 && matrix()[nextRow][nextCol] == 1;
boolean two1010 = matrix()[i][j] == 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] != 1;
boolean two0011 = matrix()[i][j] != 1 && matrix()[i][nextCol] != 1 && matrix()[nextRow][j] == 1 && matrix()[nextRow][nextCol] == 1;

if (three0111) {
System.out.print("(" + i + "," + j + ")");
System.out.print("(" + nextRow + "," + nextCol + ")");
System.out.print("(" + nextRow + "," + j + ")");
System.out.print(" || ");
quantity++;
} else if (three1011) {
System.out.print("(" + i + "," + j + ")");
System.out.print("(" + nextRow + "," + nextCol + ")");
System.out.print("(" + nextRow + "," + j + ")");
System.out.print(" || ");
quantity++;
} else if (three1101) {
System.out.print("(" + i + "," + j + ")");
System.out.print("(" + nextRow + "," + nextCol + ")");
System.out.print("(" + nextRow + "," + j + ")");
System.out.print(" || ");
quantity++;
} else if (three1110) {
System.out.print("(" + i + "," + j + ")");
System.out.print("(" + nextRow + "," + nextCol + ")");
System.out.print("(" + nextRow + "," + j + ")");
System.out.print(" || ");
quantity++;
} else if (threeRight1110) {
System.out.print("(" + i + "," + j + ")");
System.out.print("(" + i + "," + nextCol + ")");
System.out.print("(" + i + "," + nextNextCol + ")");
System.out.print(" || ");
quantity++;
} else if (threeDown1110) {
System.out.print("(" + i + "," + j + ")");
System.out.print("(" + nextRow + "," + j + ")");
System.out.print("(" + nextNextRow + "," + j + ")");
System.out.print(" || ");
quantity++;
} else if (one0100) {
System.out.print("(" + i + "," + nextCol + ")");
System.out.print(" || ");
quantity++;
}else if (one0110) {
System.out.print("(" + i + "," + nextCol + ")");
System.out.print(" || ");
quantity++;
} else if (two0101) {
System.out.print("(" + i + "," + nextCol + ")");
System.out.print("(" + nextRow + "," + nextCol + ")");
quantity++;
} else if (two1010) {
System.out.print("(" + i + "," + j + ")");
System.out.print("(" + nextRow + "," + j + ")");
quantity++;
}else if (two0011) {
System.out.print("(" + nextRow + "," + j + ")");
System.out.print("(" + nextRow + "," + nextCol + ")");
quantity++;
}
}
}

System.out.println("\nQuantity of domains is: " + quantity);
}

;

public static int[][] matrix() throws IOException {
File file = new File("src\\array.txt");
int row = 0;
int col = 0;
int[][] data = new int[6][6];
String line;

while ((line = bufRdr.readLine()) != null && row < data.length) {
StringTokenizer st = new StringTokenizer(line, " ");
while (st.hasMoreTokens()) {
data[row][col] = Integer.parseInt(st.nextToken());
col++;
}
col = 0;
row++;
}
;
return data;
}

};

6. ## Re: Java Task with matrix.

Originally Posted by vitaliy1901
Hi Jos! Thanks for the help, It's work.
I'm glad it works for you but I don't understand a thing of your code; I don't see any recursive calls so it definitely isn't a flood fill mechanism ...

kind regards,

Jos