//Garth Bruen //import utils.SavitchIn; //import utils.OutFmt; class PP_1_3 { public static void main(String[] args) { int row_n = 4; int col_n = 5; //Array size int x = 2; int y = 0; //Starting point byte theNum; //The selected value for search byte dArray[][] = new byte[row_n][col_n];//Orig array byte d2Array[][] = new byte[row_n][col_n];//empty dump array /*Load array */ dArray[0][0] = 1;dArray[0][1] = 2;dArray[0][2] = 3;dArray[0][3] = 4;dArray[0][4] = 5; dArray[1][0] = 1;dArray[1][1] = 1;dArray[1][2] = 2;dArray[1][3] = 1;dArray[1][4] = 1; dArray[2][0] = 1;dArray[2][1] = 3;dArray[2][2] = 4;dArray[2][3] = 1;dArray[2][4] = 5; dArray[3][0] = 1;dArray[3][1] = 1;dArray[3][2] = 1;dArray[3][3] = 5;dArray[3][4] = 6; System.out.println("Original Array"); displayArray(dArray); //Show original array theNum = dArray[x][y]; //Set the search value based on the first cell d2Array[x][y] = theNum; //Set the value in the dump array dArray[x][y] = (byte)255; //Set the first cell to the fixed value checkAndLoadArray(x, y, dArray, d2Array); //Kicks off the process of searching /* Display the results */ System.out.println("Dump Array"); displayArray(d2Array); System.out.println("Original Array"); displayArray(dArray); }//end main /* This recursive method is set up to look for the same value in neighboring cells. Since we're not looking diagonally, were only concered with four positions: (x+1,y),(x-1,y),(x,y+1),(x,y-1). The method takes an initial position and looks in the four surrounding cells. If the same value is found, that position is passed back into the method as the new search parameter. Matching cells are "marked" with the (byte)255 value so they are not searched again. When no more matching values are found the method stops. */ public static void checkAndLoadArray(int x, int y, byte dArray[][], byte d2Array[][]){ int row_n = 4; int col_n = 5; //Array size byte theNum; theNum = d2Array[x][y]; //Get the value from the starting point /* The four nested if statements search the cells around the stating point */ /* Search "down" or south*/ if(x<(row_n-1)){ if(dArray[x+1][y]==theNum){ d2Array[x+1][y] = theNum; dArray[x+1][y] = (byte)255; checkAndLoadArray((x+1),y,dArray,d2Array); //If found, recursive call } } /* Search "up" or north. x!=0 makes sure we dont look beyond the edge */ if(x!=0){ if(dArray[x-1][y]==theNum){ d2Array[x-1][y] = theNum; dArray[x-1][y] = (byte)255; checkAndLoadArray((x-1),y,dArray,d2Array); //If found, recursive call } } /* Search "right" or east y<(col_n-1)) makes sure we dont look beyond the edge */ if(y<(col_n-1)){ if(dArray[x][y+1]==theNum){ d2Array[x][y+1] = theNum; dArray[x][y+1] = (byte)255; checkAndLoadArray(x,(y+1),dArray,d2Array); //If found, recursive call } } /* Search "left" or west if(y!=0) makes sure we dont look beyond the edge */ if(y!=0){ if(dArray[x][y-1]==theNum){ d2Array[x][y-1] = theNum; dArray[x][y-1] = (byte)255; checkAndLoadArray(x,(y-1),dArray,d2Array); //If found, recursive call } } }//end method /* This useful method simply prints out whatever array is passed to it */ public static void displayArray(byte newArray[][]){ int row_n = 4; int col_n = 5; System.out.println(" 0 1 2 3 4"); for(int i=0;i