alogrithim possible values java sudoku - java

Where am I going wrong? This doesn't seem to work. I'm trying to specify in the parameter an i and j which locates the cell. And returns the possible values in a 9 x 9 sudoku grid.
What this does it that the first array i.e a[0]= true if the cell is empty and false if its being played or occupied.
the rest of the array consists of values which are possible values that can be inserted into that i and j cell. therefore a possible value is 4 then it will return a[4]= true and the rest of the boolean array would be possible. can anyone tel me where I'm going wrong? Is the while loop incorrect?
public boolean[] getPossible( int i, int j)
{
final int booArraySize = 10;
boolean[] possibleArray = new boolean[booArraySize];
int zero = 0;
if ( game[i][j] == 0)
{
for( int b=1; b < possibleArray.length; b++)
{
possibleArray[b] = true;
}
int row=i;
int col= 0;
int[] copyy = new int[GRID_SIZE];
for( int m = 0; m < copyy.length; m++)
{
copyy[m] = 1;
}
while ( (copyy[0] < 10) && (copyy[0] >0))
{
for ( int q= col+1; q < game.length; q++)
{
if( copyy[0] == game[row][q])
{
possibleArray[q] = false;
}
else
{
possibleArray[q] = possibleArray[q];
}
}
copyy[0] = copyy[0] + 1;
}
possibleArray[0]= true;
}
return possibleArray;
}

As I understand it, you are trying to set possibleArray to be true at positions corresponding to allowed values. You seem to be scanning the game board and doing some testing in order to decide when to set each index position to true. I would suggest you reverse your logic: set possibleArray to true for all values and then scan the board for values that would rule out particular values. You can do that by using the value of the game board at each position relevant to (i, j) as an index to set possibleArray to false, as in this pseudocode:
// initialize possibleArray to true at all positions
for (each board position u,v along row i, column j, and square containing (i,j)) {
possibleArray[game[u][v]] = false;
}
possibleArray[0] = true; // do this after instead of before
For empty game positions, this will set possibleArray[0] to false, but you fix that after the loop exits.
EDIT:
Here's my attempt to deal with rows and columns simultaneously. It doesn't deal with the 3x3 box containing position (i,j); that's left as an exercise. :)
public boolean[] getPossible( int i, int j)
{
boolean[] possibleArray = new boolean[10];
if (game[i][j] == 0)
{
for( int b=1; b < 10; b++)
{
possibleArray[b] = true;
}
for (int n = 1; n < GRID_SIZE; ++n) {
possibleArray[game[i][n]] = possibleArray[game[n][j]] = false;
}
// TODO Scan the 3x3 square containing cell (i,j)
possibleArray[0]= true;
}
return possibleArray;
}
I assume (based on your code) that game[1][1] through game[GRID_SIZE][GRID_SIZE] are the right index range for the game board.

Related

How many steps does it take to visit every cell of a 2d array?

