๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 2799_๋ธ”๋ผ์ธ๋“œ

๋ฟŒ์•ผ._. 2024. 1. 25. 10:48

Silver IV

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

< ๋ธ”๋ผ์ธ๋“œ >

 

๋ฌธ์ œ ํ’€์ด 

 

4x4 ๊ตฌ๊ฐ„์„ ํƒ์ƒ‰ํ•ด์„œ ์ฐฝ๋ฌธ์˜ ์ข…๋ฅ˜๋ฅผ ํ™•์ธํ•œ๋‹ค.

 

 

 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 _2799_ { // ๋ธ”๋ผ์ธ๋“œ

	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 arr[][] = new int[5 * m + 1][5 * n + 1];
		int result[] = new int[5];

		for (int i = 0; i < 5 * m + 1; i++) {
			String str = bf.readLine();
			for (int j = 0; j < 5 * n + 1; j++) {
				arr[i][j] = str.charAt(j);
			}
		}

		for (int j = 0; j < 5 * n; j += 5) {
			for (int i = 0; i < 5 * m; i += 5) {
				int cnt = 0;
				for (int k = i + 1; k < i + 6; k++) {
					if (arr[k][j + 1] == '*') {
						cnt += 1;
					}
				}
				result[cnt] += 1;
			}
		}

		for (int i = 0; i < 5; i++) {
			bw.write(result[i] + " ");
		}
		bw.flush();
	}

}
๋ณ€์ˆ˜)
m, n : m์ธต n๊ฐœ์˜ ์ฐฝ๋ฌธ
arr : ์•„ํŒŒํŠธ์˜ ์ƒํƒœ
result : ๊ฐ ๋ธ”๋ผ์ธ๋“œ ํƒ€์ž… ๊ฐœ์ˆ˜
cnt : ํ•œ ์ฐฝ๋ฌธ์˜ ๋ธ”๋ผ์ธ๋“œ ํƒ€์ž…

 

๊ฑด๋ฌผ์˜ ์ธต ์ˆ˜์™€ ์ฐฝ๋ฌธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

์•„ํŒŒํŠธ์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ arr ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

๊ฐ ์ธต๋งˆ๋‹ค 4x4 ์ฐฝ๋ฌธ์„ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ธ”๋ผ์ธ๋“œ์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ด์„œ result ๋ฐฐ์—ด์— ์ €์žฅ ํ›„ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.