(a) The building was demolished to avoid falling down accidentally: This is totally WRONG
When a sentence contains "to avoid (something happening)" , it must be clear WHO is trying to avoid something.
Usually it means that the SUBJECT has done something so that the SUBJECT can avoid something.
So it's fine to say "She removed the carpet to avoid tripping" : means that SHE removed the carpet so that SHE would not trip.
In A, we don't know who wants to avoid falling. In fact, nobody wants to avoid falling. Someone wants to PREVENT the building from falling.
The subject is the building, and the building has certainly not done anything and the building is not trying to avoid anything
Posted from my mobile device