classSolution{public:intgetFood(vector<vector<char>>& grid){int m = grid.size(), n = grid[0].size();vector<vector<int>> dir ={{1,0},{0,1},{-1,0},{0,-1}};queue<int> q;bool found =false;for(int i =0; i < m;++i){for(int j =0; j < n;++j)if(grid[i][j]=='*'){q.push(i*256+j);grid[i][j]='X';// 訪問過了found =true;break;}if(found)break;}int step =0;while(!q.empty()){int size = q.size();while(size--){int x = q.front()/256;int y = q.front()%256;if(grid[x][y]=='#')//食物return step;q.pop();for(int d =0; d <4;++d){int nx = x + dir[d][0];int ny = y + dir[d][1];if(nx>=0&& nx<m && ny>=0&& ny<n && grid[nx][ny]!='X'){q.push(nx*256+ny);if(grid[nx][ny]!='#')grid[nx][ny]='X';// 標(biāo)記為訪問過了}}}step++;}return-1;}};