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

13-mj111 #222

Merged
merged 1 commit into from
Sep 30, 2024
Merged

13-mj111 #222

merged 1 commit into from
Sep 30, 2024

Conversation

mjj111
Copy link
Collaborator

@mjj111 mjj111 commented Sep 9, 2024

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

๋ฐฑ์ค€ - ๋‚š์‹œ์™•
https://www.acmicpc.net/problem/17143

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

1์‹œ๊ฐ„ 30๋ถ„

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

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ƒ์–ด๊ฐ€ ๋ฐฉํ–ฅ๊ณผ ํฌ๊ธฐ์— ๋”ฐ๋ผ ์ด๋™ํ•˜๋Š” ์™€์ค‘์— ์™ผ์ชฝ ์—ด์—์„œ ์˜ค๋ฅธ์ชฝ ์—ด๋กœ ์ƒ์–ด๋ฅผ ์žก๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค!
์ดํ›„, ์žก์€ ์ƒ์–ด ํฌ๊ธฐ์˜ ํ•ฉ์„ ์ถœ๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋จผ์ € ์ฃผ์–ด์ง„ ์ž…๋ ฅ์„ ๋ฐ›์•„์ฃผ๊ณ 

        for(int i = 1; i <= SARK_COUNT; i++) {
            st = new StringTokenizer(br.readLine());
            int x = Integer.parseInt(st.nextToken());
            int y = Integer.parseInt(st.nextToken());
            int speed = Integer.parseInt(st.nextToken());
            int direction = Integer.parseInt(st.nextToken());
            int size = Integer.parseInt(st.nextToken());

            map[x][y] = i;
            sharks.add(new Shark(i, x, y, speed, direction, size));
        }

ํ•œ ์—ด์”ฉ ์žก์œผ๋ฉฐ, ์ƒ์–ด๋ฅผ ์ด๋™์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.

        int answer = 0;
        for(int i = 1; i <= COLUMN; i++) {
            answer += getShark(i);
            moveShark();
        }

        System.out.println(answer);

์ƒ์–ด๋ฅผ ์žก์„ ๋–„๋Š” ์ฃผ์–ด์ง„ ์—ด๊ฐ’์— ์˜ํ•ด ์ƒ์–ด๋ฅผ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€, ํ•ด๋‹น ์ƒ์–ด๋ฅผ ๋ฌดํšจํ™” ํ•ด์•ผํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค!
๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์–ด๋ฅผ ์ฃฝ์—ˆ๋‹ค ํ‘œ์‹œํ•˜๊ณ 
์ฃผ์–ด์ง„ map์—์„œ ์ง€์›Œ์ค๋‹ˆ๋‹ค.

    private static int getShark(int now) {
        for(int i = 1; i <= ROW; i++) {
            int sharkNumber = map[i][now];

            if(sharkNumber != 0) {
                Shark s = sharks.get(sharkNumber-1);
                s.alive = false;
                map[i][now] = 0;
                return s.size;
            }
        }
        return 0;
    }

์ƒ์–ด๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์€ ์ฃผ์–ด์ง„ ๋ฐฉํ–ฅ์œผ๋กœ ์†๋„๋งŒํผ ์›€์ง์ด๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ์ƒ์–ด์˜ ์ด๋™๋ฐฉํ–ฅ์„ ๋”ฐ๋ผ ์›€์ง์ด๋‹ค ๋ฒฝ์— ๋ถ€๋”ชํž ๊ฒฝ์šฐ ๋ฐฉํ–ฅ์„ ๋ฐ˜๋Œ€๋กœ ํ‹€๊ณ  ์ด๋™์‹œ์ผœ์•ผํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค!

    private static void moveShark() {
        int[][] copyMap = new int[ROW+1][COLUMN+1];

        for(Shark s : sharks) {
            if(!s.alive) continue;

            int speed = s.speed;
            while(speed-- > 0) {
                int nextX = s.x + dx[s.direction];
                int nextY = s.y + dy[s.direction];

                if(isOutRange(nextX, nextY)) {
                    s.changeDirection();
                    speed++;
                    continue;
                }

                s.x = nextX;
                s.y = nextY;
            }

            if(copyMap[s.x][s.y] != 0) {
                Shark op = sharks.get(copyMap[s.x][s.y] - 1);
                if(s.size < op.size) {
                    s.alive = false;
                    continue;
                } else {
                    op.alive = false;
                }
            }

            copyMap[s.x][s.y] = s.number;
        }

        for(int i = 1; i <= ROW; i++) {
            for(int j = 1; j <= COLUMN; j++) {
                map[i][j] = copyMap[i][j];
            }
        }
    }

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

