-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSam2.java
More file actions
147 lines (113 loc) · 3.26 KB
/
Sam2.java
File metadata and controls
147 lines (113 loc) · 3.26 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Sam2 {
static int N, M;
static boolean vis[][];
static int dr[] = {1, -1, 0, 0};
static int dc[] = {0, 0, -1, 1};
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new FileReader("input.txt"));
PrintWriter out = new PrintWriter(System.out);
N = sc.nextInt();
M = sc.nextInt();
String s;
int grid1[][], grid2[][];
grid1 = new int[N][M];
grid2 = new int[N][M];
vis = new boolean[N][M];
for(int i = 0; i < N; ++i) {
s = sc.next();
for(int j = 0; j < M; ++j) {
grid1[i][j] = s.charAt(j);
}
}
sc.nextLine();
for(int i = 0; i < N; ++i) {
s = sc.next();
for(int j = 0; j < M; ++j) {
grid2[i][j] = s.charAt(j);
}
}
int dist1 = bfs(0, 0, grid1);
for(int i = 0; i < N; ++i) Arrays.fill(vis[i], false);
int dist2 = bfs(0, 0, grid2);
for(int i = 0; i < N; ++i) Arrays.fill(vis[i], false);
int dist = bfs2(grid1, grid2);
if(dist1 == dist2 && dist == dist1 && dist != -1)
out.println("YES");
else
out.println("NO");
out.flush();
out.close();
}
static int bfs2(int grid1[][], int grid2[][]) {
Queue<Integer> q = new LinkedList<Integer>();
q.offer(0); q.offer(0); q.offer(0);
vis[0][0] = true;
int a, b, d, x, y;
while(!q.isEmpty()) {
a = q.poll(); b = q.poll(); d = q.poll();
if(a == N - 1 && b == M - 1) return d;
for(int i = 0; i < 4; ++i) {
x = a + dr[i];
y = b + dc[i];
if(valid(x, y) && !vis[x][y] && grid1[x][y] != '#' && grid2[x][y] != '#') {
q.offer(x);
q.offer(y);
q.offer(d + 1);
vis[x][y] = true;
}
}
}
return -1;
}
static boolean valid(int x, int y) {
return x >= 0 && y >= 0 && x < N && y < M;
}
static int bfs(int x, int y, int [][] grid) {
Queue<Integer> q = new LinkedList<Integer>();
q.offer(x); q.offer(y); q.offer(0);
vis[x][y] = true;
int a, b, d;
while(!q.isEmpty()) {
a = q.poll(); b = q.poll(); d = q.poll();
if(a == N - 1 && b == M - 1) return d;
for(int i = 0; i < 4; ++i) {
x = a + dr[i];
y = b + dc[i];
if(valid(x, y) && !vis[x][y] && grid[x][y] != '#') {
q.offer(x);
q.offer(y);
q.offer(d + 1);
vis[x][y] = true;
}
}
}
return -1;
}
static class Scanner{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public Scanner(FileReader r){ br = new BufferedReader(r);}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException { return Double.parseDouble(next()); }
public boolean ready() throws IOException {return br.ready();}
}
}