Jump to content

Need help with searching algorithm

Muffin123

The problem is then that my function is faulty, it recognises some parts as a new part, but others just get painted over with another color

 

What does you pseudo code look like?

Link to comment
Share on other sites

Link to post
Share on other sites

What does you pseudo code look like?

if (tabela[i][j]==zamenjat) return 0;    if (tabela[i][j]==trenutni) {        tabela[i][j]=zamenjat;        if (tabela[i][j+1]==1) {            return 7;        } else {            flood(i+1,j,trenutni,zamenjat,visina,sirina,tabela);            flood(i-1,j,trenutni,zamenjat,visina,sirina,tabela);            flood(i,j+1,trenutni,zamenjat,visina,sirina,tabela);            flood(i,j-1,trenutni,zamenjat,visina,sirina,tabela);                    }    }    return 0;

This is what it looks like, first it checks if its the same, if its not it checks if its the one im looking for and not 1, then the if statement which i use to change the area, and then color in all directions, but what happens is, it apparently never reaches a 1, and just colors everything in the same color

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

Make it more pseudo, i.e. human readable. Verbalize each step of the algorithm.

Link to comment
Share on other sites

Link to post
Share on other sites

I found this to be an interesting problem so I made javascript implementation http://jsfiddle.net/g0odg633/18/ well two one fast and one visual that shows how the algorithm works...

 

You only need 1 and 0 to count all areas. You need one main loop that goes through all the items and as soon as that one finds a 0 it enters the colouring phase. The colouring phase changes all 0:s it finds to 1:s and when it cant find more 0 it exits back out to the main loop.

Link to comment
Share on other sites

Link to post
Share on other sites

I found this to be an interesting problem so I made javascript implementation http://jsfiddle.net/g0odg633/18/ well two one fast and one visual that shows how the algorithm works...

 

You only need 1 and 0 to count all areas. You need one main loop that goes through all the items and as soon as that one finds a 0 it enters the colouring phase. The colouring phase changes all 0:s it finds to 1:s and when it cant find more 0 it exits back out to the main loop.

 

Ultra cool.

Link to comment
Share on other sites

Link to post
Share on other sites

I found this to be an interesting problem so I made javascript implementation http://jsfiddle.net/g0odg633/18/ well two one fast and one visual that shows how the algorithm works...

 

You only need 1 and 0 to count all areas. You need one main loop that goes through all the items and as soon as that one finds a 0 it enters the colouring phase. The colouring phase changes all 0:s it finds to 1:s and when it cant find more 0 it exits back out to the main loop.

That is amazing :o, thank you

Link to comment
Share on other sites

Link to post
Share on other sites

You can try following the boundaries of a series of 1's.

 

I like @TAtari's approach.

 

You may also be able to try using some kind of bitwise comparisons (i.e., convert your 2D array into a 1D array where each column has been reduced into a single number, e.g., {1,0,1,0,0} -> 10100 ->20) but that might be overkill, and you're limited to 32 or 64 max lengths if you do not use some object which can handle larger numbers (BigInteger in Java). I would probably go for this approach because I find it interesting and challenging. With this approach, you would perform SLL's to ease comparisons for each (i, j), i.e., each SLL would represent a new col j for row i. If you find a 20, then you would check the rows underneath for another 20 (you would need to mask out irrelevant bits), and then if another matching row is found, check the edge columns if they are 0xFFFF.... for the length of the column. This approach is definitely not the the faint of heart.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×