๋ฌธ์ œ๋ฅผ ์ฝ์œผ๋ฉด์„œ ์š”๊ตฌ์‚ฌํ•ญ์„ ์–ป์–ด๋‚ด๋Š”๋ฐ ์กฐ๊ธˆ ํž˜๋“ค์—ˆ์Šต๋‹ˆ๋‹ค..
๋”๋ถˆ์–ด Java๋กœ ๊ตฌํ˜„ํ•˜๋‹ˆ ์กฐ๊ธˆ ๋” ์–ด๋ ค์šด๊ฒƒ๋„ ๋Š๊ปด์กŒ๋„ค์š” ใ…Ž (์ด์ œ๋Š” ํŒŒ์ด์ฌ์œผ๋กœ ๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค)

๋นก๊ตฌํ˜„์€ ๊ตฌํ˜„๋ณด๋‹ค, ์š”๊ตฌ์‚ฌํ•ญ์„ ์ž˜ ์ดํ•ดํ•˜๋Š”๊ฒŒ ๊ด€๊ฑด์ž„์„ ์ฒด๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

ํ›„....

์ฒ˜์Œ์—๋Š” ๊ฒฉ์ž ์นธ ํฌ๊ธฐ๊ฐ€ ์ตœ๋Œ€ 100 x 100 ์ธ ๊ฑฐ ๋ณด๊ณ  ๋งค ๋ฐ˜๋ณต๋งˆ๋‹ค ๊ฒฉ์ž ์ซ™ ํ›‘์–ด์„œ ์ƒ์–ด ์˜ฎ๊ธฐ๊ณ  ์žก์•„๋จน๋Š” ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋„๋ก ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ์ตœ๋Œ€ 14%์ฏค์ธ๊ฐ€์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๋‚˜๋„ค์š”...

์ด๋ก  ์ƒ ๋‚š์‹œ์™• ์˜ฎ๊ธฐ๋Š” ๊ฑฐ 100๋ฒˆ x ๋งค ์ด๋™๋งˆ๋‹ค ์ƒ์–ด ์žก๊ธฐ ์ตœ๋Œ€ 100์นธ x ์ƒ์–ด ์ด๋™์„ ์œ„ํ•œ ๊ทธ๋ฆฌ๋“œ ํ›‘๊ธฐ 100 x 100... ๋”ฐ์ ธ๋ณด๋‹ˆ ์—ฐ์‚ฐ 1์–ต๋ฒˆ ๋„˜๊ธธ ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค ์‹ถ๋”๋ผ๊ตฌ์š”...

๊ทธ๋ž˜์„œ ์ƒ์–ด ์ •๋ณด๋ฅผ ๋‹ด๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์„œ ๊ฑ”๋„ค๋“ค๋งŒ ํ›‘๋„๋ก ์ˆ˜์ •ํ•˜๋‹ˆ ์‹œ๊ฐ„ ์•ˆ์— ํ†ต๊ณผํ•˜๋„ค์š” ํ›„...

๋นก๊ตฌํ˜„์€ ์ •๋ง ์‹ซ์Šต๋‹ˆ๋‹ค...

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const vector<pair<int, int>> Offset{ {0, 0}, { -1, 0 }, {1, 0}, {0, 1}, {0, -1} };

int R, C, M;
inline bool OutOfBound(int r, int c)
{
    return !(1 <= r && r <= R && 1 <= c && c <= C);
}

struct FShark
{
    int R, C;
    int Speed;
    int Direction;
    int Size;
    bool IsDead;

    FShark(int InR, int InC, int InS, int InD, int InZ)
        : R(InR)
        , C(InC)
        , Speed(InS)
        , Direction(InD)
        , Size(InZ)
        , IsDead(false)
    {

    }
};

vector<FShark*> Sharks;
vector<vector<int>> Grid;

void Move()
{
    for (FShark* Shark : Sharks)
    {
        if (Shark->IsDead) continue;

        int Speed = Shark->Speed, Direction = Shark->Direction, Size = Shark->Size;
        int r = Shark->R, c = Shark->C;
        Grid[r][c] = -1;
        
        int nr, nc;
        while (true)
        {
            nr = r + Speed * Offset[Direction].first;
            nc = c + Speed * Offset[Direction].second;

            if (!OutOfBound(nr, nc))
            {
                break;
            }

            switch (Direction)
            {
            case 1:
                Speed -= (r - 1);
                r = 1;
                Direction = 2;
                break;
            case 2:
                Speed -= R - r;
                r = R;
                Direction = 1;
                break;
            case 3:
                Speed -= C - c;
                c = C;
                Direction = 4;
                break;
            case 4:
                Speed -= (c - 1);
                c = 1;
                Direction = 3;
                break;
            }
        }

        Shark->R = nr;
        Shark->C = nc;
        Shark->Direction = Direction;
    }
}

