Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4-wnsmir #13

Merged
merged 1 commit into from
Nov 12, 2024
Merged

4-wnsmir #13

merged 1 commit into from
Nov 12, 2024

Conversation

wnsmir
Copy link
Collaborator

@wnsmir wnsmir commented Oct 6, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/2448

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

50m

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ณ„์ฐ๊ธฐ๋ฌธ์ œ๋Š” ํ•œ๋ฒˆ์”ฉ ๋‹ค ํ’€์–ด๋ณด์…จ์„๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ธฐ์ดˆ๋ ˆ๋ฒจ์˜ ๋‹ค์ค‘๋ฐ˜๋ณต๋ฌธ์„ ํ• ๋•Œ๋ผ ์ถ”์ธกํ•ด๋ณด๋Š”๋ฐ์š”, ์ด ๋ฌธ์ œ๋Š” ํ”„๋ ‰ํƒˆ๊ณผ ๊ฐ™์ด ํŒจํ„ด์ด ๋ฐ˜๋ณต๋˜์–ด ๋‚˜ํƒ€๋‚˜๋Š” ๊ทœ์น™์„ ๋ณด์—ฌ์ฃผ๊ณ ์žˆ์Šต๋‹ˆ๋‹ค.

image

์ด๋Ÿฐ ๋ชจ์–‘์ธ๋ฐ ์ข€ ์˜ค๋ž˜ ๊ณ ๋ฏผํ•ด๋ณด๋‹ค ์žฌ๊ท€๋กœ ํ’€์ž๊ณ  ๋งˆ์Œ๋จน์—ˆ์Šต๋‹ˆ๋‹ค.

base๋ฅผ 3์œผ๋กœ ํ•˜๋Š”๋ฐ, ์ด์œ ๋Š” ์ œ์ผ ์ž‘์€ ์‚ผ๊ฐํ˜•์ด [' * ', ' * * ', '*****'] 3์ค„๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

  1. ํ•จ์ˆ˜ ์ •์˜
  2. base์กฐ๊ฑด ์„ค์ • if n == 3:
  3. list์—์„œ base์˜ ๊ตฌ์„ฑ์š”์†Œ 3๊ฐœ๋ฅผ ์•ž๋’ค๋กœ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด ๊ฐ€์šด๋ฐ์ •๋ ฌ ํ•ด์ค๋‹ˆ๋‹ค.
  4. ๋‘๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ base์‚ผ๊ฐํ˜• ๋‘๊ฐœ๋ฅผ ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ํ•˜๋‚˜ ๋‘๊ณ  ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

์žฌ๊ท€๋Š” ์ดํ•ด๊ฐ€ ๋ง๋กœ๋Š” ํž˜๋“ค์–ด n==6์ผ๋–„ result๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ์ž๋ฉด

[
" * ",
" * * ",
" ***** ",
" * * ",
" * * * * ",
"***** *****"
]
์ด์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ฆ‰ nใ…‡ 12๋ผ๋ฉด, ์ €์ƒํƒœ์—์„œ ๋‹ค์‹œ ์ฒซ for๋ฌธ์„ ๋Œ์•„ ์ € ์‚ผ๊ฐํ˜• 3๊ฐœ์— ๊ณต๋ฐฑ์ด ์–‘์ชฝ์— ์ถ”๊ฐ€๋˜์–ด ์ค‘๊ฐ„์ •๋ ฌ๋˜๊ณ , ๋ฐ‘์— ์ € ๊ฒฐ๊ณผ ์ž์ฒด๊ฐ€ ๊ณต๋ฐฑ์„ ํ•˜๋‚˜ ๋‘๊ณ  ๋ฐ‘์— ๋‘๊ฐœ๊ฐ€ ๊น”๋ฆฌ๊ฒŒ ๋˜๋Š”๊ฒƒ์ž…๋‹ˆ๋‹ค.

์žฌ๊ท€์™€ ๋ถ„ํ• ์ •๋ณต ์„นํ„ฐ์— ๋ถ„๋ฅ˜ํ•ด์„œ ๋‘์—ˆ๋Š”๋ฐ์š”, ์ œ๊ท€๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฐ€์žฅ์ž‘์€ n==3์ผ๋•Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ์ฐจ ๋” ํฐ ์‚ผ๊ฐํ˜•์„ ๋งŒ๋“ค์–ด๋‚˜๊ฐ€๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์–ด๋– ํ•œ ํŒจํ„ด์ด ๋ฐ˜๋ณต๋˜๋Š”๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ธ๋‹ค๋ฉด, ์žฌ๊ท€์™€ ๋ถ„ํ• ์ •๋ณต์„ ๋– ์˜ฌ๋ ค ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์‹œ์ž‘ํ•˜๊ธฐ ์ „๊นŒ์ง€์˜ ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ•˜๋Š”๊ฒŒ ํ•ต์‹ฌ์ด๋ผ ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

