The first source for such discussions is the Talmud Bava Basra 57b.
ועוצם עיניו מראות ברע א"ר חייא בר אבא זה שאין מסתכל בנשים בשעה שעומדות על הכביסה היכי דמי אי דאיכא דרכא אחריתא רשע הוא אי דליכא דרכא אחריתא אנוס הוא לעולם דליכא דרכא אחריתא ואפ"ה מיבעי ליה למינס נפשיה
" ... and closes his eyes from seeing evil" (Yishayahu 33:15) Rabbi Chiya Bar Abba said "This is one who doesn't gaze [When he is walking at the river's edge - Rashbam] at women when they are standing at the laundry." What is the case? [The implication is that if he fails to close his eyes, he is neither righteous nor wicked - Rashbam]. If he had another way to walk [to avoid seeing it] - he is wicked [even if he closed his eyes - Rashbam], if he had no other way to walk, he is an Oneis. Actually it is a case where there is no other way to walk, nevertheless he should turn himself away [look in a different direction, then he is pious - Rashbam].
So the implication is it is completely forbidden to get into a situation where a person could see immodest things if there are alternatives, but otherwise one does not need to be trapped.
Rav Moshe Feinstein (Shut Even Haeizer Vol. 1 #56) says that the issue here is that whether or not one actually looks, the knowledge of what is there itself creates bad thoughts, therefore there is no real distinction between looking and not looking in that situation and then goes on to suggest reasons why still keeping ones eyes averted is pious behavior.
As a practical matter, it will depend on what options you have and what would rise to the equivalent of "having another route". This in regards to immodest things. Beir Mayim Chaim - Hilchos Loshon Hara Rule 6 says that the Talmud here is specifically speaking about matters of immodesty which a person naturally has an outsized desire for. In other matters, taking another route would not be necessary.