Click here to Skip to main content
15,899,679 members
Home / Discussions / C#
   

C#

 
GeneralRe: Submatrix problem Pin
maxflair27-Mar-09 23:16
maxflair27-Mar-09 23:16 
QuestionWPF listbox bug? Pin
Viplex27-Mar-09 14:11
Viplex27-Mar-09 14:11 
Answerpls ignore my question Pin
Viplex27-Mar-09 14:29
Viplex27-Mar-09 14:29 
QuestionCross Page Postback Pin
stormcandi27-Mar-09 11:31
stormcandi27-Mar-09 11:31 
QuestionSearching words in a matrix Pin
nike_arh27-Mar-09 10:42
nike_arh27-Mar-09 10:42 
AnswerRe: Searching words in a matrix Pin
Deresen27-Mar-09 12:02
Deresen27-Mar-09 12:02 
GeneralRe: Searching words in a matrix Pin
nike_arh27-Mar-09 12:32
nike_arh27-Mar-09 12:32 
AnswerRe: Searching words in a matrix Pin
Luc Pattyn27-Mar-09 21:21
sitebuilderLuc Pattyn27-Mar-09 21:21 
Hi,

I have several remarks:

1.
From every position you could move to 8 adjacent squars, however you only have 7 SeekWords calls; you skipped one, it will become even slower!

2.
I don't see the word length anywhere in your code; the code as it is will search for all the longest words; i.e. on a 5*5 board it will find several 25-character words, and a lot of shorter ones because "the head of the snake" gets caught in a dead-end. On a 6*6 board, it will find several 36-character words, and a lot of shorter ones, including all the ones found for a 5*5 board. The complexity of the job is more than exponential in z, where z is the size of the board.
Therefore, there is no decent general solution; it grows at an enormous pace.

3.
However, there are ways to improve, not so much by changing the algorithm (you have to investigate all possibilities, there is no escape), but rather by changing the implementation. The biggest factor I can see is the matrix. I suggest you smash it into a linear array, i.e. let Grid[0]...Grid[z-1] represent the first row, Grid[z]...Grid[2*z-1] be the second row, etc.

That means the current position (former x,y) becomes p, and a line such as SeekWords(word + Grid[x, y], visited, x - 1, y - 1); becomes SeekWords(word + Grid[p], visited, p-z-1);

which just is simpler, hence faster.

4.
visited[] could hold booleans instead of ints with values 0 and 1; I don't expect that to influence performance. Also visited[] could be a class member, making it unnecessary to pass it as a method parameter; doing this probably would slow things down, since class members are "farther away" than method parameters.

5.
if x equals 0 there is no need to try three SeekWords(x-1) since they will fail their initial test.
similar for y==0, x==z-1 and y==z-1
you could perform one test inside SeekWords as in
SeekWords(...) {
    if (...) {
        if (x>0) {
            SeekWords(... x-1 y-1)
            SeekWords(... x-1 y)
            SeekWords(... x-1 y+1)
        }
        if (x<z-1)>etc.

Of course, in a one-dimensional array it becomes slightly more complex.
One easy and fast way is to make 8 extra boards, one for each direction, initialize them properly and then just do eight times something like if (canStepInDirectionMM[p]) SeekWords( p-z-1)

canStepInDirectionMM[] would contain true everywhere except for those starting positions that don't allow a step in the direction (use M for minus, Z for none, P for plus in both X and Y direction).

Doing so, the complex if at the start of SeekWords reduces to if(!visited[p]) {...}

I expect the above to speed up things by say a factor of 3, so maybe it becomes acceptable for a 6*6 board, but I see no easy way to tackle larger boards unless there were an acceptance test on the words, say "an exact length of N letters", or "all letters appear in alphabetical order". If so, the nesting of SeekWords with be curtailed most of the time, leaving only very few (relatively speaking) combination to be investigated.

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


GeneralRe: Searching words in a matrix Pin
nike_arh27-Mar-09 23:27
nike_arh27-Mar-09 23:27 
AnswerRe: Searching words in a matrix Pin
nike_arh28-Mar-09 6:56
nike_arh28-Mar-09 6:56 
QuestionDragging and resizing blocks thread Pin
invader8227-Mar-09 9:40
invader8227-Mar-09 9:40 
AnswerRe: Dragging and resizing blocks thread Pin
Dave Kreskowiak27-Mar-09 12:08
mveDave Kreskowiak27-Mar-09 12:08 
GeneralRe: Dragging and resizing blocks thread Pin
invader8229-Mar-09 23:05
invader8229-Mar-09 23:05 
AnswerRe: Dragging and resizing blocks thread Pin
Alan N28-Mar-09 7:48
Alan N28-Mar-09 7:48 
GeneralRe: Dragging and resizing blocks thread Pin
invader8229-Mar-09 23:11
invader8229-Mar-09 23:11 
QuestionUserPrinicipal Enabled... Nullable type? Pin
Jacob Dixon27-Mar-09 9:18
Jacob Dixon27-Mar-09 9:18 
AnswerRe: UserPrinicipal Enabled... Nullable type? Pin
Big Daddy Farang27-Mar-09 12:05
Big Daddy Farang27-Mar-09 12:05 
GeneralRe: UserPrinicipal Enabled... Nullable type? Pin
Jacob Dixon27-Mar-09 12:08
Jacob Dixon27-Mar-09 12:08 
GeneralRe: UserPrinicipal Enabled... Nullable type? Pin
Big Daddy Farang27-Mar-09 12:48
Big Daddy Farang27-Mar-09 12:48 
QuestionRounding statement not working in C# program Pin
Vin Calitri27-Mar-09 8:56
Vin Calitri27-Mar-09 8:56 
AnswerRe: Rounding statement not working in C# program Pin
Alan N27-Mar-09 9:32
Alan N27-Mar-09 9:32 
AnswerRe: Rounding statement not working in C# program Pin
Ian Shlasko27-Mar-09 10:06
Ian Shlasko27-Mar-09 10:06 
AnswerRe: Rounding statement not working in C# program Pin
Luc Pattyn27-Mar-09 21:29
sitebuilderLuc Pattyn27-Mar-09 21:29 
GeneralRe: Rounding statement not working in C# program Pin
Vin Calitri31-Mar-09 2:33
Vin Calitri31-Mar-09 2:33 
Questionbuttons click event is fire accurately but radiobuttonlist controls SelectIndexChanged event is not fired help me !! Pin
jyoti_goel27-Mar-09 7:33
jyoti_goel27-Mar-09 7:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.