题解 | #扫雷#

扫雷

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]);
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 14:11
很喜欢小米的新车,校招薪资每月22k,攒多久能买?
测试糕手手:别看工资,先看现金流存款。有50W存款以上再考虑,车是消耗品,选适合自己的重要。你有钱就当我没说过
点赞 评论 收藏
分享
炫哥_:哥们项目描述里面vector和mysql之类的都要写吗,直接开头技术栈巴拉巴拉就行了,完全不是技术点啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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