-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardGamesMarks.java
More file actions
78 lines (60 loc) · 2 KB
/
BoardGamesMarks.java
File metadata and controls
78 lines (60 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.*;
public class BoardGamesMarks {
static class Pair {
int x, y, moves;
Pair(int x, int y, int moves) {
this.x = x;
this.y = y;
this.moves = moves;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int[][] grid = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = scanner.nextInt();
}
}
int sx = scanner.nextInt();
int sy = scanner.nextInt();
int dx = scanner.nextInt();
int dy = scanner.nextInt();
int mx = scanner.nextInt();
int my = scanner.nextInt();
System.out.println(bfs(grid, sx, sy, dx, dy, mx, my));
scanner.close();
}
private static int bfs(int[][] grid, int sx, int sy, int dx, int dy, int mx, int my) {
boolean[][] visited = new boolean[grid.length][grid[0].length];
Queue<Pair> queue = new LinkedList<>();
queue.offer(new Pair(sx, sy, 0));
visited[sx][sy] = true;
int[][] directions = {
{mx, my},
{my, -mx},
{-my, mx},
{-mx, -my}
};
while (!queue.isEmpty()) {
Pair pair = queue.poll();
if (pair.x == dx && pair.y == dy) {
return pair.moves;
}
for (int[] dir : directions) {
int nx = pair.x + dir[0];
int ny = pair.y + dir[1];
if (isValid(nx, ny, grid.length, grid[0].length) && !visited[nx][ny] && grid[nx][ny] == 0) {
queue.offer(new Pair(nx, ny, pair.moves + 1));
visited[nx][ny] = true;
}
}
}
return -1;
}
private static boolean isValid(int x, int y, int m, int n) {
return x >= 0 && x < m && y >= 0 && y < n;
}
}