Skip to content

Commit 4b36f66

Browse files
committed
shadows of the knight
1 parent 1014ee9 commit 4b36f66

File tree

4 files changed

+165
-104
lines changed

4 files changed

+165
-104
lines changed

src/main/java/main/java/MainTest.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package main.java.codechef;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
8+
public class SNELECT {
9+
public static void main(String[] args) throws IOException {
10+
final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
11+
final StringBuilder sb = new StringBuilder();
12+
final Solver solver = new Solver();
13+
for (int t = Integer.parseInt(in.readLine()); t > 0; t--) {
14+
final String[] first = in.readLine().split(" ");
15+
final int n = Integer.parseInt(first[0]), m = Integer.parseInt(first[1]);
16+
final int a[][] = new int[n][m];
17+
final int max[][] = new int[n * m][2];
18+
int length = 0, maximum = 0;
19+
for (int i = 0; i < n; i++) {
20+
final String[] row = in.readLine().split(" ");
21+
for (int j = 0; j < m; j++) {
22+
a[i][j] = Integer.parseInt(row[j]);
23+
if (maximum < a[i][j]) {
24+
maximum = a[i][j];
25+
length = 0;
26+
max[length][0] = i;
27+
max[length][1] = j;
28+
length++;
29+
} else if (maximum == a[i][j]) {
30+
max[length][0] = i;
31+
max[length][1] = j;
32+
length++;
33+
}
34+
}
35+
}
36+
sb.append(solver.solve(max, length, n, m)).append('\n');
37+
}
38+
System.out.println(sb.toString());
39+
}
40+
}
41+
42+
class Solver {
43+
44+
public int solve(final int q[][], int rear, final int n, final int m) {
45+
final boolean[][] visited = new boolean[n][m];
46+
for (int i = 0; i < rear; i++) {
47+
visited[q[i][0]][q[i][1]] = true;
48+
}
49+
int count = 0;
50+
int front = 0;
51+
while (front < rear) {
52+
final int element[] = q[front++];
53+
final int row = element[0];
54+
final int col = element[1];
55+
final int oldRear = rear;
56+
if (row > 0) {
57+
if (col > 0) {
58+
if (!visited[row - 1][col - 1]) {
59+
q[rear++] = new int[]{row - 1, col - 1};
60+
visited[row - 1][col - 1] = true;
61+
}
62+
}
63+
if (!visited[row - 1][col]) {
64+
q[rear++] = new int[]{row - 1, col};
65+
visited[row - 1][col] = true;
66+
}
67+
if (col < m - 1) {
68+
if (!visited[row - 1][col + 1]) {
69+
q[rear++] = new int[]{row - 1, col + 1};
70+
visited[row - 1][col + 1] = true;
71+
}
72+
}
73+
}
74+
if (row < n - 1) {
75+
if (col > 0) {
76+
if (!visited[row + 1][col - 1]) {
77+
q[rear++] = new int[]{row + 1, col - 1};
78+
visited[row + 1][col - 1] = true;
79+
}
80+
}
81+
if (!visited[row + 1][col]) {
82+
q[rear++] = new int[]{row + 1, col};
83+
visited[row + 1][col] = true;
84+
}
85+
if (col < m - 1) {
86+
if (!visited[row + 1][col + 1]) {
87+
q[rear++] = new int[]{row + 1, col + 1};
88+
visited[row + 1][col + 1] = true;
89+
}
90+
}
91+
}
92+
if (col > 0) {
93+
if (!visited[row][col - 1]) {
94+
q[rear++] = new int[]{row, col - 1};
95+
visited[row][col - 1] = true;
96+
}
97+
}
98+
if (col < m - 1) {
99+
if (!visited[row][col + 1]) {
100+
q[rear++] = new int[]{row, col + 1};
101+
visited[row][col + 1] = true;
102+
}
103+
}
104+
if (oldRear != rear) {
105+
System.out.println(Arrays.deepToString(q));
106+
count++;
107+
}
108+
}
109+
return count;
110+
}
111+
}

src/main/java/main/java/codechef/Solver.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main.java.codingame;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Auto-generated code below aims at helping you parse
7+
* the standard input according to the problem statement.
8+
**/
9+
public class ShadowsOfTheKnight {
10+
11+
public static void main(String args[]) {
12+
Scanner in = new Scanner(System.in);
13+
int W = in.nextInt(); // width of the building.
14+
int H = in.nextInt(); // height of the building.
15+
int N = in.nextInt(); // maximum number of turns before game over.
16+
int X0 = in.nextInt();
17+
int Y0 = in.nextInt();
18+
int LX = 0, LY = 0, HX = W - 1, HY = H - 1;
19+
// game loop
20+
in.next();
21+
X0 = (W - 1) / 2;
22+
Y0 = (H - 1) / 2;
23+
System.out.println(X0 + " " + Y0);
24+
while (true) {
25+
final String bombDir = in.next(); // the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)
26+
if (bombDir.length() > 1) {
27+
if (bombDir.charAt(1) == 'L') {
28+
HX = X0 - 1;
29+
} else if (bombDir.charAt(1) == 'R') {
30+
LX = X0 + 1;
31+
}
32+
if (bombDir.charAt(0) == 'U') {
33+
HY = Y0 - 1;
34+
} else {
35+
LY = Y0 + 1;
36+
}
37+
} else {
38+
if (bombDir.charAt(0) == 'L') {
39+
HX = X0 - 1;
40+
} else if (bombDir.charAt(0) == 'R') {
41+
LX = X0 + 1;
42+
} else if (bombDir.charAt(0) == 'U') {
43+
HY = Y0 - 1;
44+
} else {
45+
LY = Y0 + 1;
46+
}
47+
}
48+
X0 = (HX + LX) / 2;
49+
Y0 = (HY + LY) / 2;
50+
System.err.println(HX + " " + LX + " " + HY + " " + LY);
51+
System.out.println(X0 + " " + Y0);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)