-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathE.java
More file actions
176 lines (158 loc) · 4.17 KB
/
Copy pathE.java
File metadata and controls
176 lines (158 loc) · 4.17 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
public class E {
static class Pair {
int first,second;
public Pair(int f,int s) {
this.first = f;
this.second = s;
}
}
static boolean valid(int i,int j,int n, char arr[][]) {
return i >= 0 && i < n && j >= 0 && j < n && arr[i][j] != 'x';
}
public static void main(String[]args) throws Throwable {
Scanner sc = new Scanner("maze.in");
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
char arr[][] = new char[n][];
for (int i = 0 ; i < n ; ++i) {
arr[i] = sc.next().toCharArray();
}
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
Queue<Pair> q = new LinkedList();
boolean vis[][] = new boolean[n * n][n * n];
for (int i = 0 ; i < n ; ++i ) {
for (int j = 0 ; j < n ; ++j) {
if (arr[i][j] == 'x') {
continue;
}
q.add(new Pair(i * n + j, i * n + j));
vis[i * n + j][i * n + j] = true;
for (int k = 0 ; k < 4 ; ++k) {
int xc = i + dx[k],yc = j + dy[k];
if (valid(xc,yc,n,arr)) {
q.add(new Pair(i * n + j,xc * n + yc));
vis[i * n + j][xc * n + yc] = true;
}
}
}
}
int d[] = new int[4];
d[0] = 1;
d[1] = 0;
d[2] = 3;
d[3] = 2;
while (!q.isEmpty()) {
Pair p = q.poll();
vis[p.first][p.second] = true;
int i1 = p.first / n , j1 = p.first % n;
int i2 = p.second / n, j2 = p.second % n;
if (arr[i1][j1] == 'x') {
continue;
}
if (arr[i2][j2] == 'x') {
continue;
}
for (int k = 0 ; k < 4 ; ++k) {
int xc1 = i1 + dx[d[k]], yc1 = j1 + dy[d[k]];
int xc2 = i2 + dx[k], yc2 = j2 + dy[k];
if (valid(xc1,yc1,n,arr) && valid(xc2,yc2,n,arr)) {
int a = xc1 * n + yc1 , b = xc2 * n + yc2;
if (!vis[a][b]) {
q.add(new Pair(a,b));
vis[a][b] = true;
}
}
}
}
ArrayList<Integer> g[] = new ArrayList[n * n];
for (int i = 0 ; i < n * n ; ++i) {
g[i] = new ArrayList();
}
for (int i1 = 0 ; i1 < n ; ++i1) {
for (int j1 = 0 ; j1 < n ; ++j1) {
for (int i2 = 0 ; i2 < n ; ++i2) {
for (int j2 = 0 ; j2 < n ; ++j2) {
int a = i1 * n + j1, b = i2 * n + j2;
if (vis[a][b]) {
g[a].add(b);
g[b].add(a);
}
}
}
}
}
int x = sc.nextInt() - 1, y = sc.nextInt() - 1, k = sc.nextInt();
if (arr[x][y] == 'x') {
System.out.println(0);
} else {
int s = x * n + y;
int dis[] = new int[n * n];
Arrays.fill(dis, 1 << 30);
dis[s] = 0;
int ans = 0;
Queue<Integer> qq = new LinkedList();
qq.add(s);
while (!qq.isEmpty()) {
int u = qq.poll();
if (dis[u] > k) {
continue;
}
++ans;
for (Integer v : g[u]) {
if (dis[u] + 1 < dis[v]) {
dis[v] = dis[u] + 1;
qq.add(v);
}
}
}
System.out.println(ans);
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
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();
}
}
}