Advertisement
Barbie10

valid path

Feb 6th, 2023
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. int dx[8] = {1,1,1,0,0,-1,-1,-1};
  2. int dy[8] = {0,1,-1, 1,-1,0,1,-1};
  3.  
  4. string Solution::solve(int x, int y, int n, int r, vector<int> &X, vector<int> &Y) {
  5.    
  6.     vector< vector<bool>> mat(x+1, vector<bool>(y+1));
  7.    
  8.     for(int i = 0; i<=x; i++)
  9.     {
  10.         for(int j = 0; j<=y; j++)
  11.         {
  12.             bool flag = false;
  13.             for(int k = 0; k<X.size(); k++)
  14.             {
  15.                 if((X[k] - i)*(X[k] - i) + (Y[k] - j)*(Y[k] - j) <= r*r )
  16.                 {
  17.                 flag = true;
  18.                 break;
  19.                 }
  20.             }
  21.            
  22.              mat[i][j] = flag;
  23.         }
  24.     }
  25.    
  26.     queue<pair<int, int>> q;
  27.    
  28.     if(mat[0][0] = true) return "NO";
  29.     if(mat[x][y] = true) return "NO";
  30.    
  31.     q.push({0,0});
  32.     mat[0][0] = true;
  33.    
  34.     while(!q.empty())
  35.     {
  36.         pair<int, int> top = q.front();
  37.         q.pop();
  38.        
  39.         if(top.first == x && top.second == y) return "YES";
  40.        
  41.         for(int i = 0; i<8; i++)
  42.         {
  43.             int x1 = dx[i] + top.first;
  44.             int y1 = dy[i] + top.second;
  45.            
  46.             if(x1<=x && y1<=y && x1>=0 && y1>=0 && mat[x1][y1] == false) {
  47.                 q.push({x1, y1});
  48.                 mat[x1][y1] = true;
  49.             }
  50.         }
  51.        
  52.        
  53.     }
  54.    
  55.    
  56.     return "NO";
  57.        
  58.        
  59.    
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement