classSolution { public: boolfindSafeWalk(vector<vector<int>>& grid, int health){ int n = grid.size(), m = grid[0].size(); vector<vector<int>> cost(n,vector<int>(m)); queue<pair<int,int>> ZERO, ONE; auto push = [&](int y, int x, int c) { if(cost[y][x] < c) { if(grid[y][x]) ONE.push({y,x}); else ZERO.push({y,x}); cost[y][x] = c; } }; push(0,0,health - grid[0][0]); int dy[4]{-1,0,1,0}, dx[4]{0,1,0,-1}; while(ZERO.size() or ONE.size()) { while (ZERO.size()) { auto [y,x] = ZERO.front(); ZERO.pop(); for(int i = 0; i < 4; i++) { int ny = y + dy[i], nx = x + dx[i]; if(0 <= ny and ny < n and0 <= nx and nx < m) { push(ny,nx,cost[y][x] - grid[ny][nx]); } } } while(ONE.size() and !ZERO.size()) { auto [y,x] = ONE.front(); ONE.pop(); for(int i = 0; i < 4; i++) { int ny = y + dy[i], nx = x + dx[i]; if(0 <= ny and ny < n and0 <= nx and nx < m) { push(ny,nx,cost[y][x] - grid[ny][nx]); } } } } return cost[n-1][m-1] > 0; } };