Untitled

                Never    
C++
       
std::vector<std::vector<int>> PlayScene::CalculateBFSDistance() {
	// Reverse BFS to find path.
	std::vector<std::vector<int>> map(MapHeight, std::vector<int>(std::vector<int>(MapWidth, -1)));
	std::queue<Engine::Point> que;
	// Push end point.
	// BFS from end point.
	if (mapState[MapHeight - 1][MapWidth - 1] != TILE_DIRT)
		return map;
	que.push(Engine::Point(MapWidth - 1, MapHeight - 1));

	map[MapHeight - 1][MapWidth - 1] = 0;
	while (!que.empty()) {
		Engine::Point p = que.front();
		que.pop();
		// TODO: [BFS PathFinding] (1/1): Implement a BFS starting from the most right-bottom block in the map.
		//               For each step you should assign the corresponding distance to the most right-bottom block.
		//               mapState[y][x] is TILE_DIRT if it is empty.

		if(p.x<MapWidth-1&&mapState[p.y][p.x+1]&&map[p.y][p.x+1]==0) {
			que.push(Engine::Point(p.x+1,p.y));
			map[p.y][p.x+1]=map[p.y][p.x]+1;
		}
		if(p.x>0&&mapState[p.y][p.x-1]&&map[p.y][p.x-1]==0) {
			que.push(Engine::Point(p.x-1,p.y));
			map[p.y][p.x-1]=map[p.y][p.x]+1;
		}
		if(p.y<MapHeight-1&&mapState[p.y+1][p.x]&&map[p.y+1][p.x]==0) {
			que.push(Engine::Point(p.x,p.y+1));
			map[p.y+1][p.x]=map[p.y][p.x]+1;
		}
		if(p.y>0&&mapState[p.y-1][p.x]&&map[p.y-1][p.x]==0) {
			que.push(Engine::Point(p.x,p.y-1));
			map[p.y-1][p.x]=map[p.y][p.x]+1;
		}

	}
	return map;
}

Raw Text