Robust preparation of Greenberger-Horne-Zeilinger and W states of three distant atoms