๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 16918_๋ด„๋ฒ„๋งจ

๋ฟŒ์•ผ._. 2023. 6. 30. 14:38

Silver I

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/16918)

< ๋ด„๋ฒ„๋งจ >

 

๋ฌธ์ œ ํ’€์ด & ์ƒ๊ฐ

 

์‹œ๊ฐ„์ด ์ง์ˆ˜ ์ดˆ ์ผ ๋•Œ๋Š” ํญํƒ„์„ ์„ค์น˜ํ•ด ์ฃผ๊ณ  ํ™€์ˆ˜ ์ดˆ ์ผ ๋•Œ๋Š” ํญํƒ„์„ ํญ๋ฐœ์‹œํ‚จ๋‹ค.

 

 my solution (Java)

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 _16918_ { // ๋ด„๋ฒ„๋งจ
	static int arr[][], r, c;
	static int dx[] = { -1, 1, 0, 0 };
	static int dy[] = { 0, 0, -1, 1 };

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		r = Integer.parseInt(st.nextToken());
		c = Integer.parseInt(st.nextToken());
		int n = Integer.parseInt(st.nextToken());

		arr = new int[r][c];
		for (int i = 0; i < r; i++) {
			String str = bf.readLine();
			for (int j = 0; j < c; j++) {
				if (str.charAt(j) == 'O')
					arr[i][j] = 1;
			}
		}

		int time = 1;
		boolean flag = false;
		while (time < n) {
			time += 1;
			if (time % 2 == 0) {
				int temp = 2;
				if (flag)
					temp = 1;
				fill(temp);
			} else {
				if (!flag) {
					bomber(1);
					flag = true;
				} else {
					bomber(2);
					flag = false;
				}
			}
		}
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				if (arr[i][j] == 0) {
					bw.write(".");
				} else {
					bw.write("O");
				}
			}
			bw.write("\n");
		}
		bw.flush();
	}

	private static void fill(int num) {
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				if (arr[i][j] == 0)
					arr[i][j] = num;
			}
		}
	}

	private static void bomber(int num) {
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				if (arr[i][j] == num) {
					arr[i][j] = 0;
					for (int k = 0; k < 4; k++) {
						int x = i + dx[k];
						int y = j + dy[k];
						if (x >= 0 && x < r && y >= 0 && y < c && arr[x][y] != num) {
							arr[x][y] = 0;
						}
					}
				}
			}
		}
	}
}

 

Main

๋ณ€์ˆ˜)
r, c : ์ง์‚ฌ๊ฐํ˜• ๊ฒฉ์žํŒ ํฌ๊ธฐ
n : ์ดˆ
arr : ๊ฒฉ์žํŒ ์ •๋ณด
time : ์ดˆ
flag : ํ„ฐํŠธ๋ฆด ํญํƒ„ ์ •๋ณด

 

- ์ง์‚ฌ๊ฐํ˜• ๊ฒฉ์žํŒ ํฌ๊ธฐ(r, c), ์ดˆ(n) ์ž…๋ ฅ

- ๊ฒฉ์žํŒ ์ •๋ณด ์ €์žฅ

- 2์ดˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ง์ˆ˜ ์ดˆ ์ผ ๋•Œ๋Š” ํญํƒ„์„ ์„ค์น˜ํ•ด ์ฃผ๊ณ , ํ™€์ˆ˜ ์ดˆ ์ผ ๋•Œ๋Š” ํญํƒ„์„ ํ„ฐํŠธ๋ฆผ

- arr ์ถœ๋ ฅ

 

fill

๋นˆ์นธ์ด๋ผ๋ฉด ํญํƒ„์œผ๋กœ ์ฑ„์šฐ๊ธฐ

 

bomber

ํ„ฐํŠธ๋ฆด ํญํƒ„์˜ ์ƒํ•˜์ขŒ์šฐ๊ฐ€ ๊ฒฉ์žํŒ ๋ฒ”์œ„ ์•ˆ์ด๋ฉฐ ํ˜„์žฌ ํ„ฐ์งˆ ํญํƒ„์ด ์•„๋‹ˆ๋ผ๋ฉด ํ„ฐํŠธ๋ฆผ