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

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

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

728x90

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

์‹ธ์›€๋•…

์ธ๊ธฐ ๊ฒŒ์ž„์ธ ์‹ธ์›€๋•…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ฒŒ์ž„์€ n * n ํฌ๊ธฐ์˜ ๊ฒฉ์ž์—์„œ ์ง„ํ–‰๋˜๋ฉฐ, ๊ฐ๊ฐ์˜ ๊ฒฉ์ž์—๋Š” ๋ฌด๊ธฐ๋“ค์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดˆ๊ธฐ์—๋Š” ๋ฌด๊ธฐ๋“ค์ด ์—†๋Š” ๋นˆ ๊ฒฉ์ž์— ํ”Œ๋ ˆ์ด์–ด๋“ค์ด ์œ„์น˜ํ•˜๋ฉฐ ๊ฐ ํ”Œ๋ ˆ์ด์–ด๋Š” ์ดˆ๊ธฐ ๋Šฅ๋ ฅ์น˜๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ๊ฐ ํ”Œ๋ ˆ์ด์–ด์˜ ์ดˆ๊ธฐ ๋Šฅ๋ ฅ์น˜๋Š” ๋ชจ๋‘ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ฒŒ์ž„์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ๋นจ๊ฐ„์ƒ‰ ๋ฐฐ๊ฒฝ์˜ ์ˆซ์ž๋Š” ์ด์˜ ๊ฒฝ์šฐ ๊ณต๊ฒฉ๋ ฅ์„, ํ”Œ๋ ˆ์ด์–ด์˜ ๊ฒฝ์šฐ ์ดˆ๊ธฐ ๋Šฅ๋ ฅ์น˜๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ, ๋…ธ๋ž€์ƒ‰ ๋ฐฐ๊ฒฝ์˜ ์ˆซ์ž๋Š” ํ”Œ๋ ˆ์ด์–ด์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

ํ•˜๋‚˜์˜ ๋ผ์šด๋“œ๋Š” ๋‹ค์Œ์˜ ๊ณผ์ •์— ๊ฑธ์ณ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค.

 

1-1. ์ฒซ ๋ฒˆ์งธ ํ”Œ๋ ˆ์ด์–ด๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ณธ์ธ์ด ํ–ฅํ•˜๊ณ  ์žˆ๋Š” ๋ฐฉํ–ฅ๋Œ€๋กœ ํ•œ ์นธ๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ ๋‚˜๊ฐˆ ๋•Œ ๊ฒฉ์ž๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ •๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ๋ฐฉํ–ฅ์„ ๋ฐ”๊พธ์–ด์„œ 1๋งŒํผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

(์ค‘๋žต)

 

k ๋ผ์šด๋“œ ๋™์•ˆ ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ฐ ํ”Œ๋ ˆ์ด์–ด๋“ค์ด ํš๋“ํ•œ ํฌ์ธํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•ด๋ณด์„ธ์š”.

์ž…๋ ฅ ํ˜•์‹

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

์ดํ›„ n๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฒฉ์ž์— ์žˆ๋Š” ์ด์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ์ค„์—๋Š” ๊ฐ๊ฐ์˜ ํ–‰์— ํ•ด๋‹นํ•˜๋Š” n๊ฐœ์˜ ์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ˆซ์ž 0์€ ๋นˆ ์นธ, 0๋ณด๋‹ค ํฐ ๊ฐ’์€ ์ด์˜ ๊ณต๊ฒฉ๋ ฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์ดํ›„ m๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ํ”Œ๋ ˆ์ด์–ด๋“ค์˜ ์ •๋ณด x, y, d, s๊ฐ€ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. (x, y)๋Š” ํ”Œ๋ ˆ์ด์–ด์˜ ์œ„์น˜, d๋Š” ๋ฐฉํ–ฅ, s๋Š” ํ”Œ๋ ˆ์ด์–ด์˜ ์ดˆ๊ธฐ ๋Šฅ๋ ฅ์น˜๋ฅผ ์˜๋ฏธํ•˜๊ณ  ๊ฐ ํ”Œ๋ ˆ์ด์–ด์˜ ์ดˆ๊ธฐ ๋Šฅ๋ ฅ์น˜๋Š” ๋ชจ๋‘ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๋ฐฉํ–ฅ d๋Š” 0๋ถ€ํ„ฐ 3๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ↑, →, ↓, ←์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ํ”Œ๋ ˆ์ด์–ด์˜ ์œ„์น˜๋Š” ๊ฒน์ณ์ ธ ์ฃผ์–ด์ง€์ง€ ์•Š์œผ๋ฉฐ, ํ”Œ๋ ˆ์ด์–ด์˜ ์ดˆ๊ธฐ ์œ„์น˜์—๋Š” ์ด์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • 2 ≤ n ≤ 20
  • 1 ≤ m ≤ 
  • 1 ≤ k ≤ 500
  • 1 ≤ ์ด์˜ ๊ณต๊ฒฉ๋ ฅ ≤ 100,000
  • 1 ≤ s ≤ 100
  • 1 ≤ x, y ≤ n

