Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

solution #6

wants to merge 1 commit into from

Conversation

Nafiz1
Copy link

@Nafiz1 Nafiz1 commented Nov 27, 2022

CS Games 2023 Team A Application

PLEASE FILL IN THE FOLLOWING!

Full Name

Nafiz Hasan

UWindsor Email

[email protected]

Application Form

Briefly explain how your solution works and how to run it

My solution...
Works by implementing a recursive backtracking algorithm. We first have a nested for loop that will go through all elements in the board, and find the first letter of the target word. Then call the backtracking algorithm called helper to see if it leads us to the full word. In helper we will pass 5 arguments, the index to keep track of where we are in the word, i and j to keep track of where we are in the board, and the board and word. Helper has 3 base cases. First we check if i and j are out of bounds of the board, if so we return false. We also check if the current element in the board does not equal the current letter in the word. If so, it is the wrong letter or already visited and is null, so we return false. Finally, we check if the index is equal to the length of the word, if so we have found the whole word and so we return true. Otherwise we continue with the function. We put the current letter in a variable c to use later to backtrack. We then set the current element to 0 to indicate it has been visited. Then we recursively call helper for all 4 directions, changing i and j accordingly, and check if the next letter has been found. If so, we return true. If not, we backtrack and set the current element back to c, to indicate it has not been visited, then return false. If the word isn't found our function will return false.

To parse the input we will make an arraylist to hold char arrays. We then take in the input line by line and check if the input when spitted will result in a length other than one. Assuming only correct 2D arrays and words are entered, this will mean the line is for the board and so we add it to the arraylist, removing the spaces and making it into a char array. Otherwise, it will be the word and we stop taking in inputs. We convert the arraylist to a 2D array to make it easier to work with in the function.

The running time of getting the input would be O(m*n) where m is the number of lines and n is the letters per line. This is because we run the while loop for as many lines there are, and we split, replace, and convert for as many letters there are. The running time of canMakeWord would be O(m*n*4^s), where m is the number of lines and n is the number of letters and s is the length of the string. This is because we have a nested for loop which runs at m*n, and the recursive function which calls itself 4 times for each character, s, making it 4^s. The program will have a runtime of O(m*n*4^s).

To run it, enter the letters separated by spaces and new lines as per the input instructions, and then enter the word. The output will be printed to the console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant