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

24-H0ngJu #233

Merged
merged 2 commits into from
Aug 19, 2024
Merged

24-H0ngJu #233

merged 2 commits into from
Aug 19, 2024

Conversation

H0ngJu
Copy link
Collaborator

@H0ngJu H0ngJu commented Aug 17, 2024

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

์•„๊ธฐ์ƒ์–ด

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

1.5H

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

image


๋ฌธ์ œ์š”์•ฝ
์•„๊ธฐ์ƒ์–ด๋Š” ์ž์‹ ๋ณด๋‹ค ์ž‘์€ ๋ฌผ๊ณ ๊ธฐ๋งŒ ๋จน์„ ์ˆ˜ ์žˆ๋‹ค. ์ฃผ์–ด์ง„ ๊ณต๊ฐ„์—์„œ ๋ช‡ ์ดˆ ๋™์•ˆ ์•„๊ธฐ์ƒ์–ด๋Š” ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน์„ ์ˆ˜ ์žˆ๋Š”๊ฐ€?
์‹œ๊ฐ„์€ ์ด๋™ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„๋งŒ ๊ณ„์‚ฐํ•œ๋‹ค. ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์—ฌ๋Ÿฌ ๋งˆ๋ฆฌ์ธ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฌผ๊ณ ๊ธฐ๋ฅผ ๋จน๋Š”๋‹ค.



๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž BFS๋ผ๋Š” ๊ฒƒ์„ ๋‹ค๋“ค ์ƒ๊ฐํ•˜์…จ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๊ฐ€ ๊นŒ๋‹ค๋กญ๋‹ค๊ณ  ๋Š๋‚€๊ฑด ๋‹จ์ˆœํ•œ BFS๊ฐ€ ์•„๋‹ˆ๋ผ, ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ(๋ฌผ๊ณ ๊ธฐ)์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ƒ๊ธฐ๋ฉด, ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณณ์„ ๊ฐ€์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ถ”๊ฐ€๋กœ ๊ด€๋ฆฌ ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.


๊ทธ๋ž˜์„œ ์ €๋Š” ์ผ๋‹จ tmp๋ผ๋Š” ์ž„์‹œ ๋ฆฌ์ŠคํŠธ๋“ค์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ ,

์•„๊ธฐ ์ƒ์–ด๊ฐ€ ๋จน์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ๋“ค์˜ ์œ„์น˜์™€ ๊ฑฐ๋ฆฌ๋ฅผ ๋„ฃ์–ด์คฌ์Šต๋‹ˆ๋‹ค.


tmp ๋ฆฌ์ŠคํŠธ์— ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์กฐ๊ฑด์€

  1. ์•„๊ธฐ ์ƒ์–ด๋ณด๋‹ค ์ž‘์„ ๊ฒƒ
  2. tmp ์•ˆ์— ์žˆ๋Š” ์ตœ์†Œ๊ฑฐ๋ฆฌ์™€ ๊ฐ™๊ฑฐ๋‚˜ ๋” ์ž‘์„ ๊ฒƒ
    a. tmp ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ณด๋‹ค ๋” ์ž‘์€ ๊ฒฝ์šฐ : tmp๋ฅผ ์ดˆ๊ธฐํ™”
    b. tmp ์ตœ์†Œ๊ฑฐ๋ฆฌ์™€ ๊ฐ™์€ ๊ฒฝ์šฐ : tmp.appned()


์ฆ‰,

if 0 < space[cx][cy] < baby:
            if dis < min_dis:
                min_dis = dis
                tmp = [(cx, cy)]
            elif dis == min_dis:
                tmp.append((cx, cy))

์ด๋ ‡๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


๊ทธ๋ฆฌ๊ณ , ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋“ค์„ ๋‹ค ์กฐ์‚ฌํ–ˆ๋‹ค๋ฉด,
tmp ๋ฆฌ์ŠคํŠธ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์ž‘์€ ๊ฒƒ์„ ๋ฝ‘์•„์„œ, ์•„๊ธฐ ์ƒ์–ด๋ฅผ ์˜ฎ๊ฒจ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.




์—ฌ๊ธฐ๊นŒ์ง€ ์ฒ˜๋ฆฌํ•˜๊ณ ,,
๋‹น์—ฐํžˆ ํ†ต๊ณผ๊ฒ ์ง€?ํ•˜๊ณ  ์ œ์ถœํ–ˆ๋Š”๋ฐ ์‹คํŒจ๊ฐ€ ๋œจ๋”๋ผ๊ณ ์š”