์ถœ๋ ฅ ํ˜•์‹

k ๋ผ์šด๋“œ ๋™์•ˆ ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๊ฐ ํ”Œ๋ ˆ์ด์–ด๋“ค์ด ํš๋“ํ•œ ํฌ์ธํŠธ๋ฅผ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ถœ๋ ฅํ•˜์„ธ์š”.


 

์‚ผ์„ฑ ๋นก๊ตฌํ˜„๋ฌธ์ œ ์žฌ๋ฐŒ๋„ค์š”...

 

์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํŠน๋ณ„ํ•œ ๊ฑฐ ์—†์ด ์ฐจ๊ทผ์ฐจ๊ทผ ๊ตฌํ˜„ํ•ด๋‚ด๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋ผ์„œ ์‰ฌ์› ๊ณ 

1์‹œ๊ฐ„๋งŒ์— ๊ตฌํ˜„์€ ๋๋ƒˆ๋Š”๋ฐ,,

์‚ฌ์†Œํ•œ๊ฑฐ ๋ช‡ ๊ฐœ ๋†“์น˜๋Š” ๋ฐ”๋žŒ์— ๋””๋ฒ„๊น…๋„ 1์‹œ๊ฐ„ ์ •๋„ ๊ฑธ๋ฆผใ…‹ใ…‹

๊ทธ๋ž˜๋„ ํ•ด์„ค ์•ˆ๋ณด๊ณ  ํ’€์–ด๋‚ด์„œ ์™•๋ฟŒ๋“ฏ!!!!!!

 

 

 

 

๋‚˜์˜ ํ’€์ด

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n;
int m;
int k;
int map[21][21][10]; // ์ด ๋งต(์ตœ๋Œ€ ๊ณต๊ฒฉ๋ ฅ์ธ ์ด๋ถ€ํ„ฐ ๋‚ด๋ฆผ์ฐจ์ˆœ)
int pMap[21][21];  // ํ”Œ๋ ˆ์ด์–ด ์ขŒํ‘œ, ๊ฐ’์€ ํ”Œ๋ ˆ์ด์–ด ๋ฒˆํ˜ธ
int pInfo[401][2]; //d๋Š” ๋ฐฉํ–ฅ, s๋Š” ํ”Œ๋ ˆ์ด์–ด์˜ ์ดˆ๊ธฐ ๋Šฅ๋ ฅ์น˜
int pGun[401]; // ํ”Œ๋ ˆ์ด์–ด์˜ ์ด ์ •๋ณด
int point[401]; // ํ”Œ๋ ˆ์ด์–ด ํฌ์ธํŠธ ์ •๋ณด
int px[4] = {-1,0,1,0};
int py[4] = {0,1,0,-1}; // 0:↑, 1:→, 2:↓, 3:←
int loserGun;

bool desc(int a, int b) {
    return a > b;
}

void checkGun(int player, int x, int y){
    if(map[x][y][0]==0) return;
    if(pGun[player] >= map[x][y][0]){
        return;
    }
    else{
        int low = pGun[player];
        pGun[player] = map[x][y][0];
        map[x][y][0] = low;
        sort(map[x][y], map[x][y]+10, desc);
    }
    return;
}

pair<int,int> fightPlayers(int newPlayer, int x, int y){
    int stayPlayer = pMap[x][y];
    int stayPower = pInfo[stayPlayer][1] + pGun[stayPlayer];
    int newPower = pInfo[newPlayer][1] + pGun[newPlayer];
    if(stayPower == newPower){
        if(pInfo[stayPlayer][1] > pInfo[newPlayer][1]) return {stayPlayer, newPlayer};
        else return {newPlayer, stayPlayer};
    }
    if(stayPower > newPower){
        point[stayPlayer] += (stayPower-newPower);
        return {stayPlayer, newPlayer};
    }
    else{
        point[newPlayer] += (newPower-stayPower);
        return {newPlayer, stayPlayer};
    }
}

