题解 | 扫雷

扫雷

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

public class Program {
    public static void Main() {
        string[] line = System.Console.ReadLine().Split(" ");

        int n = int.Parse(line[0]);
        int m = int.Parse(line[1]);



        int[,] juZhen = new int[n, m];
        for (int i = 0; i < n; i++) {
            string lineInput =  System.Console.ReadLine();
            for (int j = 0; j < m; j++) {
                if (lineInput[j] == '.') {
                    juZhen[i, j] = 0;
                } else {
                    juZhen[i, j] = 1;
                }
            }
        }

        //输出

        int[] dicx = {-1, -1, -1, 0, 0, 1, 1, 1};
        int[] dicy = {-1, 0, 1, -1, 1, -1, 0, 1};
        for (int i = 0; i < n; i++) 
        {
            for (int j = 0; j < m; j++)
            {
             
                //遍历所有元素
                if (juZhen[i, j] == 1) {
                    System.Console.Write("*");
                }
                else 
                {
                    int tmp = 0;
                    for (int k = 0; k < dicx.Length; k++) {
                        int ni = i + dicx[k];
                        int nj = j + dicy[k];

                    if (ni >= 0 && ni < n && nj >= 0 && nj < m) {
                            if(juZhen[ni, nj] == 1) {
                                tmp += 1;
                            }
                        }
                    }
                    System.Console.Write(tmp);
                }
            }
            System.Console.WriteLine();
        }

    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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