๋ฌธ์ (์ถ์ฒ: 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
ํฐํธ๋ฆด ํญํ์ ์ํ์ข์ฐ๊ฐ ๊ฒฉ์ํ ๋ฒ์ ์์ด๋ฉฐ ํ์ฌ ํฐ์ง ํญํ์ด ์๋๋ผ๋ฉด ํฐํธ๋ฆผ
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 15900_๋๋ฌด ํ์ถ (0) | 2023.07.07 |
---|---|
[Baekjoon] 15903_์นด๋ ํฉ์ฒด ๋์ด (0) | 2023.07.02 |
[Baekjoon] 3584_๊ฐ์ฅ ๊ฐ๊น์ด ๊ณตํต ์กฐ์ (0) | 2023.06.29 |
[Baekjoon] 2036_์์ด์ ์ ์ (0) | 2023.06.29 |
[Baekjoon] 1789_์๋ค์ ํฉ (0) | 2023.06.28 |