void moveLoser(int loser, int x, int y){
    loserGun = pGun[loser];
    pGun[loser] = 0;
    for(int i=0; i<10; ++i){
        if(map[x][y][i]==0) {
            map[x][y][i] = loserGun;
            break;
        }
    }
    
    sort(map[x][y], map[x][y]+10, desc); // 0์ธ๋ฑ์Šค๊ฐ€ ์ตœ๋Œ€ ๊ณต๊ฒฉ๋ ฅ์ธ ์ด์„ ๊ฐ€์ง
    
    for(int i=0; i<4; ++i){
        int nx = x + px[pInfo[loser][0]];
        int ny = y + py[pInfo[loser][0]];
        if(nx<1 || ny<1 || nx>n || ny>n || pMap[nx][ny]>0){
            if(pInfo[loser][0]<3) {
                pInfo[loser][0]++;
            } else pInfo[loser][0] = 0;

        }else{
            pMap[x][y] = 0;
            pMap[nx][ny] = loser;
            checkGun(loser, nx, ny);
            break;
        }
    }
    
}

void getWinner(int winner, int x, int y){
    checkGun(winner, x, y);
}

void movePlayer(){
    for(int i=1; i<=m; ++i){
        bool nextTurn = false;
        for(int a=1;a<=n; ++a){
            for(int b=1; b<=n; ++b){
                if(pMap[a][b] == i){
                    int move = pInfo[i][0];
                    int nx = a + px[move];
                    int ny = b + py[move];

                    if(nx<1 || ny<1 || nx>n || ny>n){
                        if(move==0||move==1) {
                            pInfo[i][0] += 2;
                            move += 2;
                            nx = a + px[move];
                            ny = b + py[move];
                        } else {
                            pInfo[i][0] -= 2;
                            move -= 2;
                            nx = a + px[move];
                            ny = b + py[move];
                        }
                    }
                    pMap[a][b] = 0;
                    if(pMap[nx][ny] == 0) { // ๋นˆ์ž๋ฆฌ๋ฉด ์ด ํ™•์ธ
                        pMap[nx][ny] = i;
                        checkGun(i, nx, ny); 
                    }
                    else{ // ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ์žˆ์œผ๋ฉด ์‹ธ์›€
                        pair<int,int> wl = fightPlayers(i, nx, ny);
                        moveLoser(wl.second, nx, ny); // ์ง„ ์‚ฌ๋žŒ ์ด๋™์‹œํ‚ค๊ธฐ
                        getWinner(wl.first, nx, ny); // ๋” ์ข‹์€ ์ด ํš๋“
                        pMap[nx][ny] = wl.first;
                    }
                    nextTurn = true;
                    break;
                }
            }
            if(nextTurn) break;
        }
    }
}


int main() {
    int x;
    int y;
    cin >> n >> m >> k; // n ๊ฒฉ์ž์˜ ํฌ๊ธฐ, m ํ”Œ๋ ˆ์ด์–ด์˜ ์ˆ˜, k ๋ผ์šด๋“œ์˜ ์ˆ˜
    for(int i=1; i<=n; ++i){
        for(int j=1; j<=n; ++j) cin>>map[i][j][0];
    }
    for(int i=1; i<=m; ++i){
        cin>>x>>y;
        pMap[x][y] = i;
        cin>>pInfo[i][0]>>pInfo[i][1];
    }

    for(int i=0; i<k; ++i){
        movePlayer();
        // cout <<"--------------point-----\n";
        // for (int i = 1; i<=m; ++i){
        // cout << point[i] << " ";
        // }

        // cout <<"-map-\n";
        // for (int i = 1; i<=n; ++i){
        //     for (int j = 1; j<=n; ++j){
        //         cout << map[i][j][0] << " ";
        //     } cout <<"\n";
        // }
        // cout <<"--player--\n";
        // for (int i = 1; i<=n; ++i){
        //     for (int j = 1; j<=n; ++j){
        //         cout << pMap[i][j] << " ";
        //     } cout <<"\n";
        // }
    }

    for (int i = 1; i<=m; ++i){
        cout << point[i] << " ";
    }
        return 0;
}

 

728x90