Java

[백준 9082] 지뢰찾기 (JAVA)

iheeeee6-6 2023. 5. 30. 22:31
728x90

https://www.acmicpc.net/problem/9082

 

9082번: 지뢰찾기

지뢰찾기 게임은 2×N 배열에 숨겨져 있는 지뢰를 찾는 게임이다. 지뢰 주위에 쓰여 있는 숫자들로 지뢰를 찾을 수 있는데, 한 블록에 쓰여진 숫자는 그 블록 주위에 지뢰가 몇 개 있는지를 나타

www.acmicpc.net

 

첫번째 위치, 마지막 위치, 나머지 위치 이렇게 세 구간으로 나눠서

주변 위치에 있는 지뢰 수가 모두 0이 아니라면 모두 -1 처리를 하고, 지뢰 카운트를 +1 한다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	static int n, num;
	static char[] nArr;

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		for (int i = 0; i < n; i++) {
			num = Integer.parseInt(br.readLine());
			int[] numArr = new int[num];
			nArr = br.readLine().toCharArray();
			for (int k=0;k<num;k++) {
				numArr[k] = nArr[k] - '0';
			}
			char[] arr = br.readLine().toCharArray();
			
			int result = 0;
			for (int j = 0; j < num; j++) {
				if (j == 0) {
					if (numArr[j] != 0 && numArr[j + 1] != 0) {
						numArr[j]--;
						numArr[j + 1]--;
						result++;
					}
				} else if (j == num - 1) {
					if (numArr[j - 1] != 0 && numArr[j] != 0) {
						numArr[j]--;
						numArr[j - 1]--;
						result++;
					}
				} else {
					if (numArr[j - 1] != 0 && numArr[j] != 0 && numArr[j + 1] != 0) {
						numArr[j - 1]--;
						numArr[j]--;
						numArr[j + 1]--;
						result++;
					}
				}
			}

			System.out.println(result);
		}

	}
}