• 문제 링크
https://www.acmicpc.net/problem/4108
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
static boolean[][] mat;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
String s;
while ((s = br.readLine()).charAt(0) != '0') {
StringTokenizer st = new StringTokenizer(s);
mat = new boolean[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())];
for (int i = 0, j = 0; i < mat.length; i++, j = 0)
for (char c : br.readLine().toCharArray()) mat[i][j++] = c == '*';
for (int i = 0; i < mat.length; i++, sb.append('\n'))
for (int j = 0; j < mat[0].length; j++) sb.append(mat[i][j] ? "*" : count(i, j));
}
bw.write(sb.toString());
bw.flush();
}
private static int count(int y, int x) {
int cnt = 0;
for (int[] d : dir) if (inRange(d[0] + y, d[1] + x) && mat[d[0] + y][d[1] + x]) cnt++;
return cnt;
}
private static boolean inRange(int y, int x) {
return y >= 0 && y < mat.length && x >= 0 && x < mat[0].length;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 11104 Fridge of Your Dreams - Implementation / Java (0) | 2025.05.25 |
---|---|
[백준] 10599 페르시아의 왕들 - Implementation / Java (0) | 2025.05.24 |
[백준] 1308 D-Day - Implementation / Java (0) | 2025.05.22 |
[백준] 17389 보너스 점수 - Implementation / Java (0) | 2025.05.21 |
[백준] 9501 꿍의 우주여행 - Implementation / Java (0) | 2025.05.20 |
댓글