The terrain-following algorithm for low-altitude flight has been widely applied in various fields to minimize detection by hostile air defense networks during aircraft missions. In this paper, a new non-convex cost function is proposed to maintain a safe altitude and minimize overshoot to avoid collisions between the aircraft and terrain, while obtaining the optimal control solution. The optimization problem can be solved using parallel computing, and a model predictive path integral control technique is used to generate terrain-following guidance commands. The performance of the guidance commands is verified through 3-degree-of-freedom simulations, and validated through 6-degree-of-freedom simulations that consider both aircraft dynamics and control systems.