leetcode 1036. 逃离大迷宫


 1 class Solution {
 2 public:
 3     typedef long long ll;
 4     const int lim=1e6;
 5     int dx[8]={-1,0,1,0},dy[8]={0,1,0,-1};
 6     bool isEscapePossible(vectorint>>& blocked, vector<int>& source, vector<int>& target) {
 7         int n=blocked.size();
 8         mapint>,int>mp;
 9         mapint>,int>f;
10         for(auto &p:blocked)mp[p]=1;
11         int mx=n*(n-1)/2;
12         int Aok=0;
13         function<int(int ,int,int&,int )>dfs=[&](int x,int y,int &cnt,int st)
14         {
15             if(cnt>mx)return 1;
16             if(st==1&&x==target[0]&&y==target[1])
17             {
18                 ok=1;
19                 return 1;
20             }
21             if(st==2&&x==source[0]&&y==source[1])
22             {
23                 ok=1;
24                 return 1;
25             }
26             for(int k=0;k<4;k++)
27             {
28                 int a=x+dx[k],b=y+dy[k];
29                 if(a>=0&&a=0&&b<lim)
30                 {
31                     if((f.find({a,b})==f.end())&&( mp.find({a,b})==mp.end() ))
32                     {
33                         f[{a,b}]=1;
34                         if(dfs(a,b,++cnt,st))return 1;
35                     }
36                 }
37             }
38             return 0;
39         };
40         int cnt1=0,cnt2=0;
41         f.clear();
42         int sok=dfs(source[0],source[1],cnt1,1);
43         int tok=dfs(target[0],target[1],cnt2,2);
44         return (sok&&tok)||ok;
45     }
46 };

相关