๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ ์•Œ๊ณ ๋ฆฌ์ฆ˜/Code Tree

[์ฝ”๋“œํŠธ๋ฆฌ] ๋‚˜๋ฌด๋ฐ•๋ฉธ - ์‹œ๋ฎฌ๋ ˆ์ด์…˜ (๊ธฐ์ถœ๋ฌธ์ œ)

728x90

์‚ผ์„ฑ SW ์—ญ๋Ÿ‰ํ…Œ์ŠคํŠธ 2022 ์ƒ๋ฐ˜๊ธฐ ๊ธฐ์ถœ 

๋‚˜๋ฌด๋ฐ•๋ฉธ

n * n ๊ฒฉ์ž์— ๋‚˜๋ฌด์˜ ๊ทธ๋ฃจ ์ˆ˜์™€ ๋ฒฝ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‚˜๋ฌด์˜ ์„ฑ์žฅ๊ณผ ๋ฒˆ์‹๋ ฅ์ด ์ข‹์•„์„œ ์ œ์ดˆ์ œ๋ฅผ ๋ฟŒ๋ ค ๋‚˜๋ฌด์˜ ์„ฑ์žฅ์„ ์–ต์ œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ์ œ์ดˆ์ œ์˜ ๊ฒฝ์šฐ k์˜ ๋ฒ”์œ„๋งŒํผ ๋Œ€๊ฐ์„ ์œผ๋กœ ํผ์ง€๋ฉฐ, ๋ฒฝ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐ€๋กœ๋ง‰ํ˜€์„œ ์ „ํŒŒ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ดˆ๊ธฐ ์กฐ๊ฑด์ด ์ฃผ์–ด์ง„๋‹ค๊ณ  ๊ฐ€์ •ํ•  ๋•Œ, 1๋…„๋™์•ˆ ๋‚˜๋ฌด์˜ ์„ฑ์žฅ๊ณผ ์–ต์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ด๋ค„์ง‘๋‹ˆ๋‹ค.

  1. ์ธ์ ‘ํ•œ ๋„ค ๊ฐœ์˜ ์นธ ์ค‘ ๋‚˜๋ฌด๊ฐ€ ์žˆ๋Š” ์นธ์˜ ์ˆ˜๋งŒํผ ๋‚˜๋ฌด๊ฐ€ ์„ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ์„ฑ์žฅ์€ ๋ชจ๋“  ๋‚˜๋ฌด์—๊ฒŒ ๋™์‹œ์— ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค.

(์ค‘๋žต)

 

๊ฐ 3๊ฐœ์˜ ๊ณผ์ •์ด 1๋…„์— ๊ฑธ์ณ ์ง„ํ–‰๋œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, m๋…„ ๋™์•ˆ ์ด ๋ฐ•๋ฉธํ•œ ๋‚˜๋ฌด์˜ ๊ทธ๋ฃจ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์„ธ์š”.

์ž…๋ ฅ ํ˜•์‹

์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ฒฉ์ž์˜ ํฌ๊ธฐ n, ๋ฐ•๋ฉธ์ด ์ง„ํ–‰๋˜๋Š” ๋…„ ์ˆ˜ m, ์ œ์ดˆ์ œ์˜ ํ™•์‚ฐ ๋ฒ”์œ„ k, ์ œ์ดˆ์ œ๊ฐ€ ๋‚จ์•„์žˆ๋Š” ๋…„ ์ˆ˜ c๊ฐ€ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

์ดํ›„ n๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ์นธ์˜ ๋‚˜๋ฌด์˜ ๊ทธ๋ฃจ ์ˆ˜, ๋ฒฝ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด ๋‚˜๋ฌด์˜ ๊ทธ๋ฃจ ์ˆ˜๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์˜ ์ˆ˜๋กœ, ๋นˆ ์นธ์€ 0, ๋ฒฝ์€ -1์œผ๋กœ ์ฃผ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

  • 5 ≤ n ≤ 20
  • 1 ≤ m ≤ 1000
  • 1 ≤ k ≤ 20
  • 1 ≤ c ≤ 10

์ถœ๋ ฅ ํ˜•์‹

m๋…„ ๋™์•ˆ ์ด ๋ฐ•๋ฉธํ•œ ๋‚˜๋ฌด์˜ ๊ทธ๋ฃจ ์ˆ˜๋ฅผ ๊ตฌํ•˜์„ธ์š”.


 

์ œ๊ณต๋œ ์˜ˆ์ œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๊ฐ€ ๋ชจ๋‘ ๋งž๊ธฐ๊นŒ์ง€ 1์‹œ๊ฐ„ ๋ฐ˜์ด ๊ฑธ๋ ธ๋‹ค.

