Given a 2-D binary matrix of size n*m, where 0 represents an empty space while 1 represents a wall you cannot walk through. You are also given an integer k. You can walk up, down, left, or right. Given that you can remove up to k walls, return the minimum number of steps to walk from the top left corner (0, 0) to the bottom right corner (n-1, m-1).
Note: If there is no way to walk from the top left corner to the bottom right corner, return -1.
public: intshotestPath(vector<vector<int>> mat, int n, int m, int k){ if(n == 1and m == 1) return0; memset(vis, false, sizeof vis); queue<array<int,3>> q; int dy[4]{-1,0,1,0}, dx[4]{0,1,0,-1}; q.push({0,0,k}); vis[0][0][k] = true; int res = 1; while(!q.empty()) { int sz = q.size(); while(sz--) { auto A = q.front(); q.pop(); auto y = A[0], x = A[1], b = A[2]; 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) { if(mat[ny][nx] == 0and !vis[ny][nx][b]) { if(ny == n - 1and nx == m - 1) return res; vis[ny][nx][b] = true; q.push({ny,nx,b}); } elseif(mat[ny][nx] == 1and b and !vis[ny][nx][b-1]) { if(ny == n - 1and nx == m - 1) return res; vis[ny][nx][b-1] = true; q.push({ny,nx,b-1}); } } } } res++; }