for i in range(N):
    for j in range(N):
        if space[i][j] == 9:
            x, y = i, j
            space[x][y] = 0 # ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์•„๋‹ˆ๊ธฐ์— 0์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผํ•จ!!!!!!!!!!!!!

์ฒ˜์Œ ์‹œ์ž‘์ธ 9์—์„œ,, 0์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฑธ ๊นŒ๋จน๊ณ 
๋˜ ํ•œ~์ฐธ ๋“ค์—ฌ๋‹ค ๋ดค์Šต๋‹ˆ๋‹ค..

๐Ÿคฏ๐Ÿ”จ

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

Copy link

@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.

ํ—ˆํ—ˆ ๊ฐ ์›€์ง์ž„๋งˆ๋‹ค BFS ๋Œ๋ ค์„œ ์ตœ๋‹จ๊ฑฐ๋ฆฌ ๊ตฌํ•  ์ƒ๊ฐ์ด ์™ค์ผ€ ์•ˆ๋“ค์—ˆ์„๊นŒ์š”...
ํ•œ์ฐธ ์‚ฝ์งˆํ•˜๋‹ค๊ฐ€ ๊ฒจ์šฐ ๋– ์˜ฌ๋ ธ๋„ค์š” ใ…œ

์ „์ฒด ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
#include <algorithm>

using namespace std;

constexpr int BABY = 9;
const vector<pair<int, int>> Offset{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N; cin >> N;
    vector<vector<int>> Space(N, vector(N, 0));
    
    int X = 0, Y = 0;
    int SizeofBaby = 2, NumFishEaten = 0;
    
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < N; ++j)
        {
            cin >> Space[i][j];
            if(Space[i][j] == BABY)
            {
                X = i, Y = j;
                Space[i][j] = 0;
            }
        }
    }
    
    deque<tuple<int, int, int>> Q;
    vector<vector<bool>> Visited;
    
    auto CanEatFish = [&](int x, int y)
    {
        return 1 <= Space[x][y] && Space[x][y] < SizeofBaby;
    };
    
    auto CanMoveTo = [&](int x, int y)
    {
        if(x < 0 || x >= N || y < 0 || y >= N) return false;
        if(Visited[x][y]) return false;
        if(Space[x][y] > SizeofBaby) return false;
        
        return true;
    };
    
    int Tick = 0;
    while(true)
    {
        Q.clear();
        Visited.assign(N, vector(N, false));
        
        Q.emplace_back(X, Y, 0);
        Visited[X][Y] = true;
        
        int MinDistance = 400;
        vector<pair<int, int>> Candidates;
        
        while(!Q.empty())
        {
            const auto [x, y, d] = Q.front();
            Q.pop_front();
            
            if(CanEatFish(x, y))
            {
                if(d < MinDistance)
                {
                    MinDistance = d;
                    Candidates = {{x, y}};
                }
                else if(d == MinDistance)
                {
                    Candidates.emplace_back(x, y);
                }
            }
            
            for(const auto& [dx, dy] : Offset)
            {
                int nx = x + dx, ny = y + dy;
                if(!CanMoveTo(nx, ny)) continue;
                
                Visited[nx][ny] = true;
                Q.emplace_back(nx, ny, d + 1);
            }
        }
        
        if(Candidates.empty()) // Cannot Eat Fish Anymore
        {
            break;
        }
        
        pair<int, int> MinPoint = *min_element(Candidates.begin(), Candidates.end());
        Space[MinPoint.first][MinPoint.second] = 0;
        Tick += MinDistance;
        NumFishEaten++;
        
        if(SizeofBaby == NumFishEaten)
        {
            SizeofBaby++;
            NumFishEaten = 0;
        }
        
        X = MinPoint.first, Y = MinPoint.second;
    }
    
    cout << Tick;
    
    return 0;
}

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

์™€ ์ € ์ฒจ์— BFS๋กœ ํ•˜๋‹ค๊ฐ€ ๋„ˆ๋ฌด ์‹ ๊ฒฝ์จ์•ผํ•  ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ,