๊ทผ๋ฐ ์ œ์ถœํ•˜๋‹ˆ ํ…Œ์ผ€ 7๋ฒˆ์—์„œ ํ‹€๋ฆผ!!

 

๊ทผ๋ฐ ์ž˜๋ชป๋œ๊ฑฐ ๋ช‡๊ฐœ ๊ณ ์ณค๋Š”๋ฐ ๊ณ„์† ๋‹ต์ด ์ž‘์€ ๋ฒ”์œ„๋กœ ํ‹€๋ฆฌ๊ฒŒ ๋‚˜์˜ค๋Š”๊ฑฐ์ž„

ใ…  3์‹œ๊ฐ„๋™์•ˆ ๊ณ„์† ๊ณ ์ณ๋„ ํ‹€๋ ค์„œ ๊ทธ๋ƒฅ ํฌ๊ธฐํ•˜๊ณ  ํ•ด์„ค์„ ๋ด๋ฒ„๋ ธ๋‹ค~~^^ ํ™”๋”ฑ์ง€๋‚˜

 

 

ํ•ด์„ค ํ’€์ด

#include <iostream>

#define MAX_N 20
#define DIR_NUM 4

using namespace std;

int n, m, k, c;
int tree[MAX_N + 1][MAX_N + 1];
int add_tree[MAX_N + 1][MAX_N + 1];
int herb[MAX_N + 1][MAX_N + 1];

int ans;

bool IsOutRange(int x, int y) {
    return !(1 <= x && x <= n && 1 <= y && y <= n);
}

// ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๋“ฑ ์ดˆ๊ธฐ ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค.
void Init() {
    cin >> n >> m >> k >> c;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
    		cin >> tree[i][j];
}

// 1๋‹จ๊ณ„ : ์ธ์ ‘ํ•œ ๋„ค ๊ฐœ์˜ ์นธ ์ค‘ ๋‚˜๋ฌด๊ฐ€ ์žˆ๋Š” ์นธ์˜ ์ˆ˜๋งŒํผ ๋‚˜๋ฌด๊ฐ€ ์„ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.
void StepOne() {
    int dx[DIR_NUM] = {-1,  0, 1, 0};
    int dy[DIR_NUM] = { 0, -1, 0, 1};

    for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) {
            if(tree[i][j] <= 0) continue;

            // ๋‚˜๋ฌด๊ฐ€ ์žˆ๋Š” ์นธ์˜ ์ˆ˜(cnt)๋งŒํผ ๋‚˜๋ฌด๊ฐ€ ์„ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.
            int cnt = 0;
			for(int dir = 0; dir < 4; dir++) {
                int nx = i + dx[dir];
                int ny = j + dy[dir];
                if(IsOutRange(nx, ny)) continue;
                if(tree[nx][ny] > 0) cnt++;
            }
            tree[i][j] += cnt;
        }
}

// 2๋‹จ๊ณ„ : ๊ธฐ์กด์— ์žˆ์—ˆ๋˜ ๋‚˜๋ฌด๋“ค์€ ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ์นธ์— ๋ฒˆ์‹์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
void StepTwo() {
    int dx[DIR_NUM] = {-1,  0, 1, 0};
    int dy[DIR_NUM] = { 0, -1, 0, 1};

    // ๋ชจ๋“  ๋‚˜๋ฌด์—์„œ ๋™์‹œ์— ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์„ ๋” ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
    // add_tree๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ค๋‹ˆ๋‹ค.
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) 
            add_tree[i][j] = 0;

    for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) {
            if(tree[i][j] <= 0) continue;

            // ํ•ด๋‹น ๋‚˜๋ฌด์™€ ์ธ์ ‘ํ•œ ๋‚˜๋ฌด ์ค‘ ์•„๋ฌด๋„ ์—†๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
            int cnt = 0;
            for(int dir = 0; dir < 4; dir++) {
                int nx = i + dx[dir];
                int ny = j + dy[dir];
                if(IsOutRange(nx, ny)) continue;
                if(herb[nx][ny]) continue;
                if(tree[nx][ny] == 0) cnt++;
            }

            // ์ธ์ ‘ํ•œ ๋‚˜๋ฌด ์ค‘ ์•„๋ฌด๋„ ์—†๋Š” ์นธ์€ cnt๋กœ ๋‚˜๋ˆ ์ค€ ๋งŒํผ ๋ฒˆ์‹ํ•ฉ๋‹ˆ๋‹ค.
            for(int dir = 0; dir < 4; dir++) {
                int nx = i + dx[dir];
                int ny = j + dy[dir];
                if(IsOutRange(nx, ny)) continue;
                if(herb[nx][ny]) continue;
                if(tree[nx][ny] == 0) add_tree[nx][ny] += tree[i][j] / cnt;
            }
        }
    
    // add_tree๋ฅผ ๋”ํ•ด ๋ฒˆ์‹์„ ๋™์‹œ์— ์ง„ํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
    for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) tree[i][j] += add_tree[i][j];
}

