1 Replies - 711 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

Stripe

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...

a
b b
a

changes to
a
bab
a

And the grid...

a ab

a ab

changes to...

aaab
aaab
aaab

------------------------------------------------------------------------------------------------------------------------
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);
            }
		}
			
		printGrid(grid);
		System.out.println();
		stripe(grid, row, col);
		
		System.out.println("Grid after fill:");
		printGrid(grid);
//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++)
				System.out.print(grid[i][j]);
			System.out.println();
		}
	}
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Stripe

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8034
  • View blog
  • Posts: 13,747
  • 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
b*bcd
eafgh

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