๋ฌผ๊ณ ๊ธฐ ํฌ๊ธฐ(์ตœ๋Œ€), ๋ฌผ๊ณ ๊ธฐ ์Šคํƒ(์ตœ๋Œ€), ๊ฑธ๋ฆฐ ์‹œ๊ฐ„(์ตœ์†Œ), y(์ตœ์†Œ), x(์ตœ์†Œ) ํž™์œผ๋กœ ๋Œ๋ ธ๋Š”๋ฐ...

BFS๋„ ๊ฐ€๋Šฅํ•˜๊ตฐ์š”.........

from heapq import *
import sys

def input(): return sys.stdin.readline().rstrip()

N = int(input())
board = []
start = None
for row_idx in range(N):
    row = list(map(int, input().split()))
    if 9 in row: start = row_idx, (row.index(9))
    board.append(row)

board[start[0]][start[1]] = 0
dx = [0, -1, 1, 0]
dy = [-1, 0, 0, 1]

deq = []
heappush(deq,(-2, 0, 0, start[0], start[1]))
visited = set(start)
answer = 0
max_stack = 0
max_size = 2
while deq:
    now_size, now_stack, now_time, now_y, now_x = heappop(deq)
    now_size *= -1
    now_stack *= -1
    
    if max_size >= now_size and max_stack > now_stack: continue
    if max_size > now_size: continue

    if board[now_y][now_x] != 0 and board[now_y][now_x] < now_size:
        board[now_y][now_x] = 0
        visited = set()
        
        if now_stack + 1 == now_size: 
            now_size += 1
            now_stack = 0

        else: now_stack += 1
    
        max_size = now_size
        max_stack = now_stack
        answer += now_time
        now_time = 0
        
    for dir in range(4):
        new_y = now_y + dy[dir]
        new_x = now_x + dx[dir]

        if new_x < 0 or new_x >= N: continue
        if new_y < 0 or new_y >= N: continue
        if (new_x, new_y) in visited: continue
        if board[new_y][new_x] > max_size: continue
    
        heappush(deq,(now_size * -1, now_stack * -1, now_time + 1, new_y, new_x))
        visited.add((new_x, new_y))
    
print(answer)

์•„์˜ค ๋ฌธ์ œ ์ž˜๋ชป์ฝ์–ด์„œ ์‹œ๊ฐ„์„ ์™€์žฅ์ฐฝ ๋‚ ๋ ธ๋„ค์š”.......... ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… ใ… 

๊ฐ™๊ฑฐ๋‚˜ ์ž‘์€ ๊ฑฐ๋งŒ ์ง€๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฑฐ์˜€๊ตฐ์•„....

Copy link
Member

Choose a reason for hiding this comment

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

์–œ ๋ชจ์—์š” ??????

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ใ…‡ใ…ฃ ์•„์ด๋Š” ์ €๋ฒˆ์ฃผ ์›”์š”์ผ ๋‚ด๋‹ค๋ฒ„๋ฆฐ pr..........

Comment on lines +39 to +63
if 0 < space[cx][cy] < baby:
if dis < min_dis:
min_dis = dis
tmp = [(cx, cy)]
elif dis == min_dis:
tmp.append((cx, cy))

for dx, dy in dir:
nx, ny = cx + dx, cy + dy
if check(nx, ny) and not visited[nx][ny] and space[nx][ny] <= baby:
visited[nx][ny] = True
q.append((nx, ny, dis + 1))

if not tmp:
break

tx, ty = min(tmp)
space[tx][ty] = 0
time += min_dis
fish += 1
if fish == baby:
baby += 1
fish = 0
x, y = tx, ty

Copy link
Member

Choose a reason for hiding this comment

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

๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ tmp์— ๋‹ค ๋„ฃ์–ด๋†“๊ณ 

๋งˆ์ง€๋ง‰์— ์ตœ์†Œ ๊ฑฐ๋ฆฌ๋ž‘ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๊ณณ์œผ๋กœ ์ˆœ๊ฐ„์ด๋™ ใ…Žใ„ทใ„ทใ„ทใ„ทใ„ทใ„ทใ„ท

์ด๊ฑธ ์™œ ์ƒ๊ฐ ๋ชปํ–ˆ์ง€...

Copy link
Member

Choose a reason for hiding this comment

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

tmp๋ฅผ ์ด๋ ‡๊ฒŒ ๊น”๋”ํžˆ ์“ธ ์ˆ˜ ์žˆ๊ตฐ์š” ์ €๋Š” ๋„ˆ๋ฌด ๋”๋Ÿฝ๊ฒŒ ์ป๋„ค์š” ใ„ทใ„ทใ„ทใ„ทใ„ท....

