Advertisement
T-D-K

Untitled

Nov 7th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1.         static long CoordToHash(int r, int c) {
  2.             return r + ((long) c << 32);
  3.         }
  4.  
  5.         static int WalkPath(int rq, int cq, int dr, int dc, int n, HashSet<long> obstacles) {
  6.             int result = 0;
  7.             rq += dr;
  8.             cq += dc;
  9.             while (rq != 0 && cq != 0 && rq != n + 1 && cq != n + 1) {
  10.                 if (obstacles.Contains(CoordToHash(rq, cq)))
  11.                     break;
  12.                 rq += dr;
  13.                 cq += dc;
  14.                 result++;
  15.             }
  16.             return result;
  17.         }
  18.  
  19.         static int queensAttack(int n, int k, int rQ, int cQ, int[][] obstacles) {
  20.             HashSet<long> obstaclesSet = new HashSet<long>();
  21.             foreach (int[] obstacle in obstacles) {
  22.                 obstaclesSet.Add(CoordToHash(obstacle[0], obstacle[1]));
  23.             }
  24.             return WalkPath(rQ, cQ, -1, -1, n, obstaclesSet) +
  25.                    WalkPath(rQ, cQ, -1, 0, n, obstaclesSet) +
  26.                    WalkPath(rQ, cQ, -1, 1, n, obstaclesSet) +
  27.                    WalkPath(rQ, cQ, 0, 1, n, obstaclesSet) +
  28.                    WalkPath(rQ, cQ, 1, 1, n, obstaclesSet) +
  29.                    WalkPath(rQ, cQ, 1, 0, n, obstaclesSet) +
  30.                    WalkPath(rQ, cQ, 1, -1, n, obstaclesSet) +
  31.                    WalkPath(rQ, cQ, 0, -1, n, obstaclesSet);
  32.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement