Click here to Skip to main content
15,888,461 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: How to check if a tree is AVL tree? Pin
ProtoBytes26-Jan-10 13:48
ProtoBytes26-Jan-10 13:48 
QuestionComplexity help please! Pin
password9996-Jan-10 7:25
password9996-Jan-10 7:25 
AnswerRe: Complexity help please! Pin
ProtoBytes27-Jan-10 7:24
ProtoBytes27-Jan-10 7:24 
QuestionRomanization of Japanese using the Hepburn (or related - like ISO_3602) algorithm Pin
Tom Clement30-Dec-09 14:40
professionalTom Clement30-Dec-09 14:40 
AnswerRe: Romanization of Japanese using the Hepburn (or related - like ISO_3602) algorithm Pin
ProtoBytes27-Jan-10 7:20
ProtoBytes27-Jan-10 7:20 
QuestionSnake game problem Pin
venomation28-Dec-09 11:03
venomation28-Dec-09 11:03 
AnswerRe: Snake game problem [modified] Pin
Luc Pattyn28-Dec-09 11:33
sitebuilderLuc Pattyn28-Dec-09 11:33 
AnswerRe: Snake game problem Pin
Skippums13-Jan-10 10:58
Skippums13-Jan-10 10:58 
I would actually store the snake's head index, tail index, and current direction. Then, store a 2D grid such that each cell can have one of three values: empty, snake, or new_segment, based on the contents of that cell. At each time step, move the head in the current direction by updating the head index, and test the value of that cell. If the value is "snake", there is a collision. If the value is "new_segment", draw the head, change the value to "snake", and continue to the next time step. Finally, if the value at the head index is "empty", erase the tail, search the four adjacent cells to find the one with a "snake" value (that isn't the head index), and set the tail index appropriately.

This approach has the following complexities:
Memory usage:           O(grid_size)
Moving the snake:       O(1) (assuming you use arrays with constant access time)
Redrawing the snake:    O(1) (you only have to update at most two cells)
Testing for collisions: O(1)
Hope this helps,

Sounds like somebody's got a case of the Mondays

-Jeff

GeneralRe: Snake game problem Pin
Luc Pattyn13-Jan-10 11:30
sitebuilderLuc Pattyn13-Jan-10 11:30 
GeneralRe: Snake game problem Pin
Skippums13-Jan-10 13:36
Skippums13-Jan-10 13:36 
GeneralRe: Snake game problem Pin
Luc Pattyn13-Jan-10 13:56
sitebuilderLuc Pattyn13-Jan-10 13:56 
GeneralRe: Snake game problem Pin
Skippums13-Jan-10 14:22
Skippums13-Jan-10 14:22 
GeneralRe: Snake game problem Pin
Luc Pattyn13-Jan-10 14:40
sitebuilderLuc Pattyn13-Jan-10 14:40 
AnswerRe: Snake game problem Pin
pewtas23-Feb-10 20:16
pewtas23-Feb-10 20:16 
GeneralRe: Snake game problem Pin
venomation4-Mar-10 1:25
venomation4-Mar-10 1:25 
QuestionGeometry problem Pin
Daniel.Sturza27-Dec-09 3:03
Daniel.Sturza27-Dec-09 3:03 
AnswerRe: Geometry problem Pin
Luc Pattyn27-Dec-09 3:10
sitebuilderLuc Pattyn27-Dec-09 3:10 
GeneralRe: Geometry problem [modified] Pin
Daniel.Sturza27-Dec-09 3:45
Daniel.Sturza27-Dec-09 3:45 
AnswerRe: Geometry problem Pin
harold aptroot27-Dec-09 4:33
harold aptroot27-Dec-09 4:33 
GeneralRe: Geometry problem Pin
Daniel.Sturza27-Dec-09 5:31
Daniel.Sturza27-Dec-09 5:31 
AnswerRe: Geometry problem Pin
Luc Pattyn27-Dec-09 5:28
sitebuilderLuc Pattyn27-Dec-09 5:28 
GeneralRe: Geometry problem Pin
Daniel.Sturza27-Dec-09 5:52
Daniel.Sturza27-Dec-09 5:52 
GeneralRe: Geometry problem Pin
Luc Pattyn27-Dec-09 6:10
sitebuilderLuc Pattyn27-Dec-09 6:10 
GeneralRe: Geometry problem Pin
Daniel.Sturza27-Dec-09 7:13
Daniel.Sturza27-Dec-09 7:13 
AnswerRe: Geometry problem Pin
ProtoBytes28-Dec-09 6:21
ProtoBytes28-Dec-09 6:21 

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.