-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.java
96 lines (90 loc) · 3.2 KB
/
solution.java
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
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
/*
* Since detonations take place only at odd times, if n (the number of seconds) is even,
* the grid is composed only of O
* At n == 1, no detonations take place, so the grid is the starting grid .
* At n == 3, the first detonation happens .
* It may be noted that there is a recurrent pattern that repeat itself every 4 seconds:
* - at n == 5, 9, 13, ... (i.e. when n % 4 == 1), the grid is equal to the grid after second detonation
* - at n == 7, 11, 15, ... (i.e when n % 4 == 3), the grid is equal to the grid after third detonation
*/
public class solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int r = in.nextInt();
int c = in.nextInt();
int n = in.nextInt();
char[][] matrix = new char[r][c];
for (int i = 0; i < r; i++) {
matrix[i] = in.next().toCharArray();
}
char[][] newMatrix = new char[r][c];
char[][] newMatrix2 = new char[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
newMatrix[i][j] = 'O';
newMatrix2[i][j] = 'O';
}
}
if (n % 2 == 1) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (matrix[i][j] == 'O') {
newMatrix[i][j] = '.';
if (i - 1 >= 0) {
newMatrix[i - 1][j] = '.';
}
if (i + 1 < r) {
newMatrix[i + 1][j] = '.';
}
if (j - 1 >= 0) {
newMatrix[i][j - 1] = '.';
}
if (j + 1 < c) {
newMatrix[i][j + 1] = '.';
}
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (newMatrix[i][j] == 'O') {
newMatrix2[i][j] = '.';
if (i - 1 >= 0) {
newMatrix2[i - 1][j] = '.';
}
if (i + 1 < r) {
newMatrix2[i + 1][j] = '.';
}
if (j - 1 >= 0) {
newMatrix2[i][j - 1] = '.';
}
if (j + 1 < c) {
newMatrix2[i][j + 1] = '.';
}
}
}
}
}
// at n==1, no detonations take place
if (n == 1) {
printMatrix(matrix, r, c);
} else if (n % 4 == 1) {
printMatrix(newMatrix2, r, c);
} else {
printMatrix(newMatrix, r, c);
}
in.close();
}
private static void printMatrix(char[][] matrix, int r, int c) {
for (int i = 0; i < r; i++) {
System.out.println(matrix[i]);
}
}
}