题解 | #扫雷#
扫雷
https://www.nowcoder.com/practice/d5f277427d9a4cd3ae60ea6c276dddfd
//参考已有题解的,超时是因为调用太多次System.out,换成每行输出,次数从n*m次减少到n次,时间0.4s 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[][] array = new char[n][m]; for(int i = 0; i < n; i++){ array[i] = sc.next().toCharArray(); } for(int i = 0; i <n; i++){ for(int j =0; j < m; j++){ if(array[i][j] == '*'){ continue; }else{ int count = 0; for(int x = i-1; x <= i+1; x++){ if(x < 0 || x>= n){ continue; } for(int y = j-1; y <=j+1; y++){ if(y < 0 || y>= m){ continue; }else if(array[x][y] == '*'){ count++; } } } array[i][j] = (char)(count+'0'); } } System.out.println(array[i]); } } }