Skip to content

Commit e15db1e

Browse files
authored
Merge pull request #274 from AlgorithmWithGod/khj20006
[20250320] BOJ / G3 / A Knightly Pursuit / 권혁준
2 parents 09cbfad + eae59c2 commit e15db1e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
if(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int R, C;
26+
static int pr, pc, kr, kc;
27+
28+
static int[] dx = {-2,-2,-1,-1,1,1,2,2};
29+
static int[] dy = {-1,1,-2,2,-2,2,-1,1};
30+
31+
public static void main(String[] args) throws Exception {
32+
33+
for(int T=nextInt();T-->0;) {
34+
35+
ready();
36+
solve();
37+
}
38+
39+
bwEnd();
40+
41+
}
42+
43+
static void ready() throws Exception{
44+
45+
R = nextInt();
46+
C = nextInt();
47+
pr = nextInt();
48+
pc = nextInt();
49+
kr = nextInt();
50+
kc = nextInt();
51+
52+
}
53+
54+
static void solve() throws Exception{
55+
56+
int[][] time = new int[R+1][C+1];
57+
for(int i=1;i<=R;i++) Arrays.fill(time[i], -1);
58+
59+
time[kr][kc] = 0;
60+
Queue<int[]> Q = new LinkedList<>();
61+
62+
int win = -1;
63+
int stalemate = -1;
64+
Q.offer(new int[] {kr,kc,0});
65+
while(!Q.isEmpty()) {
66+
int[] now = Q.poll();
67+
int x = now[0], y = now[1], t = now[2];
68+
if(y == pc) {
69+
if(x<R && pr+t <= x && (x-pr-t)%2 == 0) {
70+
if(win == -1) win = x-pr;
71+
else win = Math.min(win, x-pr);
72+
}
73+
if(pr+t+1 <= x && (x-pr-t-1)%2 == 0) {
74+
if(stalemate == -1) stalemate = x-pr-1;
75+
else stalemate = Math.min(stalemate, x-pr-1);
76+
}
77+
78+
}
79+
for(int i=0;i<8;i++) {
80+
int xx = x+dx[i], yy = y+dy[i];
81+
if(xx<1 || xx>R || yy<1 || yy>C || time[xx][yy]!=-1) continue;
82+
time[xx][yy] = t+1;
83+
Q.offer(new int[] {xx,yy,t+1});
84+
}
85+
}
86+
if(win != -1) bw.write("Win in " + Math.max(win, 0) + " knight move(s).\n");
87+
else if(stalemate != -1) bw.write("Stalemate in " + stalemate + " knight move(s).\n");
88+
else bw.write("Loss in " + Math.max(R-pr-1, 0) + " knight move(s).\n");
89+
90+
}
91+
92+
}
93+
94+
```

0 commit comments

Comments
 (0)