Although map filtering-aided Pedestrian Dead Reckoning (PDR) is capable of largely improving indoor localization accuracy, it becomes less efficient when coping with highly complex indoor spaces. For instance, indoor spaces with a few close corners or neighboring passages can lead to particles entering erroneous passages, which can further cause the failure of subsequent tracking. To address this problem, we propose GridiLoc, a reliable and accurate pedestrian indoor localization method through the fusion of smartphone sensors and a grid model. The key novelty of GridiLoc is the utilization of a backtracking grid filter for improving localization accuracy and for handling dead ending issues. In order to reduce the time consumption of backtracking, a topological graph is introduced for representing candidate backtracking points, which are the expected locations at the starting time of the dead ending. Furthermore, when the dead ending is caused by the erroneous step length model of PDR, our solution can automatically calibrate the model by using the historical tracking data. Our experimental results show that GridiLoc achieves a higher localization accuracy and reliability compared with the commonly-used map filtering approach. Meanwhile, it maintains an acceptable computational complexity.