// 3๋‹จ๊ณ„ : ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ•๋ฉธ๋˜๋Š” ์นธ์— ์ œ์ดˆ์ œ๋ฅผ ๋ฟŒ๋ฆฝ๋‹ˆ๋‹ค.
void StepThree() {
    int dx[DIR_NUM] = {-1,  1, 1, -1};
    int dy[DIR_NUM] = {-1, -1, 1,  1};

    int max_del = 0;
    int max_x = 1;
    int max_y = 1;

    for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) {
            // ๋ชจ๋“  ์นธ์— ๋Œ€ํ•ด ์ œ์ดˆ์ œ๋ฅผ ๋ฟŒ๋ ค๋ด…๋‹ˆ๋‹ค. ๊ฐ ์นธ์—์„œ ์ œ์ดˆ์ œ๋ฅผ ๋ฟŒ๋ฆด ์‹œ ๋ฐ•๋ฉธ๋˜๋Š” ๋‚˜๋ฌด์˜ ๊ทธ๋ฃจ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ ,
            // ์ด ๊ฐ’์ด ์ตœ๋Œ€๊ฐ€ ๋˜๋Š” ์ง€์ ์„ ์ฐพ์•„์ค๋‹ˆ๋‹ค.
            if(tree[i][j] <= 0) continue;
            int cnt = tree[i][j];
            for(int dir = 0; dir < 4; dir++) {
                int nx = i;
                int ny = j;
				for(int x = 1; x <= k; x++) {
                    nx = nx + dx[dir];
                    ny = ny + dy[dir];
                    if(IsOutRange(nx, ny)) break;
                    if(tree[nx][ny] <= 0) break;
                    cnt += tree[nx][ny];
                }
            }
            if(max_del < cnt) {
                max_del = cnt;
                max_x = i;
                max_y = j;
            }
        }

    ans += max_del;

    // ์ฐพ์€ ์นธ์— ์ œ์ดˆ์ œ๋ฅผ ๋ฟŒ๋ฆฝ๋‹ˆ๋‹ค.
    if(tree[max_x][max_y] > 0) {
        tree[max_x][max_y] = 0;
        herb[max_x][max_y] = c;
        for(int dir = 0; dir < 4; dir++) {
            int nx = max_x;
            int ny = max_y;
            for(int x = 1; x <= k; x++) {
                nx = nx + dx[dir];
                ny = ny + dy[dir];
                if(IsOutRange(nx, ny)) break;
                if(tree[nx][ny] < 0) break;
                if(tree[nx][ny] == 0) {
                    herb[nx][ny] = c;
                    break;
                }
                tree[nx][ny] = 0;
                herb[nx][ny] = c;
            }
        }
    }
}

// ์ œ์ดˆ์ œ์˜ ๊ธฐ๊ฐ„์„ 1๋…„ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค.
void DeleteHerb() {
    for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++) 
            if(herb[i][j] > 0) 
                herb[i][j] -= 1;
}

int main() {
    // ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๋“ฑ ์ดˆ๊ธฐ ์ž‘์—…์„ ํ•ฉ๋‹ˆ๋‹ค.
    Init();

    for(int i = 1; i <= m; i++) {
        // 1๋‹จ๊ณ„ : ์ธ์ ‘ํ•œ ๋„ค ๊ฐœ์˜ ์นธ ์ค‘ ๋‚˜๋ฌด๊ฐ€ ์žˆ๋Š” ์นธ์˜ ์ˆ˜๋งŒํผ ๋‚˜๋ฌด๊ฐ€ ์„ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.
        StepOne();

        // 2๋‹จ๊ณ„ : ๊ธฐ์กด์— ์žˆ์—ˆ๋˜ ๋‚˜๋ฌด๋“ค์€ ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ์นธ์— ๋ฒˆ์‹์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
        StepTwo();

        // ์ œ์ดˆ์ œ์˜ ๊ธฐ๊ฐ„์„ 1๋…„ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค.
        DeleteHerb();

        // 3๋‹จ๊ณ„ : ๊ฐ€์žฅ ๋งŽ์ด ๋ฐ•๋ฉธ๋˜๋Š” ์นธ์— ์ œ์ดˆ์ œ๋ฅผ ๋ฟŒ๋ฆฝ๋‹ˆ๋‹ค.
        StepThree();
    }

    cout << ans;
    return 0;
}

 

728x90