In this paper, we propose a method of using an autonomous flying robot to explore an underground tunnel environment and build a 3D map. The robot model we use is an extension of a 2D non-holonomic robot. The measurements and sensors we considered in the presented method are simple and valid in practical unmanned aerial vehicle (UAV) engineering. The proposed safe exploration algorithm belongs to a class of probabilistic area search, and with a mathematical proof, the performance of the algorithm is analysed. Based on the algorithm, we also propose a sliding control law to apply the algorithm to a real quadcopter in experiments. In the presented experiment, we use a DJI Guidance sensing system and an Intel depth camera to complete the localization, obstacle detection and 3D environment information capture. Furthermore, the simulations show that the algorithm can be implemented in sloping tunnels and with multiple UAVs.