I have the following problem I can't figure out on my own.
There is a 2D array with 8x8 dimensions. A dog is randomly put within this dimension. The dog is not allowed to walk out of the dimension, if it does those steps are not counted. Every step the dog takes to another square is counted. Each simulation should calculate the number of overall steps it took for the dog to visit all squares of the 8x8 dimension at least once.
How do I know if all 64 squares got stepped at least once to stop the simulation and print the steps?
int [] [] onSquare = new int [8][8];
int counter = 0;
int x = rnd.nextInt(8); // random position for the dog 0-7
int y = rnd.nextInt(8);
for (int i = 0; i < onSquare.length; i++) {
for (int j = 0; j < onSquare.length; j++) {
onSquare [i] [j] = -1; // dog is on no square in beginning
}
while (onSquare[i][j] == -1) {
int dog = rnd.nextInt(4)+1; // random movement from dog
if (dog == 1) {
x++; // x,y show position of the dog
counter++; // number of steps
if (onSquare[i][j] == onSquare[x][y]) { <---- This gives me errors when checking if x,y lies within i,j
onSquare[i][j] = 1; // stepped on square
}
}
if (dog == 2) {
x--;
counter++;
onSquare[i][j] = 1;
}
if (dog == 3) {
y++;
counter++;
onSquare[i][j] = 1;
}
if (dog == 4) {
y--;
counter++;
onSquare[i][j] = 1;
}
if (x < 0 || y < 0 || x > onSquare.length || y > onSquare.length) {
counter++;
onSquare[i][j] = 0;
}
}
}
use a steps counter to keep track of movement, for each movement, increment its value.
instead of using boolean array, use int array for tracking cells traversed.
int [] [] onSquare = new int [8] [8];
initialize all the cell to have -1 , indicating no dog movement to the cell yet.
for(int i = 0 ; i < 8; i++ )
for(int j = 0; j < 8 ; j++ )
onSquare[i][j]=-1;
when dog enters it, assign its value to 1 , indicating presence of dog
when dog exits, assign its value to 0, indicating absence of dog
once all cell have non-negative value, stop the simulation and display the value of steps counter!!
Edit: since you are struggling to solve , here is the complete code:
/**
* This simulation assumes dog movement is discrete relative to grid cells
* i.e. its either in one of these cells at a time, overlapping two cells in not allowed!!
* **/
public class DogMovementSimulation
{
int onBoard[][] = null;
int dogPosX = 0;
int dogPosY = 0;
int dogPrevPosX = 0;
int dogPrevPosY = 0;
int directionOfMovement = 0;
int stepsCount = 0;
DogMovementSimulation()
{
onBoard = new int[8][8];
//initialize each position in onBoard to -1 ,implying dog has not been placed yet, not even once!!
for( int i = 0 ; i < 8 ; i++ )
{
for( int j = 0 ; j < 8 ; j++ )
{
onBoard[i][j] = -1;//implying dog has not been placed yet, not even once!!
}
}
//place dog in random cell
dogPosX = (int)Math.round(Math.random()*7);//generating random number between 0 and 7, since index is from 0 to 7 as there are 8 cell!!
dogPosY = (int)Math.round(Math.random()*7);
//assigning 1 to onBoard at index dogPosX,dogPosY to indicate dog has been placed there
onBoard[dogPosX][dogPosY] = 1;
}
/*this function returns false if any cell has -1,else true
* cause when all cells have been traversed , each cell have non negative value,either 0 or 1
* */
public boolean areAllCellsTraversed()
{
boolean result = true;
for( int i = 0 ; i < 8 ; i++ )
{
for( int j = 0 ; j < 8 ; j++ )
{
if( onBoard[i][j] == -1 )//implying this cell not traversed yet,i.e dog not placed in this cell yet!!
{
result = false;
}
}
}
return result;
}
public void simulate()
{
//loop while all cells have been not traversed
while( !areAllCellsTraversed() )
{
directionOfMovement = (int)Math.round(Math.random()*3);//generating random number between 0 and 3
switch( directionOfMovement )
{
case 0://move left-to-right
dogPosX += 1;
if( dogPosX >= 7 ) dogPosX = 0; //since largest array index is 1 less than its size, we compare with 7 instead of 8
break;
case 1://move right-to-left
dogPosX -= 1;
if( dogPosX <= 0 ) dogPosX = 7;
break;
case 2://move top-to-bottom
dogPosY += 1;
if( dogPosY >= 7 ) dogPosY = 0;
break;
case 3://move bottom-to-top
dogPosY -= 1;
if( dogPosY <= 0 ) dogPosY = 7;
break;
}
//assign 0 to previous position, meaning dog is no longer there
onBoard[dogPrevPosX][dogPrevPosY] = 0;
//assign 1 to new position , meaning dog is here
onBoard[dogPosX][dogPosY] = 1;
stepsCount++;
dogPrevPosX = dogPosX;
dogPrevPosY = dogPosY;
}
//once all cells have been traversed , print result!!
printSteps();
}
/*prints the total number of step taken to traverse all cells*/
public void printSteps()
{
System.out.println("Total steps taken by dog to traverse all cell = "+stepsCount);
}
public static void main(String[] args)
{
DogMovementSimulation dms = new DogMovementSimulation();
dms.simulate();
}
}
Going to write this as a series of suggestions, otherwise I'd feel like I'm basically writing an assignment for you.
It sounds like you should get rid of dimension, since it isn't doing anything useful.
You should store the value 8 in a variable.
int size = 8;
It sounds like you're trying to both check when all squares have been visited, but also check how many steps were taken, including revisiting old squares. For this you will require two counters, not one.
int steps = 0;
int visited = 0;
You need bounds checking to make sure x and y don't go out of bounds.
if (dog == 1 && x < size - 1) {
x += 1;
steps += 1;
You need to check whether the dog has visited this location before. If he hasn't been here before, then increment the counter.
if (!onSquare[x][y]) {
onSquare[x][y] = true;
visited += 1;
}
You need to stop when you've visited every location.
while (visited < size * size) {

How do i correct my DP solution for the Cherry Pickup problem

My approach seems to be correct , the issue is whether multiple trips are allowed.
My solution seems to exceed the correct answer.
The Question:
In a N x N grid representing a field of cherries, each cell is one of three possible integers.
0 means the cell is empty, so you can pass through;
1 means the cell contains a cherry, that you can pick up and pass through;
-1 means the cell contains a thorn that blocks your way.
Your task is to collect maximum number of cherries possible by following the rules below:
Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1);
After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;
When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);
If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.
My Solution:
class Solution {
public int cherryPickup(int[][] grid) {
int N = grid.length;
int[][] dp;
char[][] track;
boolean f = true;
int sum = 0;
int count = 0;
while(f)
{
dp = new int[N][N];
track = new char[N][N];
dp[0][0] = grid[0][0];
track[0][0] = 'a';
char c;
c='u';
for(int i=1;i<N;i++)
{
if(c=='r'||grid[i][0]==-1)
{
c='r';
dp[i][0]=-1;
}
else
dp[i][0] = dp[i-1][0] + grid[i][0];
track[i][0] = c;
}
c='s';
for(int j=1;j<N;j++)
{
if(c=='r'||grid[0][j]==-1)
{
c='r';
dp[0][j]=-1;
}
else
dp[0][j] = dp[0][j-1] + grid[0][j];
track[0][j] = c;
}
for(int i=1;i<N;i++)
for(int j=1;j<N;j++)
{
if(grid[i][j]==-1||(track[i-1][j]=='r' && track[i][j-1]=='r'))
{
track[i][j] = 'r';
dp[i][j] = -1;
}
else
{
if(dp[i-1][j]>=dp[i][j-1])
{
track[i][j] = 'u';
dp[i][j] = dp[i-1][j] + grid[i][j];
}
else
{
track[i][j] = 's';
dp[i][j] = dp[i][j-1] + grid[i][j];
}
}
}
if(dp[N-1][N-1]<=0)
break;
sum += dp[N-1][N-1];
int r = N-1 , cr = N-1;
while(r>0||cr>0)
{
grid[r][cr]=0;
if(track[r][cr]=='s')
cr--;
else
r--;
}
grid[0][0] = 0;
}
return sum;
}
}
Can someone help??