์žฌ๊ท€๋Š” ํ•œ๋ฒˆ ์ดํ•ด๋งŒ ํ•˜๋ฉด ์ดํ›„๋กœ๋Š” ๊ฐ€์žฅ ๊น”๋”ํ•œ ํ’€์ด๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ฒ•์ด๋ผ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ํŒจํ„ด๋ฐ˜๋ณต -> ์žฌ๊ท€, ๋ถ„ํ• ์ •๋ณต ๋‹ค์Œ ์ด๋Ÿฐ๋ฌธ์ œ๋ฅผ ๋ดค์„๋•Œ ์ด๊ฑธ ๋– ์˜ฌ๋ ค ์‹œ๊ฐ„์„ ๋‹จ์ถ•ํ–ˆ๋‹ค๋ฉด, ์ด๋ฌธ์ œ๊ฐ€ ์ €์—๊ฒŒ ์ค€ ๊ฐ€๋ฅด์นจ์€ ๋‹คํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ค€์šฉ๋‹˜ ์ฝ”๋“œ๋ณด๊ณ  ์ž‘์„ฑํ•œ๊ฑด ๋ญ”๊ฐ€ c++ ์Šค๋Ÿฝ์ง€์•Š์€? ๋Š๋‚Œ์ด ์žˆ์–ด์„œ ์ข€ ์ฐพ์•„๋ดค์Šต๋‹ˆ๋‹ค.

์ „์ฒด ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋†“๊ณ , ๊ณ„์‚ฐ์— ๋”ฐ๋ผ ๋ณ„์„ ์ง์ ‘ ์ฐ๋Š” ๋กœ์ง์ด ๋Œ€๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ๋ฅผ ๋ณด๊ณ ๋Š” c++์Šคํƒ€์ผ์ด ๋” ์ž˜ ์ดํ•ด๋˜์—ˆ๊ณ , ๋…ผ๋ฆฌ์ ์œผ๋กœ๋Š” ์ค€์šฉ๋‹˜ ์Šคํƒ€์ผ์ด ๋” ์ž˜ ์ดํ•ด๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋ณ„์ฐ๊ธฐ c++ ์ฝ”๋“œ
#include <iostream>
#include <cstring>

using namespace std;

// ๋†’์ด = n, ๋ฐ‘๋ณ€ = 2n-1
char board[3072][6144];

void draw(int x, int y) {
    board[x + 0][y + 0] = '*';
    board[x + 1][y - 1] = '*';
    board[x + 2][y - 2] = '*';
    board[x + 2][y - 1] = '*';
    board[x + 2][y + 0] = '*';
    board[x + 2][y + 1] = '*';
    board[x + 2][y + 2] = '*';
    board[x + 1][y + 1] = '*';
}

void star(int N, int x, int y) {
    if (N == 3) draw(x, y);
    else {
        star(N / 2, x, y);
        star(N / 2, x + N / 2, y - N / 2);
        star(N / 2, x + N / 2, y + N / 2);
    }
}

int main() {
    // 1) input
    int N;
    cin >> N;

    // 2) solution
    memset(board, ' ', sizeof(board));
    star(N, 0, N - 1);

    for (int y = 0; y < N; y++) {
        for (int x = 0; x < 2 * N - 1; x++) cout << board[y][x];
        cout << endl;
    }

    return 0;
}

์ €๊ธฐ ๋ณด์ด๋Š” ๊ธฐ๊ดดํ•œ drawํ•จ์ˆ˜... ๊ฒ๋จน์ง€ ๋ง๊ณ  ์ฒœ์ฒœํžˆ ๋ณด์‹œ๋ฉด ์ดํ•ด๊ฐ€ ๋น ๋ฅผ ๊ฒ๋‹ˆ๋‹ค.

https://nim-code.tistory.com/85
์—ฌ๊ธฐ ์ฐธ๊ณ  ๋งŽ์ด ํ–ˆ์–ด์š”.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ๋ณ„์„ ๊ธฐ์ค€์œผ๋กœ ์–‘์ชฝ์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ ๋ฐฐ์—ด์„ ์™„์„ฑํ•ด๋‚˜๊ฐ€๋Š” ๊ฑธ ์ƒ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค....
c++๋กœ ๋„ˆ๋ฌด ์–ด๋ ต๋”๋ผ๊ณ ์š”..