@alstjr7437
Copy link
Member

์ €๋Š” ์•ฝ๊ฐ„ ์ •์„์˜ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์–ด์š”!!

๊ทผ๋ฐ ๋ฌผ๊ณ ๊ธฐ ์ฐพ๊ณ  ๋จน๋Š” ๋ถ€๋ถ„์—์„œ ์กฐ๊ธˆ ์˜ค๋ฅ˜๊ฐ€ ๊ฑธ๋ ธ๋˜...

  1. while๋ฌธ ๋Œ๊ธฐ
  2. bfs์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฌผ๊ณ ๊ธฐ ์ฐพ์•„์„œ ๋จน๊ธฐ
  3. ์ด๋™ํ•œ ๊ฑฐ๋ฆฌ๋ฅผ ๋”ํ•ด์ค€๋‹ค
  4. ๋‹ค์Œ while๋ฌธ์—์„œ ๋‹ค์Œ ์ด๋™ํ•  ๊ณณ์„ ์ฐพ๋Š”๋‹ค
  5. ๋‹ค์Œ ์ด๋™ํ•  ๊ณณ์ด ์—†์œผ๋ฉด break

์‹์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค!

from collections import deque

dx, dy = [0,0,-1,1], [1,-1,0,0]

N = int(input())
map = [list(map(int, input().split())) for _ in range(N)]

baby_shark = 2
shark_eaten = 0

for i in range(N):
    for j in range(N):
        if map[i][j] == 9:
            now_x, now_y = i, j
            map[i][j] = 0

def bfs():
    global now_x, now_y, baby_shark, shark_eaten

    queue = deque()
    queue.append((now_x, now_y))
    visited = [[False] * N for _ in range(N)]
    visited[now_x][now_y] = True
    distance = [[0] * N for _ in range(N)]
    temp = []

    while queue:
        x, y = queue.popleft()

        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < N and 0 <= ny < N:
                if not visited[nx][ny] and map[nx][ny] <= baby_shark:
                    queue.append((nx, ny))
                    visited[nx][ny] = True
                    distance[nx][ny] = distance[x][y] + 1
                    if 0 < map[nx][ny] < baby_shark:
                        temp.append((nx, ny, distance[nx][ny]))

    if temp:
        # ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฌผ๊ณ ๊ธฐ ์ฐพ๊ธฐ
        temp.sort(key=lambda x: (x[2], x[0], x[1]))
        target_x, target_y, dist = temp[0]

        # ๋ฌผ๊ณ ๊ธฐ ๋จน๊ธฐ
        map[target_x][target_y] = 0
        shark_eaten += 1
        if shark_eaten == baby_shark:
            baby_shark += 1
            shark_eaten = 0

        now_x, now_y = target_x, target_y
        return dist  # ์ด๋™ํ•œ ๊ฑฐ๋ฆฌ ๋ฐ˜ํ™˜

    return 0  # ๋” ์ด์ƒ ๋จน์„ ๋ฌผ๊ณ ๊ธฐ๊ฐ€ ์—†์„ ๋•Œ

total_time = 0

while True:
    time = bfs()
    if time == 0:
        break
    total_time += time

print(total_time)

Comment on lines +39 to +63
if 0 < space[cx][cy] < baby:
if dis < min_dis:
min_dis = dis
tmp = [(cx, cy)]
elif dis == min_dis:
tmp.append((cx, cy))

for dx, dy in dir:
nx, ny = cx + dx, cy + dy
if check(nx, ny) and not visited[nx][ny] and space[nx][ny] <= baby:
visited[nx][ny] = True
q.append((nx, ny, dis + 1))

if not tmp:
break

tx, ty = min(tmp)
space[tx][ty] = 0
time += min_dis
fish += 1
if fish == baby:
baby += 1
fish = 0
x, y = tx, ty

Copy link
Member

Choose a reason for hiding this comment

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

tmp๋ฅผ ์ด๋ ‡๊ฒŒ ๊น”๋”ํžˆ ์“ธ ์ˆ˜ ์žˆ๊ตฐ์š” ์ €๋Š” ๋„ˆ๋ฌด ๋”๋Ÿฝ๊ฒŒ ์ป๋„ค์š” ใ„ทใ„ทใ„ทใ„ทใ„ท....

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