Jump to content

So i have a 2d array that looks like this:

 

1111111111111111111111110
1000000000000000000000010
1001111100001111000011010
1000000001001111011011011
1011000010101111001100011
1001000000000000000110010
1001101111110001000011011
1001101000011001001000001
1000001101101101000000101
1011101100100100110101101
1001100110000100000100001
1011100011111101110110111
1000000000000001011000110
1111111111111100000111101
 
What i need to do is, read the 1s that are grouped together as a cell (in my program stMolekul). For whatever reason i cant seem to get it right, this is what ive got so far (my code so far only works for the 1s where theres only 1 surrounded by 0s.
 
class naloga {   public static void main(String[] args) {   Scanner sc = new Scanner(System.in);   int stVrstic = sc.nextInt(); //vrstice aka visina   int stStolpcev = sc.nextInt(); //stolpci aka sirina   int stMolekul = 0;     int [][] celota = new int [stVrstic + 1][stStolpcev + 1];   for (int i = 0; i < stVrstic; i++) {     char[] vrstica = sc.next().toCharArray();     for(int j = 0; j < stStolpcev; j++){      celota[i][j] = vrstica[j] - '0';     }  } for (int i = 0; i < stVrstic; i++) {  for(int j = 0; j < stStolpcev ; j++) {    if (celota[i][j] == 1) {      if (i == 0 && j == 0) {        if (celota[i+1][j] == 0 && celota[i][j+1] == 0) {          stMolekul++;        } else if (celota[i+1][j] != 1) {          i++;        } else if (celota[i][j+1] != 1) {          j++;       }      } else if (i == 0 && j > 0) {         if (celota[i+1][j] == 0 && celota[i][j+1] == 0 && celota[i][j-1] == 0) {             stMolekul++;         } else if (celota[i+1][j] != 1) {             i++;         } else if (celota[i][j+1] != 1) {             j++;         } else if (celota[i][j-1] != 1) {             j--;         }       } else if (j == 0 && i > 0) {        if (celota[i+1][j] == 0 && celota[i-1][j] == 0 && celota[i][j+1] == 0) {           stMolekul++;       } else if (celota[i+1][j] != 1) {           i++;       } else if (celota[i][j+1] != 1) {           j++;      } else if (celota[i-1][j] != 1) {           i--;      }     } else if (j > 0 && i > 0) {      if (celota[i+1][j] == 0 && celota[i-1][j] == 0 && celota[i][j+1] == 0 && celota[i][j-1] == 0) {         stMolekul++;      } else if (celota[i+1][j] != 1) {         i++;      } else if (celota[i][j+1] != 1) {         j++;      } else if (celota[i-1][j] != 1) {         i--;      } else if (celota[i][j-1] != 1) {         j--;    }     }   }  }}   System.out.print(stMolekul);}}
Link to comment
https://linustechtips.com/topic/274258-reading-cells-from-an-array/
Share on other sites

Link to post
Share on other sites

so, to make sure i got it right, you want to identify the groups of 1s whatever the shape they make, square, rectangular or irregular. is that right?

Yes, thats exactly right from any kind of an array any combination of 1s

 

int stVrstic represents the 'higth' of the array and stStolpcev represents the 'width', stMolekul would represent the number of groups of 1s. and at that point i get given an array, ive managed to get it to read that kind of an array with this 

import java.util.Scanner; class naloga {      public static void main(String[] args) {      Scanner sc = new Scanner(System.in);      int stVrstic = sc.nextInt();       int stStolpcev = sc.nextInt();       int stMolekul = 0;       int [][] celota = new int [stVrstic +2][stStolpcev +2];     for (int i = 0; i < stVrstic; i++) {        char[] vrstica = sc.next().toCharArray();          for(int j = 0; j < stStolpcev; j++){           celota[i][j] = vrstica[j] - '0';           }      } for (int i = 0; i < stVrstic; i++) {      for(int j = 0; j < stStolpcev ; j++) {           if (celota[i][j] == 1) {                 if (celota[i+1][j] == 1 && celota[i][j+1] == 0) {                       //do nothing                  } else if (celota[i][j+1] == 1 && celota[i+1][j] == 0) {                     //do nothing                 } else if (celota[i+1][j] == 0 && celota [i][j+1] == 0) {                     stMolekul++;          }         }    }  } System.out.print(stMolekul);}} 

But it still wont read an array like this;

 

0000000000000000000000001000000000010011

0011111100000010000000011100000000110011

0001100000000001110000111110000001110000

0000111000000111111000011100000000011111

0011110000011111100000001000000000000000

1111000000000000000000000111111111110000

0011111111111000000000000011111111111111

0000000000111110000000000000000111100000

1111111111000000000011111000011100011101

1111111111000000000011111000000111101101

 

or say this:

 

 

1111111111111111111111110

1000000000000000000000010

1001111100001111000011010

1000000001001111011011011

1011000010101111001100011

1001000000000000000110010

1001101111110001000011011

1001101000011001001000001

1000001101101101000000101

1011101100100100110101101

1001100110000100000100001

1011100011111101110110111

1000000000000001011000110

1111111111111100000111101

Link to post
Share on other sites

alright, i'm thinking looking for a group, a group would be two or more '1' consecutive, ither vertical or horizontal. create a function that would go like this

private void checkCell(int celota, int i, int j){//i and j would represent the position of the 1 you are checkingcelota[i][j]=0;if(celota[i-1][j]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i-1, j);}if(celota[i+1][j]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i+1, j);}if(celota[i][j-1]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i, j-1);}if(celota[i][j+1]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i, j+1);}}

this way you recursively check every position arround the 1. then you can store the position of every one on the cell in an arrayList for example

i am assuming the 1s in diagonal don't correspond to the same cell

sorry about the code, it seems i cannot tabulate code

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to post
Share on other sites

alright, i'm thinking looking for a group, a group would be two or more '1' consecutive, ither vertical or horizontal. create a function that would go like this

private void checkCell(int celota, int i, int j){//i and j would represent the position of the 1 you are checkingcelota[i][j]=0;if(celota[i-1][j]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i-1, j);}if(celota[i+1][j]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i+1, j);}if(celota[i][j-1]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i, j-1);}if(celota[i][j+1]==1){//add to the group//not implementedcelota[i][j];checkCell(celota, i, j+1);}}

this way you recursively check every position arround the 1. then you can store the position of every one on the cell in an arrayList for example

i am assuming the 1s in diagonal don't correspond to the same cell

sorry about the code, it seems i cannot tabulate code

Yeah you cant tabulate code so i tried with spaces thats why it look so bad in mine too

 

Ive got the vertical and horizontal covered, diagnoal ones dont correspond yes, if only done checks for down and right since if im not mistaken the 2 for loops should move the from left to right top to bottom, so technically you shouldnt need to check left and up since its always gonna catch it at the left point and at the top (correct me if im wrong) but the problem i get is when its doing random shapes, that are not square or anything like that

Link to post
Share on other sites

that's where my function comes in, once you find the first 1, you check the four adyacent positions and on overy check you call the function again so this solves the random shapes problem. assume this:

 

000000000000

000110000000

011100000000

000111000000

011100000000

000110000000

000000000000

 

when going through the array and you encounter the first 1 you call the function. this checks the fiend on the right, the field on the left, the field above and the field below. when it checks the field below it will check all four adyacent fields again, so it will check the 1 on the left and this 1 will check the 1 on it's left so it will cover any random shape

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×