void Update()
{
    for (int Index = 0; Index < M; ++Index)
    {
        FShark* Shark = Sharks[Index];
        if (Shark->IsDead) continue;

        int R = Shark->R, C = Shark->C;
        if (Grid[R][C] == -1)
        {
            Grid[R][C] = Index;
        }
        else
        {
            int OtherIndex = Grid[R][C];
            FShark* Other = Sharks[OtherIndex];

            if (Shark->Size > Other->Size)
            {
                Other->IsDead = true;
                Grid[R][C] = Index;
            }
            else
            {
                Shark->IsDead = true;
            }
        }
    }
}

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);

    cin >> R >> C >> M;
    Sharks.resize(M);
    Grid.assign(R + 1, vector<int>(C + 1, -1));

    for (int i = 0; i < M; ++i)
    {
        int r, c, s, d, z; cin >> r >> c >> s >> d >> z;
        Grid[r][c] = i;
        Sharks[i] = new FShark(r, c, s, d, z);
    }

    int Answer = 0;
    for (int c = 1; c <= C; ++c)
    {
        for (int r = 1; r <= R; ++r)
        {
            const int Index = Grid[r][c];
            if (Index > -1)
            {
                Answer += Sharks[Index]->Size;
                Sharks[Index]->IsDead = true;
                Grid[r][c] = -1;
                break;
            }
        }

        Move();
        Update();
    }

    cout << Answer;

    return 0;
}

Comment on lines +70 to +82
while(speed-- > 0) {
int nextX = s.x + dx[s.direction];
int nextY = s.y + dy[s.direction];

if(isOutRange(nextX, nextY)) {
s.changeDirection();
speed++;
continue;
}

s.x = nextX;
s.y = nextY;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

๋งค ์ƒ์–ด๋งˆ๋‹ค ํ•œ ์นธ์”ฉ ์ด๋™ ํŒ์ •์„ ์ฃผ๋Š” ๊ฒƒ๋„ ์ƒ๊ฐ๋ณด๋‹ค ์‹œ๊ฐ„์„ ์ข€ ์žก์•„๋จน๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ฒ˜์Œ์— ์‹œ๊ฐ„ ์•ˆ์— ํ†ต๊ณผํ•œ ์ฝ”๋“œ๊ฐ€ ๋Š๋ฆฐ ๊ฒŒ ์•ฝ๊ฐ„ ์ด ๋ฌธ์ œ๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
speed๋ž‘ ๋ฐฉํ–ฅ์„ ์ด์šฉํ•ด ๋‹ค์Œ ์ขŒํ‘œ๋ฅผ ํ•œ ๋ฒˆ์— ์–ป๊ณ , bound๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๋ณด์ •์„ ์ฃผ๋Š” ํ˜•์‹์œผ๋กœ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ € ๋‹น์‹œ์— ์Šคํ”ผ๋“œ์— ๋งž์ถฐ์„œ ํ‰ ํŠ•๊ธฐ๋Š” ์ธ๋ฑ์Šค ๊ณ„์‚ฐํ•˜๊ธฐ ๋ณด๋‹ค ๊ทธ๋ƒฅ speed ์•ˆ์— ๋™์ž‘ํ•˜๋„๋ก ํ•˜๋Š”๊ฒŒ ๊ณ„์‚ฐํ•ด๋ณด๋‹ˆ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ์•ˆ๋‚จ๋Š” ํฌ๊ธฐ๋ผ์„œ ๊ทธ๋žฌ๋˜๊ฑฐ๊ฐ™์•„์š”!

๋นก๊ตฌํ˜„ ๋นจ๋ฆฌ ํ‘ธ๋Š”๋ฐ ์ง‘์ค‘ํ•ด์„œ ์ •์‹ ์ด ์—†์—ˆ์–ด์š” ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ใ… 

Comment on lines +84 to +94
if(copyMap[s.x][s.y] != 0) {
Shark op = sharks.get(copyMap[s.x][s.y] - 1);
if(s.size < op.size) {
s.alive = false;
continue;
} else {
op.alive = false;
}
}

copyMap[s.x][s.y] = s.number;
Copy link
Collaborator

Choose a reason for hiding this comment

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

๊ฐ ์ƒ์–ด๊ฐ€ ์ขŒํ‘œ ์ •๋ณด์™€ ์‚ด์•˜๋Š”๊ฐ€/์ฃฝ์—ˆ๋Š”๊ฐ€ ์ •๋ณด๋„ ๋“ค๊ณ  ์žˆ์œผ๋‹ˆ,
Move ํ•จ์ˆ˜ ๋‚ด์—์„œ๋Š” ์ƒ์–ด๋ฅผ ์˜ฎ๊ธฐ๋Š” ๊ธฐ๋Šฅ๋งŒ ํ•˜๊ณ  ์ƒ์–ด ์žก์•„๋จน๋Š” ์ฒ˜๋ฆฌ๋Š” ๋ณ„๋„์˜ ํ•จ์ˆ˜์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

๋ฐฐ์—ด์— ์ƒ์–ด ์ •๋ณด๋ฅผ ๋„ฃ๊ณ ,
๋‚š์‹œ์™•์ด ์žˆ๋Š” ์—ด์—์„œ ๊ฐ€์žฅ ๋•…์— ๊ฐ€๊นŒ์šด ์ƒ์–ด๋ฅผ ์žก์Šต๋‹ˆ๋‹ค

๊ทธ ํ›„ ์‚ด์•„์žˆ๋Š” ์ƒ์–ด ์ˆ˜๋งŒํผ popํ•ด์ค˜์„œ
์ƒ์–ด์˜ ์œ„์น˜์— ์ƒ์–ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ทธ๋Œ€๋กœ ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์œผ๋กœ ์ƒ์‚ฌ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ณ 
์ƒ์–ด๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณตํ•ด์„œ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค!

์ „์ฒด ์ฝ”๋“œ

from collections import deque

input = open("input.txt").readline

R, C, M = map(int,input().split())

shark_cnt = M
dq = deque([])

table = [[0]*C for _ in range(R)]

for _ in range(M):
    r, c, s, d, z = map(int, input().split())

    if not s:
        s = s % (2*R-2) if d <= 2 else s % (2*C-2)

    table[r-1][c-1] = max(table[r-1][c-1], z)
    dq.append((r-1, c-1, s, d, z))

result = 0

for i in range(C):
    
    # fishing
    for j in range(R):
        if not table[j][i]:
            continue
        
        result += table[j][i]
        table[j][i] = 0
        break

    # shark_check
    for _ in range(shark_cnt):
        r, c, s, d, z = dq.popleft()

        if not table[r][c] == z:
            shark_cnt -= 1
            continue
        else:
            table[r][c] = 0
            dq.append(( r, c, s, d, z ))

    # shark_move
    for _ in range(shark_cnt):
        
        r, c, s, d, z = dq.popleft()

        # 1 : ์œ„ / 2 : ์•„๋ž˜ / 3 : ์˜ค๋ฅธ์ชฝ / 4 : ์™ผ์ชฝ
        if d <= 2:
            r += -s if d==1 else s

            while not 0 <= r < R:
                r = abs(r) if r < 0 else 2*R-2-r
                d = 1 if d==2 else 2

        else:
            c += s if d == 3 else -s

            while not 0 <= c < C:
                c = abs(c) if c <0 else 2*C-2-c
                d = 3 if d == 4 else 4

        table[r][c] = max(table[r][c], z)
        dq.append((r, c, s, d, z))
    
print(result)

๋‚˜๋ฆ„ ์Šค๋ฌด์Šคํ•˜๊ฒŒ ํ’€์–ด์„œ ๊ธฐ๋ถ„์ด ์ข‹๋„ค์š” :)

