What are elements of dramatic representation?

How can I get a 2 dimensional array elements to recognize like elements?

  • This is a programming assignment and I am a bit stuck. I have a 2 dimensional array; simply a 20x10 grid with characters A,B and C. I have to record the number of 'blobs' of each element in the grid, and the number in the largest blob. For example if there are 2 A's in a row next to one another and one directly underneath, it would be of size 3. I would then have to count how many individual blobs there are, and repeat for all 3 characters. I have the randomly generating grid, converted the grid to a 0's and 1's representation for each element (the grid for A has all A's as 1's, everything else as 0's. Ditto for B and C) but I just can not wrap my head around a way to efficiently have the elements recognize they are in a blob. The solution can not be that hard but with finals I am a bit burnt out. Any help would be appreciated, the solutions I keep trying I end up counting the same element multiple times, or missing chunks of the list. Here is the code I have so far that is working with my current attempt commented out; package assignment4; import java.util.Random; public class Main { public static void main(String[] args) { char[][] grid = new char[20][10]; int k=0; int count=0; int[][] gridcheck = new int [20][10]; Random rnd = new Random(); final int CHARS = 3; for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { char c = (char) (rnd.nextInt(CHARS) + 'A'); grid[j][i]=c; System.out.print(grid[j][i]); } System.out.println(); } for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { gridcheck[j][i]=0; } } for (char c1='A'; c1<='C';c1++){ System.out.println(c1); for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { if (grid[j][i]==c1){ gridcheck[j][i]=1; } else{ gridcheck[j][i]=0; } } } // for (int i = 0; i < 10; i++) { //this is where I am trying // for (int j = 0; j < 20; j++) { //to think of a way to count // if (gridcheck[j][i]==1){ //the touching elements // if (j+1 <20){ //but am stuck as to where to // k= j+1; //go next // } // } // } } for (int i = 0; i < 10; i++) { for (int j = 0; j < 20; j++) { if (gridcheck[j][i]==1){ System.out.print(1); } else System.out.print('-'); } System.out.println(); } } }

  • Answer:

    I have a guess in C++-style pseudocode int blobsize(x,y,blobtype) { // make sure coordinates are in range if(x < 0 || x > width || y < 0 || y > width) return 0; // make sure we're currently on a valid square if(grid[x][y] != blobtype) return 0; // shift it so there's no backtracking grid[x][y] += 3; // return the size of the blobs surrounding the current placement return 1+blobsize(x+1,y,blobtype)+blobsize(x,y+… } http://pastebin.com/f46e4f155 This makes sure the coordinates are valid and that the square requested is the type requested ('A', 'B', or 'C'). Then it adds 3 to the current index, because then it will appear as a different letter and no double-counting of the coordinate will occur. It then returns the size of the same-type blobs up, down, left, and to the right, and then adds 1 for the current placement. If these blobs overlap, double counting won't occur because of the +3. Run this for each coordinate (two embedded loops for rows and columns), and for everytime the blobsize function doesn't return 0, add one to number_of_blobs. Also, keep a variable for largest_blob, and just set it if a returned value is bigger int total = 0; int biggest = 0; for(char type = 'A',type<='C';++type) { for(int i=0;i<width;++i) { for(int j=0;j<height;++j) { int size = blobsize(i,j,type); if(size > 0) ++total; if(size > biggest) biggest = size; } } } http://pastebin.com/f7a1323c0 It might work, it might not. It probably requires some tweaking. This is C++-style, but the ideas are there.

groovcha... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

I think this is a natural application for a stack. Since it tracks elements in a two-dimensional array, the details are up to you; you could have separate arrays with one stack index for row and column numbers, or you could come up with a way mash the row and column into a single number. Consider the situation when, looking through the gridcheck array, we find a case where gidcheck[j][i] = 1; We need to do three things: (1) Schedule its neighbors to be looked at. (2) Count it! (3) Zero it so we don't count it again. (Once its neighbors are scheduled to be checked, we don't need it set to 1 any more.) Step (1) can be described in more detail. We want to add the following neighbors for examination: gridcheck[j+1][i] gridcheck[j-1][i] gridcheck[j][i+1] gridcheck[j][i-1] I recommend that before stacking you check whether adjacent row or column indices are valid: if they aren't, just don't stack that neighbor. (We already know that j and i are valid, and if we check before stacking, we'll also know all the elements we pop off the stack for examination are valid.) You could also check for a cell value of 1 before stacking, to keep the stack size down. But you have to check again when you pop a cell off the stack. There's every chance some cells will get stacked more than once, and zeroing the cell when you count it and stack its neighbors is the best way to avoid double-counting. Let's imagine this in operation. You're running your nice simple two-dimensional scan of the grid, and you find a cell set to 1. What happens? - You stack its neighbors if they have valid indices. - You zero and count the cell (which will always be the first cell of a blob, if you found it on the main scan). - You then keep unstacking cells, checking them for 1s, and repeating the stack-neighbors-zero-and-count operation until the stack is empty. When you find the stack is empty: (a) you have a correct count of that blob, and (b) all the cells in it have been cleared so they won't be mistakenly recounted. (A lot of them might have been cleared well before the last time they were popped off the stack, but since in that case you don't count them or stack the neighbors, you're sure you won't overcount and you'll eventually clear the stack.) You do need a safe stack size. Theoretically, each of the 200 cells could be stacked up to 4 times. You can try to figure out how many cells have to be unstacked and counted to get the rest stacked the maximum number, or you can just figure a stack capacity of 800 entries is safe. I'll leave the specifics of coding to you. Your basic approach, using simplified patterns in the gridcheck array, was well done, so I'm confident you can handle this.

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.