张乐飞


私信TA

用户名:anmyi

访问量:8107

签 名:

等  级
排  名 480
经  验 4681
参赛次数 0
文章发表 55
年  龄 0
在职情况 学生
学  校 湖北生物科技职业学院
专  业

  自我简介:

解题思路:

注意事项:

参考代码:


import java.util.Arrays;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int n1 = 1;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            if (n == 0 && m == 0)
                break;
            String a[] = new String[n];
            char b[][] = new char[n][m];
            int c[][] = new int[n][m];
            for (int j = 0; j < a.length; j++) {
                a[j] = scanner.next();
                for (int k = 0; k < a[j].length(); k++) {
                     
                    b[j][k] = a[j].charAt(k);
                    if (b[j][k] == '*'&&n!=1) {
                        if (j == 0 && k == 0) {
                            c[j + 1][k]++;
                            c[j][k + 1]++;
                            c[j+1][k + 1]++;
                        } else if (j == 0 && k != 0 && k != c[0].length-1) {
                            c[j][k + 1]++;
                            c[j][k - 1]++;
                            c[j + 1][k]++;
                            c[j+1][k + 1]++;
                            c[j+1][k - 1]++;
                        } else if (j == 0 && k == c[0].length-1) {
                            c[j + 1][k]++;
                            c[j][k - 1]++;
                            c[j+1][k - 1]++;
                        } else if (j != 0 && j != c.length-1 && k == 0) {
                            c[j + 1][k]++;
                            c[j - 1][k]++;
                            c[j][k + 1]++;
                            c[j-1][k + 1]++;
                            c[j+1][k + 1]++;
                        } else if (j != 0 && j != c.length-1 && k == c[j].length-1) {
                            c[j + 1][k]++;
                            c[j - 1][k]++;
                            c[j][k - 1]++;
                            c[j+1][k - 1]++;
                            c[j-1][k - 1]++;
                        } else if (j == c.length-1 && k == 0) {
                            c[j - 1][k]++;
                            c[j][k + 1]++;
                            c[j-1][k + 1]++;
                        } else if (j == c.length-1 && k != 0 && k != c[j].length-1) {
                            c[j - 1][k]++;
                            c[j][k - 1]++;
                            c[j][k + 1]++;
                            c[j-1][k + 1]++;
                            c[j-1][k - 1]++;
                        } else if (j == c.length-1 && k == c[j].length-1) {
                            c[j - 1][k]++;
                            c[j][k - 1]++;
                            c[j-1][k - 1]++;
                        } else {
                            c[j - 1][k]++;
                            c[j - 1][k+1]++;
                            c[j + 1][k]++;
                            c[j - 1][k-1]++;
                            c[j + 1][k+1]++;
                            c[j + 1][k-1]++;
                            c[j][k - 1]++;
                            c[j][k + 1]++;
                        }
                    }
                    if (b[j][k] == '*'&&n==1) {
                        if (j == 0 && k == 0) {
                            c[j][k + 1]++;
                        } else if (j == 0 && k != 0 && k != c[0].length-1) {
                            c[j][k + 1]++;
                            c[j][k - 1]++;
                        } else if (j == 0 && k == c[0].length-1) {
                            c[j][k - 1]++;               
                        } 
                    }
                }
            }
            System.out.println("Field #" + n1 + ":");
            n1++;
            for (int j = 0; j < c.length; j++) {
                for (int k = 0; k < c[j].length; k++) {
                    if (b[j][k] == '*') {
                        System.out.print(b[j][k]);
                    } else
                        System.out.print(c[j][k]);
                }
                System.out.println();
            }
            System.out.println();
        }
   
   
}
}


 

0.0分

0 人评分

  评论区

  • «
  • »