题解 | #拜访#

拜访

https://www.nowcoder.com/practice/491828fc7d93459db450b344c2aaaeef

class Solution {
public:
    map<int,int> cnt;
    int dx[4] = {0,0,1,-1};
    int dy[4] = {1,-1,0,0};
    int countPath(vector<vector<int> >& cityMap, int n, int m) 
    {
        vector<vector<bool>> vis(n,vector<bool>(m, false)); 
        function<void(int,int,int)> dfs = [&](int i, int j, int count)
        {
            for(int a = 0;a < 4;a++)
            {
                int x = dx[a]+i, y = dy[a]+j;
                if(x >= 0 && x < n && y >= 0 && y < m && cityMap[x][y] != -1 && !vis[x][y])
                {
                    if(cityMap[x][y] == 2)  // 到了
                    {
                        cnt[count]++;
                        break;
                    }
                    if(cnt.size() > 0 && count > cnt.begin()->first)
                        break;
                    else
                    {
                        vis[x][y] = true;
                        dfs(x,y,count+1);
                        vis[x][y] = false;
                    }
                }
            }
        };
        for(int i = 0;i < n;i++)
            for(int j = 0;j < m;j++)
                if(cityMap[i][j] == 1)
                    dfs(i, j, 0);
        return cnt.begin()->second;  // 最短路径的数量
    }
};

全部评论

相关推荐

darius_:我试了简历上有微服务和没微服务主动要简历的外包的比例都不一样,微服务稍微看看还是要写上去,人人都写你不写会被pass
点赞 评论 收藏
分享
敢逐云霄志:你打招呼语怎么能这么长,hr都没看下去的欲望,简明扼要说重点,就读于某某学校某某专业,26届应届毕业生,学信网可查,先后在某某公司实习过(如有),然后做过什么项目,想找一份什么样的工作,可实习几个月以上,期待您的回复。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务