자바로 백준 1996 풀기

hong030·2023년 7월 2일
0
  • 실버 5단계 문제

풀이)

배열을 통해 지뢰를 탐색하는데, 상하좌우로 움직이는 것을 좌표값을 통해 계산한다.
dx, dy를 배열로 두어 계산한다.

내 코드)

import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class Main {
 
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		char[][] map = new char[n][n], mine = new char[n][n];
		int[] dx = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy = { -1, 0, 1, -1, 1, -1, 0, 1 };
		for (int i = 0; i < n; i++) {
			String tmp = br.readLine();
			for (int j = 0; j < n; j++)
				map[i][j] = tmp.charAt(j);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				int cnt = 0;
				if (map[i][j] != '.') {
					mine[i][j] = '*';
					continue;
				}
				else
					for (int k = 0; k < 8; k++) {
						if (i + dx[k] < 0 || i + dx[k] >= n || j + dy[k] < 0 || j + dy[k] >= n)
							continue;
						if (map[i + dx[k]][j + dy[k]] > '0')
							cnt += map[i + dx[k]][j + dy[k]] - '0';
					}
				if (cnt < 10)
					mine[i][j] = (char) (cnt + '0');
				else
					mine[i][j] = 'M';
			}
		}
		for (int i = 0; i < n; i++)
			System.out.println(mine[i]);
	}
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글