Skip to content

Solution for CS Games Team A! #22

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

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

Conversation

kellyowenya
Copy link

CS Games 2023 Team A Application

PLEASE FILL IN THE FOLLOWING!

Full Name

Kelly Owenya

UWindsor Email

[email protected]

Application Form

Briefly explain how your solution works and how to run it

How it works:

My solution uses recursion, a stack, maps, and optimization to get an answer.

First, information about the dimensions of the given board is stored, and a history list is initialized to keep track of what cells have already been used.

Then, in order to avoid losing time recursively looking through inputs that won't work, I've included a way to catch those tedious inputs and immediately return False.

First, I populate two hashmaps: one with the actual word, and one with the characters in the board. They map a character to its frequency.

Then, I iterate through the dictionary of the word's frequencies, and if the word requires more of the same letter than the board has of that letter, it will be impossible for the word to exist within the board, so it can just return False immediately. Even in the case that a word has a new character that the board doesn't have at all, which normally raises a KeyError, it should still return False.

The last optimization is if the frequency of the first letter in the word is higher in the board than the last letter of the word, I reverse the word because the way my algorithm works, it's faster for a letter with less frequency to be the first one checked.

Now, it's time to explain the way my search algorithm works.

The first if statement is if we’ve just simply arrived at the last character of the word. Then, we can return True, because we’ve already validated that every other character of the word can be formed from the board recursively. (This is part of why the last optimization saves time.)

Otherwise, if the letter is out of bounds, not possible to be found, or has already been visited and used, it should return False. These are all things that would make a word impossible.

If those things aren’t encountered, we begin the recursive part of the function, because we’ve found a viable place for the desired letter and want to see if the next one can be found.

The coordinates of the letter are pushed onto the stack of the history (so it can’t be used again), and then the search algorithm is recursively called on every possible adjacent square to see if on at least one, the next letter of the word can be found. After that, the letter coordinates are simply popped off the stack, because we won’t be needing them anymore. Then, the result of the search is returned.

Search is called on every square of the board until one square reveals a path that leads to everything being found, which means the word exists in a valid way within the board. (This is the other part of why reversing the word can save time, because the algorithm will spend time exploring less paths.) Otherwise, if no viable paths are found, it returns False.

I also included an input parser which takes the format specified by the question and makes sure the program can interpret it (because it requires a 2D list and a string, and not just a multi-line string). You can add testcases either way as the given testcases show, just make sure you call *input_parser() if you use the way specified by the question!

How to run it:

Make sure your computer has Python 3 installed, and you can run it on any IDE of your choice!

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