1 Replies - 888 Views - Last Post: 16 July 2011 - 08:45 PM Rate Topic: -----

#1 digifort  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-July 11


Posted 16 July 2011 - 07:36 PM

Create a class with the static method below and a main method that uses this method.

void stripe(char[][]grid)

Given a rectangular array 2-d array char grid[row][col], look at every char in the grid, doing row 0 from left to right, then row 1, and so on. For each char that is the alphabetic letter(as defined by Character.isLetter()), what is the nearest letter to its right? If the letter is the same as the char, then change all the intervening chars to be the same. So for example if we are looking at the left "a" in a row "a ; axyz", then we could change the row to "aaaa xyz". For each char, do this operation going out straight right/left/up/down, and then proceed to the next char. It's fine to use a brute-force strategy, searching out in the four directions for each char. For example, the grid...

b b

changes to

And the grid...

a ab

a ab

changes to...


Below is my current trashy code for I am really lost. Would greatly appreciate your help. Thanks a lot.
import java.util.*;

public class Stripee {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		  System.out.println("Enter desired number of rows: ");
          	int row = scan.nextInt();
          System.out.println("Enter desired number of columns: ");
          	int col = scan.nextInt();
          //declare arrays
          	char[][]grid = new char [row][col];
         System.out.println("\nInput your grid (0 for space): ");
		for(int i=0; i<row; i++) {  		//i loop is rows      
            for(int j=0; j<col; j++) {		//j loop is cols
            	String tempVar = scan.next();
				grid[i][j] = tempVar.charAt(0);
		stripe(grid, row, col);
		System.out.println("Grid after fill:");
//method for Stripe
	public static void stripe(char[][]grid, int row, int col){
		//Character filled up
		char letter = grid[row][col];
		// Fill
		grid[row][col] = 'a';
		// Search up
		if (row-2 >= 0 && grid[row-2][col] == letter)
			stripe(grid, row-2, col);
		// Search down
		if (row+2 < grid.length && grid[row+2][col] == letter)
			stripe(grid, row+2, col);
		// Search left
		if (col-2 >= 0 && grid[row][col-2] == letter)
			stripe(grid, row, col-2);
		// Search right
		if (col+2 < grid[row].length && grid[row][col+2] == letter)
			stripe(grid, row, col+2);
	//print grid
	public static void printGrid(char[][] grid) {
		for (int i=0; i<grid.length; i++) {
			for (int j=0; j<grid[i].length; j++)

Is This A Good Question/Topic? 0
  • +

Replies To: Stripe

#2 jon.kiparsky  Icon User is offline

  • Screw Trump (before he screws you)
  • member icon

Reputation: 10505
  • View blog
  • Posts: 17,843
  • Joined: 19-March 11

Re: Stripe

Posted 16 July 2011 - 08:45 PM

The thing I'd suggest is that you only need to repeat your search code once. Searching to the left is searching [row][col+1], that is, [row+0][col+1]. So if you make search a method, you can pass it four numbers: row, col, delta-row and delta-col. The latter two define the direction of your scan: -1, +1 would go right and down, assuming that 0,0 is the top left corner. In that search, you have to look at the next item (ie, current row+delta-row, current col+ delta-col) until you come to an alphabetical character, then you have to decide whether to fill in the intervening characters or move on to the next search.

So if I have

a a b

My first search will be starting at (0,0), in the direction (0,1). I'll look at the point (0,1), it's not an alpha char so I look at (0,2) - it's an alpha char, and it's the same as the one I started with, so I have to fill in. Then I repeat the process with starting point (0,0) and the direction (0, -1). I look at the point (0,-1) - oops, it's not on the board, never mind. Same for direction (-1,0). Then I look at the direction (1,0), and I find that there's an alphabetic character there, and it's not the one I started with, so I'm done with (0,0), and I repeat the process for the point (0,1), which is now the character 'a'. You see? Abstract that whole mess into one process, which you can repeat, and it's a lot easier. Don't write the same code over and over.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1