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
| import java.util.Scanner; // 1:无需package // 2: 类名必须Main, 不可修改
public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); //在此输入您的代码... int n = scan.nextInt(); int m = scan.nextInt(); int[][] data = new int[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { data[i][j] = scan.nextInt(); } } int[][] assistance = new int[n][m]; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(data[i][j] == 1) { assistance[i][j] = 9; continue; } //如果上一行存在 if(i - 1 >= 0) { //正上方如果有雷 if(data[i - 1][j] == 1) { assistance[i][j]++; } //如果左上方存在且有雷 if(j - 1 >= 0 && data[i - 1][j - 1] == 1) { assistance[i][j]++; } //右上方存在且有雷 if(j + 1 < m && data[i - 1][j + 1] == 1) { assistance[i][j]++; } } //对于同一行前一列存在且有雷 if(j - 1 >= 0 && data[i][j - 1] == 1) { assistance[i][j]++; } //对于同一行后一列存在 if(j + 1 < m && data[i][j + 1] == 1) { assistance[i][j]++; } //下一行存在 if(i + 1 < n) { //正下方有雷 if(data[i + 1][j] == 1) { assistance[i][j]++; } //左下方存在且有雷 if(j - 1 >= 0 && data[i + 1][j - 1] == 1) { assistance[i][j]++; } //右下方存在且有雷 if(j + 1 < m && data[i + 1][j + 1] == 1) { assistance[i][j]++; } } } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(assistance[i][j] + " "); } System.out.println(); } scan.close(); } }
|