题目
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201224055316588.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3ODIxNzAx,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201224055337512.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM3ODIxNzAx,size_16,color_FFFFFF,t_70)
解法:BFS
就是个BFS变形。只是要注意,判断下一个位置需不需要入队,必须满足当前位置和下一个位置相互联通,需要双向。这跟普通BFS有区别
class Solution {
public:
bool hasValidPath(vector<vector<int>>& grid) {
queue<pair<int,int>> q;
q.push({
0,0});
int m = grid.size(), n = grid[0].size();
vector<pair<int,int>> dirs{
{
0,1},{
0,-