์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step10] 2447 : ๋ณ„ ์ฐ๊ธฐ - 10 (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 20. 16:11

www.acmicpc.net/problem/2447

 

2447๋ฒˆ: ๋ณ„ ์ฐ๊ธฐ - 10

์žฌ๊ท€์ ์ธ ํŒจํ„ด์œผ๋กœ ๋ณ„์„ ์ฐ์–ด ๋ณด์ž. N์ด 3์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ(3, 9, 27, ...)์ด๋ผ๊ณ  ํ•  ๋•Œ, ํฌ๊ธฐ N์˜ ํŒจํ„ด์€ N×N ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋‹ค. ํฌ๊ธฐ 3์˜ ํŒจํ„ด์€ ๊ฐ€์šด๋ฐ์— ๊ณต๋ฐฑ์ด ์žˆ๊ณ , ๊ฐ€์šด๋ฐ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์นธ์— ๋ณ„์ด

www.acmicpc.net


๋ฌธ์ œ

์žฌ๊ท€์ ์ธ ํŒจํ„ด์œผ๋กœ ๋ณ„์„ ์ฐ์–ด ๋ณด์ž. N์ด 3์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ(3, 9, 27, ...)์ด๋ผ๊ณ  ํ•  ๋•Œ, ํฌ๊ธฐ N์˜ ํŒจํ„ด์€ N×N ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋‹ค.

ํฌ๊ธฐ 3์˜ ํŒจํ„ด์€ ๊ฐ€์šด๋ฐ์— ๊ณต๋ฐฑ์ด ์žˆ๊ณ , ๊ฐ€์šด๋ฐ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์นธ์— ๋ณ„์ด ํ•˜๋‚˜์”ฉ ์žˆ๋Š” ํŒจํ„ด์ด๋‹ค.

***

*  *

***

N์ด 3๋ณด๋‹ค ํด ๊ฒฝ์šฐ, ํฌ๊ธฐ N์˜ ํŒจํ„ด์€ ๊ณต๋ฐฑ์œผ๋กœ ์ฑ„์›Œ์ง„ ๊ฐ€์šด๋ฐ์˜ (N/3)×(N/3) ์ •์‚ฌ๊ฐํ˜•์„ ํฌ๊ธฐ N/3์˜ ํŒจํ„ด์œผ๋กœ ๋‘˜๋Ÿฌ์‹ผ ํ˜•ํƒœ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํฌ๊ธฐ 27์˜ ํŒจํ„ด์€ ์˜ˆ์ œ ์ถœ๋ ฅ 1๊ณผ ๊ฐ™๋‹ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 3์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ์ด๋‹ค. ์ฆ‰ ์–ด๋–ค ์ •์ˆ˜ k์— ๋Œ€ํ•ด N=3k์ด๋ฉฐ, ์ด๋•Œ 1 ≤ k < 8์ด๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๋ณ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1

27

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

 


ํ’€์ด

( ์•„๋‹ˆ ๋ฌธ์ œ์—๋Š” N=3k์ด๋ฉฐ, ์ด๋•Œ 1 ≤ k < 8 ์ด๋ผ๊ณ  ๋‚˜์™€์„œ 1์ด ์ž…๋ ฅ๋˜๋Š” ๊ฑฐ๋Š” ์ „ํ˜€ ์ƒ๊ฐ๋„ ์•ˆํ•˜๊ณ  ๊ณ ๋ ค๋„ ์•ˆํ–ˆ๋Š”๋ฐ ๋ญ ์–ด๋–ค ๋ฐฉ๋ฒ•์„ ์จ๋„ ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์„œ ๋ญ์ง€ ํ•˜๊ณ  ์ฐพ์•„๋ดค๋”๋‹ˆ 1์ด ์ž…๋ ฅ๋˜๋ฉด ๋ณ„ ํ•˜๋‚˜๊ฐ€ ์ถœ๋ ฅ๋˜์•ผ ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.... ๊ทธ๋ž˜์„œ ๊ทธ๊ฒƒ๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ ์—†์ด ์ •๋‹ต... ๋„ˆ๋ฌด ํ™”๋‚œ๋‹ค... )

n์€ 3์˜ ๋ฐฐ์ˆ˜๋กœ ์ž…๋ ฅ๋˜๊ณ , n*n ํฌ๊ธฐ์˜ ์‚ฌ๊ฐํ˜•์œผ๋กœ *์ด ์ถœ๋ ฅ๋œ๋‹ค.

์ „์ฒด ์‚ฌ๊ฐํ˜•์„ 9๊ฐœ๋กœ ๋‚˜๋ˆด์„ ๋•Œ, ์ • ๊ฐ€์šด๋ฐ๋Š” ํ•ญ์ƒ ๋นˆ์นธ, ๋‚˜๋จธ์ง€ 8๊ฐœ ๋ถ€๋ถ„์€ ๋˜ 9๊ฐœ๋กœ ๋‚˜๋ˆ ์ ธ ๊ฐ™์€ ๊ทœ์น™์„ ๋ฐ˜๋ณตํ•œ๋‹ค => ์žฌ๊ท€ํ•จ์ˆ˜

 

์ฒ˜์Œ์—๋Š” ํ•œ์ค„ํ•œ์ค„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ฑธ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๊ทธ๋Ÿฐ ๋ฐฉ๋ฒ•์€ ๋„์ €ํžˆ ๋– ์˜ค๋ฅด์ง€ ์•Š์•˜๋‹ค. n*nํฌ๊ธฐ์˜ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด *๊ณผ ๊ณต๋ฐฑ์„ ์ž…๋ ฅํ•˜๋Š”๊ฒŒ ๋‚ซ๊ฒ ๋‹ค ์‹ถ์—ˆ๋‹ค

 

๊ฐ€์žฅ ์ž‘์€ ๋ฒ”์œ„๋Š” n์ด 3์ผ ๋•Œ ๋…ธ๋ž€์ƒ‰ ๋„ค๋ชจ ๋ถ€๋ถ„์ฒ˜๋Ÿผ ์ถœ๋ ฅ๋  ๋•Œ์ด๋‹ค : ์ตœ์†Œ ์‚ฌ๊ฐํ˜•

 

void Star(String[][] star, int n, int x, int y)

๋ฐฐ์—ด star์™€ n, ์™ผ์ชฝ ๊ผญ๋Œ€๊ธฐ์˜ ์ขŒํ‘œ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ์œ„์น˜์— ์ตœ์†Œ ์‚ฌ๊ฐํ˜•์„ ์ €์žฅ

   1. n == 3 ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ํ•ด๋‹น ์œ„์น˜์— ์ตœ์†Œ ์‚ฌ๊ฐํ˜•์„ ์ €์žฅ,

   2. n != 3 ์ด๋ฉด n/3์™€ ๋ณ€๊ฒฝ๋œ x, y๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์žฌํ˜ธ์ถœํ•œ๋‹ค.

 

void Blank(String[][] star, int n, int x, int y)

๋ฐฐ์—ด๊ณผ n, ์™ผ์ชฝ ๊ผญ๋Œ€๊ธฐ์˜ ์ขŒํ‘œ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ํ•ด๋‹น ์œ„์น˜์— n*n ํฌ๊ธฐ์˜ ๊ณต๋ฐฑ์„ ์ €์žฅ

 

 

1. n์ด ์ž…๋ ฅ๋˜๋ฉด Star(star, n, 0, 0)์„ ํ˜ธ์ถœ

2. Star ๋‚ด๋ถ€์—์„œ n == 3 ์ธ์ง€ ๊ฒ€์‚ฌ, 3์ด ์•„๋‹ˆ๋ผ๋ฉด n/3 ํฌ๊ธฐ์˜ Star์„ ์žฌํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ด 9๋ถ€๋ถ„์œผ๋กœ ์ชผ๊ฐœ์ง€๊ธฐ ๋•Œ๋ฌธ์— n/3 ํฌ๊ธฐ์˜ ์‚ฌ๊ฐํ˜•์„ 9๋ฒˆ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.

๋˜, 5๋ฒˆ์งธ ์‚ฌ๊ฐํ˜•์€ ๋ชจ๋‘ ๊ณต๋ฐฑ์ด์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— conut๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ ์„ ์–ธํ•ด์„œ count๊ฐ€ 5์ผ๋•Œ๋Š” Blank ํ•จ์ˆ˜๋ฅผ, ๊ทธ ์™ธ์—๋Š” Star ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค.

9๋ฒˆ ํ˜ธ์ถœ์‹œ x,y๋ฅผ (0, 0) , (0, n/3) , (0, 2n/3) , (n/3, 0) , (n/3, n/3) , (n/3, 2n/3) , (2n/3, 0) , (2n/3, n/3) , (2n/3, 2n/3)์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— for๋ฌธ์˜ ์กฐ๊ฑด์„ for(int i = x; i < x + n; i += n/3) / for(int j = y; j < y + n; j += n/3) ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

 

์žฌ๊ท€ํ˜ธ์ถœ

 

import java.io.*;
import java.util.Scanner;

public class B2447 {
	
	static void Star(String[][] star, int n, int x, int y) {
		if(n == 3) {
			for(int i = x; i < x+3; i ++) {
				for(int j = y; j < y+3; j ++) {
					star[i][j] = "*";
				}
			}
			star[x+1][y+1] = " ";
		} else {
			int count = 1;
			for(int i = x; i < x + n; i += n/3) {
				for(int j = y; j < y + n; j += n/3) {
					if(count == 5) {
						Blank(star, n/3, i, j);
					} else {
						Star(star, n/3, i, j);
					}
					count++;
				}
			}
		}	
	}
	
	static void Blank(String[][] star, int n, int x, int y) {
		for(int i = x; i < x + n; i ++) {
			for(int j = y; j < y + n; j ++) {
				star[i][j] = " ";
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int n = sc.nextInt();
		if(n == 1) {
			System.out.println("*");
			return;
		}
		
		String[][] star = new String[n][n];
		
		Star(star, n, 0, 0);		
			
		for(int i = 0; i < n; i ++) {
			for(int j = 0; j < n; j ++) {
				bw.write(star[i][j]);
			}
			bw.newLine();
		}
		bw.flush();
	}
}