conway game of life neighbor issue(java)

I'm currrently having issues with the neighbors in my project where i have to modify two diffrent methods for a conway game of life. My TA says my code looks like it should work but the neightbor count isn't working. I'v been printing the neightbor code and it works for the first time than it just goes to 0 for the rest of the run. Anyone have an idea where I am messing up?
public static void updateLife(Boolean[][] gameCellAlive) {
int size = gameCellAlive.length;
System.out.println("size of temp--->"+size);
Boolean[][] tempCell = new Boolean [size][size];
int row = 0;
int col = 0;
for (row = 0; row<tempCell.length; row++) {
for(col=0; col<tempCell[0].length; col++) {
tempCell[row][col] = gameCellAlive[row][col];
}
}
for (int i = 0; i<tempCell.length; i++) {
for (int j = 0; j<tempCell[0].length; j++) {
int tempInt = getLifeNeighborCount(gameCellAlive, j, i);
System.out.println("neighbors---->"+tempInt);
if ((tempInt>3) || (tempInt<2)) {
tempCell[i][j] = false;
}
else if(tempInt == 3) {
tempCell[i][j] = true;
}
else if(tempInt==2) {
tempCell[i][j]=true;
}
/*else {
tempCell[row][col]=gameCellAlive[row][col];
}*/
}//2nd for loop
}//for loop
for (int x = 0; x<tempCell.length; x++) {
for(int y=0; y<tempCell[0].length; y++) {
gameCellAlive[x][y] = tempCell[x][y];
}
}
// METHOD STUB - This method needs to be implemented!
//if statemeent for requirements.
} // end method updateLife
/**
*
* #param gameBoard A 2D boolean array containing the current life status of
* each cell at each x,y coordinate on the board. true indicates that the
* cell is alive. false indicates no life in that cell.
* #param colIndex The x position of the cell in the game board whose
* neighbors are to be counted.
* #param rowIndex The y position of the cell in the game board whose
* neighbors are to be counted.
* #return the number of cells adjacent to the cell at the specified row and
* column that contain life. This value ranges between 0 (no adjacent cells
* contain life) and 8 (all adjacent cells contain life).
*
* CS1180 Note: YOU NEED TO IMPLEMENT THIS METHOD
*/
public static int getLifeNeighborCount(Boolean[][] gameBoard, int colIndex, int rowIndex) {
// METHOD STUB - THIS METHOD NEEDS TO BE IMPLEMENTED
int neighborCount = 0;
//check for alive or dead
for (int i = rowIndex-1; i<=rowIndex+1; i++) {
for (int j = colIndex-1; j<=rowIndex+1; j++) {
try {
if (gameBoard[i][j]==true && (i !=rowIndex || j!=colIndex)) {
//System.out.println("hello");
neighborCount++;
}//end if
}//end try
catch (ArrayIndexOutOfBoundsException e){
}//end catch
}//end second foor loop
}//end first foor loop
return neighborCount;
}// end method getLifeNeighborCount
You are using the wrong variable in the condition in this loop:
for (int j = colIndex-1; j<=rowIndex+1; j++) {
It should be:
for (int j = colIndex-1; j<=colIndex+1; j++) {

Strange Stack Overflow Error in Sudoko Backtracker

(Disclaimer: There are maybe 20 different versions of this question on SO, but a reading through most of them still hasn't solved my issue)
Hello all, (relatively) beginner programmer here. So I've been trying to build a Sudoku backtracker that will fill in an incomplete puzzle. It seems to works perfectly well even when 1-3 rows are completely empty (i.e. filled in with 0's), but when more boxes start emptying (specifically around the 7-8 column in the fourth row, where I stopped writing in numbers) I get a Stack Overflow Error. Here's the code:
import java.util.ArrayList;
import java.util.HashSet;
public class Sudoku
{
public static int[][] puzzle = new int[9][9];
public static int filledIn = 0;
public static ArrayList<Integer> blankBoxes = new ArrayList<Integer>();
public static int currentIndex = 0;
public static int runs = 0;
/**
* Main method.
*/
public static void main(String args[])
{
//Manual input of the numbers
int[] completedNumbers = {0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,3,4,
8,9,1,2,3,4,5,6,7,
3,4,5,6,7,8,9,1,2,
6,7,8,9,1,2,3,4,5,
9,1,2,3,4,5,6,7,8};
//Adds the numbers manually to the puzzle array
ArrayList<Integer> completeArray = new ArrayList<>();
for(Integer number : completedNumbers) {
completeArray.add(number);
}
int counter = 0;
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
puzzle[i][j] = completeArray.get(counter);
counter++;
}
}
//Adds all the blank boxes to an ArrayList.
//The index is stored as 10*i + j, which can be retrieved
// via modulo and integer division.
boolean containsEmpty = false;
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
if(puzzle[i][j] == 0) {
blankBoxes.add(10*i + j);
containsEmpty = true;
}
}
}
filler(blankBoxes.get(currentIndex));
}
/**
* A general method for testing whether an array contains a
* duplicate, via a (relatively inefficient) sort.
* #param testArray The int[] that is being tested for duplicates
* #return True if there are NO duplicate, false if there
* are ANY duplicates.
*/
public static boolean checkDupl(int[] testArray) {
for(int i = 0; i < 8; i++) {
int num = testArray[i];
for(int j = i + 1; j < 9; j++) {
if(num == testArray[j] && num != 0) {
return false;
}
}
}
return true;
}
/**
* If the puzzle is not full, the filler will be run. The filler is my attempt at a backtracker.
* It stores every (i,j) for which puzzle[i][j] == 0. It then adds 1 to it's value. If the value
* is already somewhere else, it adds another 1. If it is 9, and that's already there, it loops to
* 0, and the index beforehand is rechecked.
*/
public static void filler(int indexOfBlank) {
//If the current index is equal to the size of blankBoxes, meaning that we
//went through every index of blankBoxes, meaning the puzzle is full and correct.
runs++;
if(currentIndex == blankBoxes.size()) {
System.out.println("The puzzle is full!" + "\n");
for(int i = 0; i < 9; i++) {
System.out.println();
for(int j = 0; j < 9; j++) {
System.out.print(puzzle[i][j]);
}
}
System.out.println("\n" + "The filler method was run " + runs + " times");
return;
}
//Assuming the puzzle isn't full, find the row/column of the blankBoxes index.
int row = blankBoxes.get(currentIndex) / 10;
int column = blankBoxes.get(currentIndex) % 10;
//Adds one to the value of that box.
puzzle[row][column] = (puzzle[row][column] + 1);
//Just used as a breakpoint for a debugger.
if(row == 4 && column == 4){
int x = 0;
}
//If the value is 10, meaning it went through all the possible values:
if(puzzle[row][column] == 10) {
//Do filler() on the previous box
puzzle[row][column] = 0;
currentIndex--;
filler(currentIndex);
}
//If the number is 1-9, but there are duplicates:
else if(!(checkSingleRow(row) && checkSingleColumn(column) && checkSingleBox(row, column))) {
//Do filler() on the same box.
filler(currentIndex);
}
//If the number is 1-9, and there is no duplicate:
else {
currentIndex++;
filler(currentIndex);
}
}
/**
* Used to check if a single row has any duplicates or not. This is called by the
* filler method.
* #param row
* #return
*/
public static boolean checkSingleRow(int row) {
return checkDupl(puzzle[row]);
}
/**
* Used to check if a single column has any duplicates or not.
* filler method, as well as the checkColumns of the checker.
* #param column
* #return
*/
public static boolean checkSingleColumn(int column) {
int[] singleColumn = new int[9];
for(int i = 0; i < 9; i++) {
singleColumn[i] = puzzle[i][column];
}
return checkDupl(singleColumn);
}
public static boolean checkSingleBox(int row, int column) {
//Makes row and column be the first row and the first column of the box in which
//this specific cell appears. So, for example, the box at puzzle[3][7] will iterate
//through a box from rows 3-6 and columns 6-9 (exclusive).
row = (row / 3) * 3;
column = (column / 3) * 3;
//Iterates through the box
int[] newBox = new int[9];
int counter = 0;
for(int i = row; i < row + 3; i++) {
for(int j = row; j < row + 3; j++) {
newBox[counter] = puzzle[i][j];
counter++;
}
}
return checkDupl(newBox);
}
}
Why am I calling it a weird error? A few reasons:
The box that the error occurs on changes randomly (give or take a box).
The actual line of code that the error occurs on changes randomly (it seems to usually happen in the filler method, but that's probably just because that's the biggest one.
Different compilers have different errors in different boxes (probably related to 1)
What I assume is that I just wrote inefficient code, so though it's not an actual infinite recursion, it's bad enough to call a Stack Overflow Error. But if anyone that sees a glaring issue, I'd love to hear it. Thanks!
Your code is not backtracking. Backtracking implies return back on failure:
if(puzzle[row][column] == 10) {
puzzle[row][column] = 0;
currentIndex--;
filler(currentIndex);// but every fail you go deeper
}
There are must be something like:
public boolean backtrack(int currentIndex) {
if (NoBlankBoxes())
return true;
for (int i = 1; i <= 9; ++i) {
if (NoDuplicates()) {
puzzle[row][column] = i;
++currentIndex;
if (backtrack(currentIndex) == true) {
return true;
}
puzzle[row][column] = 0;
}
}
return false;
}

Recursive Searching in Java

So I've been writing a program for the game boggle. I create a little board for the user to use, but the problem is I don't know how to check if that word is on the board recursively. I want to be able to check if the word the entered is indeed on the board, and is valid. By valid I mean, the letters of the word must be adjacent to each other. For those who have played boggle you'll know what I mean. All I want to do is check if the word is on the board.
This is what I have so far ....
import java.io.*;
public class boggle {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
private String s = "";
private int [][] lettersNum = new int [5][5];
private char [][] letters = new char [5][5];
private char [] word = new char [45]; // Size at 45, because the longest word in the dictionary is only 45 letters long
private char [] temp;
public void generateNum()
{
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col++)
{
lettersNum [row][col] = (int) (Math.random() * 26 + 65);
}
}
}
public void numToChar()
{
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col++)
{
letters [row][col] = (char)(lettersNum[row][col]);
}
}
}
public void display()
{
for (int row = 0; row < 5; row ++)
{
for (int col = 0; col < 5; col++)
{
System.out.print(letters[row][col]);
}
System.out.println("");
}
}
public void getInput() throws IOException
{
System.out.println("Please enter a word : ");
s=br.readLine();
s=s.toUpperCase();
word = s.toCharArray();
}
public int search(int row, int col)
{
if((row <0) || (row >= 5) || (col < 0) || (col >= 5))
{
return (0);
}
else
{
temp = word;
return (1+ search(row +1, col) +
search(row -1, col) +
search(row, col + 1) +
search(row, col-1) +
search(row +1, col +1)+
search(row +1, col -1)+
search(row -1, col +1)+
search(row -1, col -1));
}
}
}
The search was my searching algorithm to check if the word is on the board but I don't know if it is correct or if it will work. Furthermore, I don't know how to actually tell the user that the word is valid !
Thanks for all the help :)
SO I tried to use what you suggested below but I dont really understand the int [5][5] thing. So this is what I tried, but I keep getting out of bounds errors ! Here is the soruce ...
public void locate()
{
temp = word[0];
for (int row = 0; row <5; row++)
{
for (int col = 0; col <5; col++)
{
if(temp == letters[row][col])
{
search(row,col);
}
}
}
}
public int search(int row, int col)
{
if(letters[row][col-1]==word[count]) // Checks the letter to the left
{
count++;
letters[row][col-1] = '-'; // Just to make sure the program doesn't go back on itself
return search(row, col-1);
}
else if (letters[row][col+1] == word[count])// Checks the letter to the right
{
count++;
letters[row][col+1] = '-';// Just to make sure the program doesn't go back on itself
return search(row, col +1);
}
else if (letters[row+1][col]== word[count])// Checks the letter below
{
count++;
letters[row+1][col] = '-';// Just to make sure the program doesn't go back on itself
return search(row +1 , col);
}
else if (letters[row-1][col]== word[count])// Checks the letter above
{
count++;
letters[row-1][col] = '-';// Just to make sure the program doesn't go back on itself
return search(row -1 , col);
}
else if (letters[row-1][col-1]== word[count])// Checks the letter to the top left
{
count++;
letters[row-1][col-1] = '-';// Just to make sure the program doesn't go back on itself
return search(row -1 , col-1);
}
else if (letters[row-1][col+1]== word[count])// Checks the letter to the top right
{
count++;
letters[row-1][col+1] = '-';// Just to make sure the program doesn't go back on itself
return search(row -1 , col+1);
}
else if (letters[row+1][col-1]== word[count])// Checks the letter to the bottom left
{
count++;
letters[row+1][col-1] = '-';// Just to make sure the program doesn't go back on itself
return search(row +1 , col-1);
}
else if (letters[row+1][col+1]== word[count])// Checks the letter to the bottom right
{
count++;
letters[row+1][col+1] = '-';// Just to make sure the program doesn't go back on itself
return search(row +1 , col+1);
}
return 0;
}
private int count = 0; (was declared at the top of the class, in case you were wondering where I got the word[count] from
Your current search function doesn't actually do anything. I'm assuming this is homework so, no free lunch ;)
The simplest approach would be to have two recursive functions:
public boolean findStart(String word, int x, int y)
This will do a linear search of the board looking for the first character in word. If your current location doesn't match, you call yourself with the next set of coords. When it finds a match, it calls your second recursive function using word, the current location, and a new, empty 4x4 matrix:
public boolean findWord(String word, int x, int y, int[][] visited)
This function first checks to see if the current location matches the first letter in word. If it does, it marks the current location in visited and loops through all the adjoining squares except ones marked in visited by calling itself with word.substring(1) and those coords. If you run out of letters in word, you've found it and return true. Note that if you're returning false, you need to remove the current location from visited.
You can do this with one function, but by splitting out the logic I personally think it becomes easier to manage in your head. The one downside is that it does do an extra comparison for each first letter in a word. To use a single function you would need to keep track of what "mode" you were in either with a boolean or a depth counter.
Edit: Your longest word should only be 16. Boggle uses a 4x4 board and a word can't use the same location twice. Not that this really matters, but it might for the assignment. Also note that I just did this in my head and don't know that I got it 100% right - comments appreciated.
Edit in response to comments:
Here's what your iterative locate would look like using the method I outline above:
public boolean locate(String word)
{
for (int row = 0; row < 4; row++)
{
for (int col =0; col < 4; col++)
{
if (word.charAt(0) == letters[row][col])
{
boolean found = findWord(word, row, col, new boolean[4][4]);
if (found)
return true;
}
}
}
return false;
}
The same thing recursively looks like the following, which should help:
public boolean findStart(String word, int x, int y)
{
boolean found = false;
if (word.charAt(0) == letters[x][y])
{
found = findWord(word, x, y, new boolean[4][4]);
}
if (found)
return true;
else
{
y++;
if (y > 3)
{
y = 0;
x++;
}
if (x > 3)
return false;
}
return findStart(word, x, y);
}
So here's findWord() and a helper method getAdjoining() to show you how this all works. Note that I changed the visited array to boolean just because it made sense:
public boolean findWord(String word, int x, int y, boolean[][] visited)
{
if (letters[x][y] == word.charAt(0))
{
if (word.length() == 1) // this is the last character in the word
return true;
else
{
visited[x][y] = true;
List<Point> adjoining = getAdjoining(x,y,visited);
for (Point p : adjoining)
{
boolean found = findWord(word.substring(1), p.x, p.y, visited);
if (found)
return true;
}
visited[x][y] = false;
}
}
return false;
}
public List<Point> getAdjoining(int x, int y, boolean[][] visited)
{
List<Point> adjoining = new LinkedList<Point>();
for (int x2 = x-1; x2 <= x+1; x2++)
{
for (int y2 = y-1; y2 <= y+1; y2++)
{
if (x2 < 0 || x2 > 3 ||
y2 < 0 || y2 > 3 ||
(x2 == x && y2 == y) ||
visited[x2][y2])
{
continue;
}
adjoining.add(new Point(x2,y2));
}
}
return adjoining;
}
So now, after you get input from the user as a String (word), you would just call:
boolean isOnBoard = findStart(word,0,0);
I did this in my head originally, then just went down that path to try and show you how it works. If I were to actually implement this I would do some things differently (mainly eliminating the double comparison of the first letter in the word, probably by combining the two into one method though you can do it by rearranging the logic in the current methods), but the above code does function and should help you better understand recursive searching.

Categories

Resources