题解 | 扫雷

扫雷

https://www.nowcoder.com/practice/d5f277427d9a4cd3ae60ea6c276dddfd

import java.util.Scanner;

public class Main {

    static char[][] arr;

    static char[][] ans;

    static void check(int n,int m) {

        int[] arr1 = {-1,0,1,-1,1,-1,0,1};

        int[] arr2 = {-1,-1,-1,0,0,1,1,1};

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < m; j++) {

                int sum = 0;

                if (arr[i][j] == '*') {

                    ans[i][j] = '*';

                    continue;

                }

                for(int k = 0; k < 8; k++) {

                    int temp1 = arr1[k] + i;

                    int temp2 = arr2[k] + j;

                    if (temp1 >= 0 && temp2 >= 0 && temp1 < n && temp2 < m ) {

                        if (arr[temp1][temp2] == '*')

                            sum++;

                    }

                }

                ans[i][j] = (char)(sum + '0');

            }

        }

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < m; j++) {

                System.out.print(ans[i][j]);

            }

            System.out.println();

        }

    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int m = sc.nextInt();

        sc.nextLine();

        arr = new char[n][m];

        ans = new char[n][m];

        for (int i = 0; i < n; i++) {

            String s = sc.nextLine();

            for (int j = 0; j < m; j++) {

                arr[i][j] = s.charAt(j);

            }

        }

        check(n,m);

    }

}

全部评论

相关推荐

开发转测第二人:没实习的话,两个项目吧,八股也要准备一下,这个时间点有点小晚了
点赞 评论 收藏
分享
牛至超人:我将凌晨两点给你打电话
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务