写了一个比较简单的代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan =new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
//一个二维数组储存输入的点
int[][] the_y=new int[n][m];
//一个二维数组储存输出的点,注意这个输出的二维数组比输入的大一圈
/*目的下面会给出。
* 例如:
* 输入:5*6
* 1 1 1 1 1 1
* 1 1 1 1 1 1
* 1 1 1 1 1 1
* 1 1 1 1 1 1
* 1 1 1 1 1 1
* 输出:7*8
* 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0
* 0 0 0 0 0 0 0 0
*/
int[][] the_l=new int[n+2][m+2];
//输入填充the_y,并且置the_l全部为零
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
the_y[i][j]=scan.nextInt();
the_l[i][j]=0;
}
}
//这里会遍历输入数组的所有值,如果是1,则置9,周围全加1,因为the_l比the_y大一圈,所以不用考虑边界。
for(int i=1;i<n+1;i++) {
for(int j=1;j<m+1;j++) {
if(the_y[i-1][j-1]==1) {
the_l[i][j]=9;
the_l[i-1][j-1]++;
the_l[i-1][j]++;
the_l[i-1][j+1]++;
the_l[i][j-1]++;
the_l[i][j+1]++;
the_l[i+1][j-1]++;
the_l[i+1][j]++;
the_l[i+1][j+1]++;
}
}
}
//打印,注意,这里要将大于9的数置9
for(int i=1;i<n+1;i++) {
for(int j=1;j<m+1;j++) {
if(the_l[i][j]>9)
the_l[i][j]=9;
System.out.print(the_l[i][j]+" ");
}
System.out.println();
}
}
}