taiPyのお悩み解決ブログ

日々の発見をまとめます!

Javaの解答例:学習記録:B - Minesweeper , AtCoder Beginner Contest 075

はじめに

作りかけです。2024年4月10日 21時10分。おやすみなさい。明日の自分よ、頑張れ!

今回のポイント

目次

問題と解説

問題

atcoder.jp

YouTube解説

www.youtube.com

解答例

import java.util.Scanner;

public class Main {
    static int H, W;
    static String[] B = new String[101];

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        H = scanner.nextInt();
        W = scanner.nextInt();
        for (int y = 0; y < H; y++) {
            B[y] = scanner.next();
        }

        for (int y = 0; y < H; y++) {
            for (int x = 0; x < W; x++) {
                if (B[y].charAt(x) == '.') {
                    int c = 0;
                    for (int dx = -1; dx <= 1; dx++) {
                        for (int dy = -1; dy <= 1; dy++) {
                            if (dx == 0 && dy == 0) continue;
                            int xx = x + dx;
                            int yy = y + dy;
                            if (0 <= xx && xx < W && 0 <= yy && yy < H) {
                                if (B[yy].charAt(xx) == '#') c++;
                            }
                        }
                    }
                    char[] temp = B[y].toCharArray();
                    temp[x] = (char) ('0' + c);
                    B[y] = new String(temp);
                }
            }
        }

        for (int y = 0; y < H; y++) {
            System.out.println(B[y]);
        }
    }
}