Copy link
Member

@xxubin04 xxubin04 left a comment

Choose a reason for hiding this comment

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

์กฐ๊ธˆ ๊ตฌํ˜„ํ•˜๋‹ค๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•ด์„œ ํฌ๊ธฐํ–ˆ๋„ค์š”.. ์ˆ˜๊ณ ๋งŽ์œผ์…จ์Šต๋‹ˆ๋‹ค!!
์ƒ์–ด๊ฐ€ ๋์— ๋‹ค๋‹ค๋ž์„ ๋•Œ ๋ฐฉํ–ฅ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ๋„ ์ผ์ผ์ด ํ•ด์ฃผ์–ด์•ผ ํ•˜๊ณ , ๊ทธ๋ฆฌ๊ณ  ๋ฐ”๋€ ์œ„์น˜๋„ ๊ณ„์† ๊ฐฑ์‹ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๊ฒŒ ์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ต๋„ค์š”.
์ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ™์€ ์œ„์น˜์— ์—ฌ๋Ÿฌ ์ƒ์–ด๊ฐ€ ์žˆ์œผ๋ฉด ํฌ๊ธฐ ๋น„๊ตํ•ด์ฃผ๋Š” ๊ฑฐ๋ž‘ ๊ฐ€์žฅ ์œ„์˜ ์ƒ์–ด๊ฐ€ ๋‚š์‹œ๋˜์–ด ์—†์–ด์ง€๋Š” ๊ฒƒ๋„ ์ƒ๊ฐํ•ด์•ผํ•˜๊ณ  ์—ฌ๋Ÿฌ๋ชจ๋กœ ๋ณต์žกํ•œ ๋ฌธ์ œ๋„ค์š”..๐Ÿ˜ญ

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