-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVA_10189.java
85 lines (69 loc) · 2.05 KB
/
UVA_10189.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
import java.util.*;
import java.io.BufferedReader;
import java.lang.*;
public class UVA_10189 {
private static class mineSweeper {
private static int[] dxx = { -1, -1, -1, 0, 1, 1, 1, 0 };
private static int[] dyy = { -1, 0, 1, 1, 1, 0, -1, -1 };
private static long[][] mineGrid;
public static void problemSolve(String[] Grid, int n, int m) {
mineGrid = new long[n + 50][m + 50];
for (int i = 0; i < mineGrid.length; i++) {
Arrays.fill(mineGrid[i], 0);
}
for (int i = 2; i <= n + 1; i++) {
for (int j = 1; j <= m; j++) {
if (Grid[i - 1].charAt(j - 1) == '*') {
mineGrid[i - 1][j - 1] = '*';
gridCalculation(j - 1, i - 1, n, m);
}
}
// System.out.println();
}
}
public static void show(int n, int m) {
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) {
if (mineGrid[i][j] == 42) {
System.out.print("*");
} else
System.out.print(mineGrid[i][j]+"");
}
System.out.println();
}
}
private static void gridCalculation(int x, int y, int n, int m) {
for (int i = 0; i < dxx.length; i++) {
int xx = dxx[i] + x;
int yy = dyy[i] + y;
if (xx >= 0 && xx < m && yy >= 1 && yy <= n) {
if (Grid[yy].charAt(xx) == '.') {
mineGrid[yy][xx]++;
}
}
}
}
}
public static void main(String[] args) throws Exception {
long testCase = 0;
// while(scanner.hasNextLine()){
while (true) {
int ny = scanner.nextInt();
int mx = scanner.nextInt();
if (ny == 0 && mx == 0)
break;
testCase++;
for (int i = 0; i <= ny; i++) {
Grid[i] = scanner.nextLine();
}
mx = Grid[1].length();
mineSweeper.problemSolve(Grid, ny, mx);
if(testCase>1) System.out.println();
System.out.println("Field #" + testCase + ":");
mineSweeper.show(ny, mx);
}
}
private static Scanner scanner = new Scanner(System.in);
private static String[] Grid = new String[100 + 50];
//private static Scanner scanner = fileConnectedFactory.getScanner();
}