for star in stars:
        result.append(' ' * (n // 2) + star + ' ' * (n // 2))

ํŠนํžˆ ์ด๋Ÿฐ ์—ฐ์‚ฐ cpp์—์„œ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๊ฐ์ด ์•ˆ์žกํ˜”์Šต๋‹ˆ๋‹ค...

for (const string& star : stars) {
        string spaces(n / 2, ' '); // n 
        result.push_back(spaces + star + spaces);
    }

์š”๋Ÿฐ์‹์œผ๋กœ ํ•ด๋ณด๋‹ค๋ณด๋‹ˆ ๊ฐ์€ ์žกํ˜”๋Š”๋ฐ....
๊ณ ๋ฏผํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์—„์ฒญ ์˜ค๋ž˜ ๊ฑธ๋ ธ์Šต๋‹ˆ๋‹ค.

๋ณ„์ฐ๊ธฐ.. ์šฐ์Šต๊ฒŒ ๋ดค๋Š”๋ฐ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„, ๊ตฌํ˜„์˜ ์–ด๋ ค์›€์ด ๋งŽ์€ ๋ฌธ์ œ์˜€๋„ค์š”...

๋ณ„์ฐ๊ธฐ c++ ์ „์ฒด ์ฝ”๋“œ
#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<string> draw_star(int n) {
    if (n == 3) return {"  *  ", " * * ", "*****"};

    vector<string> stars = draw_star(n / 2);
    vector<string> result;

    for (const string& star : stars) {
        string spaces(n / 2, ' '); // n 
        result.push_back(spaces + star + spaces);
    }

    for (const string& star : stars) {
        result.push_back(star + ' ' + star);
    }

    return result;
}

int main() {
    int N;
    cin >> N;

    vector<string> result = draw_star(N);

    for (const string& line : result) 
        cout << line << endl;

    return 0;
}

Copy link
Collaborator

@g0rnn g0rnn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ณ„์ฐ๊ธฐ๋„ ์–ด๋ ค์šด๊ฒŒ ๋งŽ๊ตฐ์š”.. ๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค~

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ œ ์ฝ”๋“œ ์ž…๋‹ˆ๋‹น

code
#include <iostream>
#include <vector>
#include <string>
using namespace std;

vector<string> catchStars(int n) {
	if (n == 3) {
		return { "  *  ", " * * ", "*****" };
	}

	// ํ•œ ๋‹จ๊ณ„ ์ „์˜ stars๋“ค์„ ๋ฐ›์Œ
	vector<string> stars = catchStars(n/2);
	vector<string> triple_stars;

	// ์ฒซ๋ฒˆ์งธ ์‚ผ๊ฐํ˜• ์ƒ์„ฑ
	for (auto s : stars) {
		string line;
		line.append(n/2, ' ');
		line.append(s);
		line.append(n/2, ' ');
		triple_stars.push_back(line);
	}

	// ๋‘, ์„ธ๋ฒˆ์งธ ์‚ผ๊ฐํ˜• ์ƒ์„ฑ
	for (auto s : stars) {
		string line;
		line.append(s);
		line.append(" "); // ๋‘๋ฒˆ์งธ์™€ ์„ธ๋ฒˆ์งธ ์‚ผ๊ฐํ˜• ์‚ฌ์ด ๊ณต๋ฐฑ ์ถ”๊ฐ€
		line.append(s);
		triple_stars.push_back(line);
	}

	return triple_stars;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n;
	cin >> n;
	vector<string> stars = catchStars(n);
	for (auto& s : stars)
		cout << s << '\n';
	return 0;
}

@kokeunho
Copy link
Collaborator

kokeunho commented Nov 7, 2024

์ค€์šฉ๋‹˜ ์„ค๋ช…์ด๋ž‘ ๋‹ค๋ฅธ ์ž๋ฃŒ๋“ค ๋ณด๋ฉด์„œ ํ•œ ๋ฒˆ ํ’€์–ด๋ดค๋Š”๋ฐ ์žฌ๋ฐŒ์—ˆ์Šต๋‹ˆ๋‹ค.
์ž๋ฐ”๋กœ ์–ธ์–ด๋ฅผ ๋ฐ”๊พธ์—ˆ๋Š”๋ฐ ๊ตฌํ˜„ ์—ฐ์Šต์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ์˜€์Šต๋‹ˆ๋‹ค.

java code
import java.util.Scanner;

public class Main {
    static char[][] board;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();

        board = new char[N][N*2-1];
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N*2-1; j++) {
                board[i][j] = ' ';
            }
        }

        draw_star(0, N -1, N);

        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N*2 -1; j++) {
                sb.append(board[i][j]);
            }
            sb.append('\n');
        }
        System.out.print(sb.toString());
    }

    static void draw_star(int x, int y, int n) {
        if(n==3) {
            board[x][y] = '*';
            board[x+1][y-1] = '*';
            board[x+1][y+1] = '*';
            board[x+2][y-2] = '*';
            board[x+2][y-1] = '*';
            board[x+2][y] = '*';
            board[x+2][y+1] = '*';
            board[x+2][y+2] = '*';
            return;
        }

        int mid_tri = n/2;
        draw_star(x, y, mid_tri);
        draw_star(x+mid_tri, y-mid_tri, mid_tri);
        draw_star(x+mid_tri, y+mid_tri, mid_tri);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants