classSolution { public: boolfindSafeWalk(vector<vector<int>>& grid, int health){ int n = static_cast<int>(grid.size()); int m = static_cast<int>(grid[0].size());
while (!q.empty()) { auto [x, y, h] = q.front(); q.pop(); if (h <= 0) { continue; } if (x == n - 1and y == m - 1) { returntrue; }
for (int i = 0; i < 4; i += 1) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0or nx >= n or ny < 0or ny >= m or h - grid[nx][ny] <= 0or visited[nx][ny][h - grid[nx][ny]]) { continue; } q.push({nx, ny, h - grid[nx][ny]}); visited[nx][ny][h - grid[nx][ny]] = true; } }
classSolution { public: intmaxPathLength(vector<vector<int>>& coordinates, int k){ int n = static_cast<int>(coordinates.size()); std::vector<int> order(n); std::iota(order.begin(), order.end(), 0); std::sort(order.begin(), order.end(), [&](int i, int j) { if (coordinates[i][0] == coordinates[j][0]) { return coordinates[i][1] > coordinates[j][1]; } return coordinates[i][0] < coordinates[j][0]; });
std::vector<int> u; for (int i = 0; i < n; i += 1) { if ((coordinates[order[i]][0] < coordinates[k][0] and coordinates[order[i]][1] < coordinates[k][1]) or (coordinates[order[i]][0] > coordinates[k][0] and coordinates[order[i]][1] > coordinates[k][1])) { auto it = std::lower_bound(u.begin(), u.end(), coordinates[order[i]][1]); if (it == u.end()) { u.push_back(coordinates[order[i]][1]); } else { *it = coordinates[order[i]][1]; } } }