Dynamic switching between escape and avoidance regimes reduces Caenorhabditis elegans exposure to noxious heat