Criação de Algoritmo com Matrizes

Java

10/05/2017

Alguém consegue me auxiliar com um trabalho, estou com um pouco de dificuldade de terminar ele...


Ler um .txt

6 5
    000000
    011000
    011010
    000010
    000010

que contém uma matriz de 6 linhas por 5 colunas, que pode ser armazenada na forma:

     | 0 | 0 | 0 | 0 | 0 | 0 |
     | 0 | 1 | 1 | 0 | 0 | 0 |
     | 0 | 1 | 1 | 0 | 1 | 0 |
     | 0 | 0 | 0 | 0 | 1 | 0 |
     | 0 | 0 | 0 | 0 | 1 | 0 |

Os grupos de 1 que formam ilhas devem estar conectados apenas na horizonta ou vertical, e não na diagonal. Por exemplo a seguinte matriz possui apenas uma ilha:

O resultado esperado é:

   2


abaixo o codigo que iniciei:

class Matriz

import java.awt.List;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Matriz {
private int[][] matrixFinal;
private int colum;
private int line;

public void readDocument() throws FileNotFoundException, IOException {

//READ .TXT
try (BufferedReader br = new BufferedReader(new FileReader("/Users/arthur/Documents/workspace/TRAB_ALGIII/Matriz/example_0.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
boolean firsTime = false;
int count = 0;
this.createTheMatrixFromADocument(line);

while (line != null) {

if (firsTime) {
this.populateTheMatrix(line, count);
if (count < this.colum) {
count++;

}
}
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();

firsTime = true;
}

System.out.println("-----------------------------------------");
this.printMatrix(this.matrixFinal);

}
}
//PRINT MATRIX
public void printMatrix(int[][] matrix) {
Arrays.stream(matrix).forEach((row) -> {
System.out.print("|");
Arrays.stream(row).forEach((el) -> System.out.print(" " + el + " "));
System.out.println("|");

});

}

//CONVERTER
private int getValueFromAChar(char[] index, int position) {
int result = 0;
result = Character.getNumericValue(index[position]);
return result;
}

//CREATE MATRIX
private void createTheMatrixFromADocument(String line) {
String[] meh = line.split(" ");
int colum = Integer.parseInt(meh[0]);
int lines = Integer.parseInt(meh[1]);
this.line = lines;
this.colum = colum;

this.matrixFinal = this.instantiateTheMatrix(colum, lines);
}

//EXTEND
private int[][] instantiateTheMatrix(int line, int colum) {
int[][] result = new int[colum][line];
return result;
}

//POPULATE
private void populateTheMatrix(String line, int count) {
char[] index = line.toCharArray();
int[] intArray = new int[index.length];

for (int i = 0; i < index.length; i++) {
intArray[i] = getValueFromAChar(index, i);

}
for (int i = 0; i < this.colum; i++) {
this.matrixFinal[count][i] = intArray[i];

}
}
//RESULT MATRIX
public void ResultMatrix(int[][] matrix) {
Arrays.stream(matrix).forEach((row) -> {
System.out.print("|");
Arrays.stream(row).forEach((el) -> System.out.print(" " + el + " "));
System.out.println("|");

});
}



public int[][] getMatrixFinal() {
return matrixFinal;
}

public void setMatrixFinal(int[][] matrixFinal) {
this.matrixFinal = matrixFinal;
}

public int getColum() {
return colum;
}

public void setColum(int colum) {
this.colum = colum;
}

public int getLine() {
return line;
}

public void setLine(int line) {
this.line = line;
}

}

class Main

import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {

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

Matriz M = new Matriz();
M.readDocument();;
}

}
Arthur

Arthur

Curtidas 0
POSTAR