algorithm - Interview Prep Words in a Grid -


i've been doing interview prep questions , question i've had trouble i'm unsure how implement solution. here's setup. you're given 8x8 grid of letters , list of words, , must return longest word in list can formed starting @ letter on grid , moving grid in way knight in chess. example, if had list ["word", "string", "test"] , following grid:

y w e z t n u w o p a c q g f t e l z x c v b n m m w f r t o u o n s d f b e j o l z v c t b n m q w e r t s g x z r s 

then return "test", because can formed starting @ bottom left corner of grid 't', jumping 2 , right 1 'e', jumping down 2 , right 1 's', , left 2 , 1 't', , none of other words can formed on grid.

i think you'd use branch , bound algorithm i'm totally lost on how set up. help? i'm trying implement in python.

note: letters can repeated in grid, i.e. can jump on same letter many times want.

my solution is: every word in array, iterate through matrix find first letter can use breath first search (bfs) or depth first search (dfs) on first letter's neighbors( in case 8 positions knight can jump to) see if match up. can implement bfs or dfs iteratively using queue or stack respectively.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -