๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1895_ํ•„ํ„ฐ

๋ฟŒ์•ผ._. 2024. 1. 5. 12:04

Silver IV

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

< ํ•„ํ„ฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

์ด๋ฏธ์ง€์— 3x3 ํฌ๊ธฐ์˜ ํ•„ํ„ฐ๋ฅผ ๋‘๊ณ  ์›€์ง์ด๋ฉด์„œ ์ค‘์•™๊ฐ’์„ ์ฐพ๋Š”๋‹ค.

 

* ์ด ๋ฌธ์ œ์—์„œ T๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒƒ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ค‘์•™๊ฐ’๋“ค์„ ๋”ฐ๋กœ ์ €์žฅํ•  ํ•„์š” ์—†์ด ์ค‘์•™๊ฐ’์„ ํ•˜๋‚˜ ๊ตฌํ•  ๋•Œ๋งˆ๋‹ค T์™€ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋ฉด ๋œ๋‹ค.

 

 my solution (Java)

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

public class _1895_ { // ํ•„ํ„ฐ

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

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

		int arr[][] = new int[r][c];

		for (int i = 0; i < r; i++) {
			st = new StringTokenizer(bf.readLine());
			for (int j = 0; j < c; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
			}
		}

		int t = Integer.parseInt(bf.readLine());

		ArrayList<Integer> list = new ArrayList<>();
		int result = 0;

		for (int i = 0; i <= r - 3; i++) {
			for (int j = 0; j <= c - 3; j++) {
				for (int x = i; x < i + 3; x++) {
					for (int y = j; y < j + 3; y++) {
						list.add(arr[x][y]);
					}
				}
				Collections.sort(list);
				if (list.get(4) >= t) {
					result += 1;
				}
				list.clear();
			}
		}
		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
r, c : ์ด๋ฏธ์ง€ ํฌ๊ธฐ
arr : ํ”ฝ์…€ ๊ฐ’
t : T ๊ฐ’
list : 3x3 ๊ตฌ๊ฐ„์˜ ๊ฐ’
result : T๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒƒ์˜ ๊ฐœ์ˆ˜

 

์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ํ”ฝ์…€ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์™ผ์ชฝ ์œ„๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ 3x3 ํ•„ํ„ฐ๋ฅผ ์›€์ง์ด๋ฉฐ 3x3 ๋ฒ”์œ„ ์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ list์— ์ €์žฅํ•œ๋‹ค. list๋ฅผ ์ •๋ ฌ ํ›„ ๊ฐ€์šด๋ฐ ๊ฐ’์ธ 4๋ฒˆ์งธ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. ์ด ๊ฐ’์ด T๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด ์ •๋‹ต์ด๋ฏ€๋กœ result+1์„ ํ•ด์ค€๋‹ค.

 

์ตœ์ข… ์ •๋‹ต์ธ result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.