Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static long CoordToHash(int r, int c) {
- return r + ((long) c << 32);
- }
- static int WalkPath(int rq, int cq, int dr, int dc, int n, HashSet<long> obstacles) {
- int result = 0;
- rq += dr;
- cq += dc;
- while (rq != 0 && cq != 0 && rq != n + 1 && cq != n + 1) {
- if (obstacles.Contains(CoordToHash(rq, cq)))
- break;
- rq += dr;
- cq += dc;
- result++;
- }
- return result;
- }
- static int queensAttack(int n, int k, int rQ, int cQ, int[][] obstacles) {
- HashSet<long> obstaclesSet = new HashSet<long>();
- foreach (int[] obstacle in obstacles) {
- obstaclesSet.Add(CoordToHash(obstacle[0], obstacle[1]));
- }
- return WalkPath(rQ, cQ, -1, -1, n, obstaclesSet) +
- WalkPath(rQ, cQ, -1, 0, n, obstaclesSet) +
- WalkPath(rQ, cQ, -1, 1, n, obstaclesSet) +
- WalkPath(rQ, cQ, 0, 1, n, obstaclesSet) +
- WalkPath(rQ, cQ, 1, 1, n, obstaclesSet) +
- WalkPath(rQ, cQ, 1, 0, n, obstaclesSet) +
- WalkPath(rQ, cQ, 1, -1, n, obstaclesSet) +
- WalkPath(rQ, cQ, 0, -1, n, obstaclesSet);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement