๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1051_์ˆซ์ž ์ •์‚ฌ๊ฐํ˜•

๋ฟŒ์•ผ._. 2024. 5. 9. 23:11

Silver III

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

< ์ˆซ์ž ์ •์‚ฌ๊ฐํ˜• >

 

๋ฌธ์ œ ํ’€์ด 

 

๊ฐ€๋Šฅํ•œ ์ •์‚ฌ๊ฐํ˜• ํฌ๊ธฐ๋งŒํผ ๊ผญ์ง“์ ์„ ์‚ดํŽด๋ณธ๋‹ค.

 

 my solution (Java)

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

public class _1051_ { // ์ˆซ์ž ์ •์‚ฌ๊ฐํ˜•

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

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

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

		int result = 1;

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				int size = 1;
				while (i + size < n && j + size < m) {

					if (arr[i][j] == arr[i + size][j] && arr[i][j] == arr[i][j + size]
							&& arr[i][j] == arr[i + size][j + size]) {
						result = Math.max(result, size + 1);
					}
					size += 1;
				}
			}
		}
		System.out.println(result * result);
	}
}
๋ณ€์ˆ˜)
n, m : ์ง์‚ฌ๊ฐํ˜• ํฌ๊ธฐ
arr : ์ง์‚ฌ๊ฐํ˜•์— ์“ฐ์—ฌ์žˆ๋Š” ์ˆ˜
result : ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• ํ•œ ๋ณ€์˜ ๊ธธ์ด
size : ํ˜„์žฌ ์ •์‚ฌ๊ฐํ˜• ํ•œ ๋ณ€์˜ ๊ธธ์ด

 

์ง์‚ฌ๊ฐํ˜• ํฌ๊ธฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์ง์‚ฌ๊ฐํ˜•์— ์“ฐ์—ฌ์žˆ๋Š” ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•œ๋‹ค. ์ง์‚ฌ๊ฐํ˜•์„ ์ „์ฒด ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ •์‚ฌ๊ฐํ˜•์„ ๋งŒ๋“ค ๋•Œ ๊ผญ์ง“์ ์— ์“ฐ์—ฌ ์žˆ๋Š” ์ˆ˜๊ฐ€ ๋ชจ๋‘ ๊ฐ™์€์ง€ ํŒ๋‹จํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜•์˜ ๊ธธ์ด๋ฅผ ์ฐพ๋Š”๋‹ค.

 

์ตœ์ข… ์ •์‚ฌ๊ฐํ˜• ํฌ๊ธฐ์ธ result*result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.