๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/5549)
< ํ์ฑ ํ์ฌ >
๋ฌธ์ ํ์ด
๋ง์ฝ ์ผ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์ ๋ ฅ์ด ๋ค์ด์จ๋ค๋ฉด ์ค๋ฅธ์ชฝ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ๋์ ํฉ์ ๊ตฌํ๋ค.
๋์ ํฉ์ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ [4 = 3+2-1+4๋ฒ์ ๊ฐ]์ด๋ค.
๋์ ํฉ์ ๊ตฌํ ๋ฐฐ์ด์์ ๋นจ๊ฐ ๋ฐ์ค ์์ญ์ ๋์ด๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ [๋์ด = 12-10-4+2]์ด๋ค.
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 _5549_ { // ํ์ฑ ํ์ฌ
static class Planet {
private int j, o ,i;
public Planet(int j, int o, int i) {
this.j = j;
this.o = o;
this.i = i;
}
}
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());
int m = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(bf.readLine());
char arr[][] = new char[m + 1][n + 1];
Planet result[][] = new Planet[m + 1][n + 1];
for (int i = 0; i < m + 1; i++) {
for (int j = 0; j < n + 1; j++) {
result[i][j] = new Planet(0, 0, 0);
}
}
for (int i = 1; i <= m; i++) {
String str = bf.readLine();
for (int j = 1; j <= n; j++) {
arr[i][j] = str.charAt(j - 1);
result[i][j].j = result[i - 1][j].j + result[i][j - 1].j - result[i - 1][j - 1].j;
result[i][j].o = result[i - 1][j].o + result[i][j - 1].o - result[i - 1][j - 1].o;
result[i][j].i = result[i - 1][j].i + result[i][j - 1].i - result[i - 1][j - 1].i;
if (arr[i][j] == 'J') {
result[i][j].j += 1;
} else if (arr[i][j] == 'O') {
result[i][j].o += 1;
} else {
result[i][j].i += 1;
}
}
}
for (int i = 0; i < k; i++) {
st = new StringTokenizer(bf.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
Planet answer = new Planet(result[c][d].j, result[c][d].o, result[c][d].i);
answer.i -= result[c][b-1].i;
answer.j -= result[c][b-1].j;
answer.o -= result[c][b-1].o;
answer.i -= result[a-1][d].i;
answer.j -= result[a-1][d].j;
answer.o -= result[a-1][d].o;
answer.i += result[a-1][b-1].i;
answer.j += result[a-1][b-1].j;
answer.o += result[a-1][b-1].o;
bw.write(answer.j + " " + answer.o + " " + answer.i + "\n");
}
bw.flush();
}
}
๋ณ์)
m, n : ์ง๋์ ํฌ๊ธฐ
k : ์กฐ์ฌ ๋์ ์์ญ์ ๊ฐ์
arr : ์ง๋์ ๋ด์ฉ
result : ๋์ ํฉ
a, b, c, d : ์กฐ์ฌ ๋์ ์์ญ์ ์ ๋ณด
answer : ์กฐ์ฌ ๋์ ์์ญ์ ํฌํจ๋์ด ์๋ ์ ๊ธ, ๋ฐ๋ค, ์ผ์ ์
Planet : ์ ๊ธ, ๋ฐ๋ค, ์ผ์์ ์๋ฅผ ๊ฐ์ง๋ ํ์ฑ
์ง๋์ ํฌ๊ธฐ์ ์กฐ์ฌ ๋์ ์์ญ์ ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ๋์ ํฉ์ ๊ตฌํ๊ธฐ ์ํด result ๋ฐฐ์ด์ ๊ฐ์ Planet์ผ๋ก ์ด๊ธฐํํ๋ค. ์ง๋์ ๋ด์ฉ์ ์ ๋ ฅ๋ฐ์ arr ๋ฐฐ์ด์ ์ ์ฅํ๋ฉด์ ๋์ ํฉ์ ๊ตฌํ๋ค.
์กฐ์ฌ ๋์ ์์ญ์ ๊ฐ์๋งํผ ์กฐ์ฌ ๋์ ์์ญ์ ์ ๋ณด๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ์์์ ์ค๋ช ํ ๊ณต์๋๋ก ๊ฐ ์์ญ์ ํฌํจ๋์ด ์๋ ์ ๊ธ, ๋ฐ๋ค, ์ผ์์ ์๋ฅผ ๊ตฌํด ์ถ๋ ฅํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 12931_๋ ๋ฐฐ ๋ํ๊ธฐ (0) | 2024.03.18 |
---|---|
[Baekjoon] 16938_์บ ํ ์ค๋น (1) | 2024.03.15 |
[Baekjoon] 1990_์์์ธํฐ๋ฆฐ๋๋กฌ (0) | 2024.03.13 |
[Baekjoon] 1456_๊ฑฐ์ ์์ (0) | 2024.03.12 |
[Baekjoon] 5636_์์ ๋ถ๋ถ ๋ฌธ์์ด (0) | 2024.03.11 |