题解 | 扫雷
扫雷
https://www.nowcoder.com/practice/d5f277427d9a4cd3ae60ea6c276dddfd
import java.util.Scanner;
public class Main {
static char[][] arr;
static char[][] ans;
static void check(int n,int m) {
int[] arr1 = {-1,0,1,-1,1,-1,0,1};
int[] arr2 = {-1,-1,-1,0,0,1,1,1};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int sum = 0;
if (arr[i][j] == '*') {
ans[i][j] = '*';
continue;
}
for(int k = 0; k < 8; k++) {
int temp1 = arr1[k] + i;
int temp2 = arr2[k] + j;
if (temp1 >= 0 && temp2 >= 0 && temp1 < n && temp2 < m ) {
if (arr[temp1][temp2] == '*')
sum++;
}
}
ans[i][j] = (char)(sum + '0');
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(ans[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
sc.nextLine();
arr = new char[n][m];
ans = new char[n][m];
for (int i = 0; i < n; i++) {
String s = sc.nextLine();
for (int j = 0; j < m; j++) {
arr[i][j] = s.charAt(j);
}
}
check(n,m);
}
}


