I second DavidT's words and go even further.
The Alter Rebbe in the Tanya says a person should not despair by having impure thoughts, on the contrary, he should rejoice! G-d gave this person a special task. One that even tzadikim can't accomplish (they don't have impure thoughts), and that is to divert the thought and thus subdue the klipot. That causes G-d an immense pleasure. He should also know that the impure thoughts come from his animal soul, not from his G-dly soul. So it's only natural that this person and 99% of the world population should have impure thoughts. Even gedolim have admitted they have hirhurim. The key is to realize you can banish them, and that's an enormous mitzva. When it comes, don't engage in it. Don't try to understand it. Just try to think about anything else, and occupy yourself with an activity. As a further geder, try not to be so alone. Take notes when and how do you fall. If you can avoid being alone, all the better.