解题思路:
利用炸弹位置来计算,先把炸弹的坐标存list里,然后再计算炸弹周围的数量,++即可
注意事项:
计算数量的时候要注意判断位置的时候,要先判断位置合理性,然后判断不可在自己的位置
参考代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
char[][] db = new char[n][m];
int[][] arr = new int[n][m];
List<int[]> count = new ArrayList<>();
for (int i = 0; i < n; i++) {
String str = sc.next();
for (int j = 0; j < m; j++) {
db[i][j] = '0';
arr[i][j] = 0;
if (str.charAt(j) == '*') {
count.add(new int[]{i, j});
}
}
}
count.forEach(point -> countNum(point[0], point[1], db, arr));
printArray(db);
}
public static void countNum(int i, int j, char[][] db, int[][] arr) {
db[i][j] = '*';
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
int a = i + x;
int b = j + y;
if (a >= 0 && a < db.length && b >= 0 && b < db[0].length && db[a][b] != '*') {
arr[a][b]++;
db[a][b] = (char) (arr[a][b] + '0');
}
}
}
}
public static void printArray(char[][] db) {
for (char[] row : db) {
System.out.println(new String(row));
}
}
}
0.0分
0 人评分