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

62-9kyo-hwang #217

Merged
merged 11 commits into from
Sep 19, 2024
Merged

62-9kyo-hwang #217

merged 11 commits into from
Sep 19, 2024

Conversation

9kyo-hwang
Copy link
Collaborator

@9kyo-hwang 9kyo-hwang commented Aug 12, 2024

πŸ”— 문제 링크

무인도 μ—¬ν–‰

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

10λΆ„

✨ μˆ˜λ„ μ½”λ“œ

1. 문제 μ„€λͺ…

1 x 1크기의 μ‚¬κ°ν˜•λ“€λ‘œ 이루어진 μ§μ‚¬κ°ν˜• 격자 ν˜•νƒœμ˜ 지도 정보가 주어진닀. 격자의 각 μΉΈμ—λŠ” 'X' λ˜λŠ” 1μ—μ„œ 9 μ‚¬μ΄μ˜ μžμ—°μˆ˜κ°€ μ ν˜€μžˆλ‹€.
μ§€λ„μ˜ 각 μΉΈμ—μ„œ 상, ν•˜, 쒌, 우둜 μ—°κ²°λ˜λŠ” 칸에 적힌 숫자λ₯Ό λͺ¨λ‘ ν•©ν•œ 값듀을 κ΅¬ν•˜κ³ μž ν•œλ‹€.

예λ₯Ό λ“€μ–΄, ["X591X","X1X5X","X231X", "1XXX1"]와 같이 λ¬Έμžμ—΄λ‘œ 주어진닀면 μ΄λŠ” λ‹€μŒκ³Ό 같은 지도λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 것이닀.
image

μ—°κ²°λœ μΉΈλ“€μ˜ 값을 ν•©μΉ˜λ©΄ λ‹€μŒκ³Ό κ°™μœΌλ©°
image

μ΄λŠ” [1, 1, 27]둜 μ €μž₯ν•  수 μžˆλ‹€.

지도λ₯Ό λ‚˜νƒ€λ‚΄λŠ” λ¬Έμžμ—΄ λ°°μ—΄ mapsκ°€ λ§€κ°œλ³€μˆ˜λ‘œ μ£Όμ–΄μ§ˆ λ•Œ, 숫자 칸듀을 μ—°κ²°ν•΄ 얻을 수 μžˆλŠ” μˆ«μžλ“€μ„ 배열에 μ˜€λ¦„μ°¨μˆœμœΌλ‘œ λ‹΄μ•„ return ν•˜λŠ” solution ν•¨μˆ˜λ₯Ό μ™„μ„±ν•˜λΌ. λ§Œμ•½ 숫자 칸이 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ -1을 배열에 λ‹΄μ•„ return ν•˜λΌ.

2. μ„€λͺ…

κ·Έλž˜ν”„ μ—°κ²° μš”μ†Œ κ΄€λ ¨ λ¬Έμ œμž„μ„ μ•Œ 수 μžˆλ‹€. μ—°κ²°μš”μ†Œ κ΄€λ ¨λœ λ¬Έμ œλŠ” μ—¬λŸ¬ μœ ν˜•μ΄ 있으며, μ—¬κΈ°μ„œλŠ” 각 μ—°κ²°μš”μ†Œμ˜ '크기'λ₯Ό κ΅¬ν•˜λŠ” 문제둜 해석할 수 μžˆλ‹€.
각 μΉΈ(즉, κ·Έλž˜ν”„ λ…Έλ“œ)의 크기가 1 ~ 9 사이이고, 상/ν•˜/쒌/우둜 λ…Έλ“œκ°€ μ—°κ²°λœ κ·Έλž˜ν”„λ‘œ 해석할 수 μžˆλ‹€.

문제 해석이 μ™„λ£Œλλ‹€λ©΄, μ½”λ“œλŠ” μ‰½κ²Œ κ΅¬ν˜„ν•  수 μžˆλ‹€.

vector<int> DaysofStay;
for(int i = 0; i < Maps.size(); ++i)
{
    for(int j = 0; j < Maps[0].size(); ++j)
    {
        if(Maps[i][j] != 'X')
        {
            DaysofStay.emplace_back(Merge(Maps, i, j));
        }
    }
}

무인도가 μ•„λ‹Œ μΉΈ(λ…Έλ“œ)이라면, Merge ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄ ν•΄λ‹Ή ν•¨μˆ˜κ°€ λ°˜ν™˜ν•˜λŠ” μ—°κ²° μš”μ†Œμ˜ 크기λ₯Ό DaysofStay λ¦¬μŠ€νŠΈμ— μ €μž₯ν•œλ‹€.

int Merge(vector<string>& Maps, int x, int y)
{
    if(x < 0 || x >= Maps.size() || y < 0 || y >= Maps[0].size() || Maps[x][y] == 'X') 
    {
        return 0;
    }
    
    int Day = Maps[x][y] - '0';
    Maps[x][y] = 'X';
    
    return Day 
        + Merge(Maps, x - 1, y) 
        + Merge(Maps, x, y + 1) 
        + Merge(Maps, x + 1, y) 
        + Merge(Maps, x, y - 1);
}

μ—°κ²°μš”μ†Œμ˜ 크기λ₯Ό λ°˜ν™˜ν•˜λŠ” Merge ν•¨μˆ˜λŠ” DFSλ₯Ό 기반으둜 μž‘μ„±ν–ˆλ‹€.
ν˜„μž¬ (x, y) μ’Œν‘œκ°€ κ·Έλ¦¬λ“œ λ²”μœ„λ₯Ό λ²—μ–΄λ‚¬κ±°λ‚˜ 무인도('X') 칸이라면 0을 λ°˜ν™˜ν•˜κ³ , μ•„λ‹ˆλΌλ©΄ ν˜„μž¬ 칸의 숫자(Maps[x][y], charμ΄λ―€λ‘œ int둜 λ³€ν™˜ ν•„μš”)와 μΈμ ‘ν•œ 4λ°©ν–₯ 칸에 λŒ€ν•œ μž¬κ·€ 호좜의 κ²°κ³Όλ₯Ό λͺ¨λ‘ 더해 λ°˜ν™˜ν•œλ‹€.
이 λ•Œ, ν˜„μž¬ μΉΈμ—” 더 이상 λ°©λ¬Έν•˜μ§€ μ•Šλ„λ‘ λ§ˆν‚Ή(무인도, 즉 'X' κ°’μœΌλ‘œ λ³€κ²½)ν•΄μ€€λ‹€.

if(DaysofStay.empty()) 
{
    return {-1};
}

sort(DaysofStay.begin(), DaysofStay.end());
return DaysofStay;

μ§€λ„μ˜ λͺ¨λ“  칸에 λŒ€ν•΄ 검사λ₯Ό μ™„λ£Œν–ˆλŠ”λ°λ„ DaysofStay λ¦¬μŠ€νŠΈκ°€ λΉ„μ–΄μžˆλ‹€λ©΄, 숫자 칸이 μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” κ²ƒμ΄λ―€λ‘œ [-1]을 λ°˜ν™˜ν•œλ‹€.
μ•„λ‹ˆλΌλ©΄ μ˜€λ¦„μ°¨μˆœ μ •λ ¬ ν›„ λ°˜ν™˜ν•œλ‹€.

전체 μ½”λ“œ

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int Merge(vector<string>& Maps, int x, int y)
{
    if(x < 0 || x >= Maps.size() || y < 0 || y >= Maps[0].size() || Maps[x][y] == 'X') 
    {
        return 0;
    }
    
    int Day = Maps[x][y] - '0';
    Maps[x][y] = 'X';
    
    return Day 
        + Merge(Maps, x - 1, y) 
        + Merge(Maps, x, y + 1) 
        + Merge(Maps, x + 1, y) 
        + Merge(Maps, x, y - 1);
}

vector<int> solution(vector<string> Maps) 
{
    vector<int> DaysofStay;
    for(int i = 0; i < Maps.size(); ++i)
    {
        for(int j = 0; j < Maps[0].size(); ++j)
        {
            if(Maps[i][j] != 'X')
            {
                DaysofStay.emplace_back(Merge(Maps, i, j));
            }
        }
    }
    
    if(DaysofStay.empty()) 
    {
        return {-1};
    }
    
    sort(DaysofStay.begin(), DaysofStay.end());
    return DaysofStay;
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

음 μ΄λ²ˆμ—λ„ λ„ˆλ¬΄ 빨리 풀어버린...

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.

image

10λΆ„λ§Œμ— ν‘Έμ‹œλ‹€λ‹ˆ...
λ¬΄μΈλ„μΉΈμ΄κ±°λ‚˜ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜λ©΄ 0을 λ°˜ν™˜ν•΄μ„œ μ‰½κ²Œ 더할 수 μžˆκ΅°μš”!
수고 λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€..!

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

무지성 bfs λ°”λ‘œ λ•Œλ ΈμŠ΅λ‹ˆλ‹€.

import collections

def can_go(x, y):
    if not (0 <= x < N and 0 <= y < M):
        return False
    if visited[x][y]:
        return False
    if maps[x][y] == 'X':
        return False
    return True

def get_food_amount(start_x, start_y):
    global dx, dy, maps, visited
    
    food = 0
    queue = collections.deque([(start_x, start_y)]) 
    visited[start_x][start_y] = True 
    
    while queue:
        x, y = queue.popleft()
        food += int(maps[x][y])

        for i in range(4):
            next_x = x + dx[i]
            next_y = y + dy[i]
            if can_go(next_x, next_y):
                visited[next_x][next_y] = True
                queue.append((next_x, next_y))
    
    return food

N, M = 0, 0
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
maps, visited = [], []

def solution(mapss):
    global N, M, visited, maps
    maps = mapss
    N = len(maps)
    M = len(maps[0])
    visited = [[False] * M for _ in range(N)]
    
    answer = []
    for i in range(N):
        for j in range(M):
            if maps[i][j] != 'X' and not visited[i][j]:
                answer.append(get_food_amount(i, j))
    
    if not answer:
        return [-1]
    
    return sorted(answer)

@gjsk132
Copy link
Member

gjsk132 commented Sep 18, 2024

였... mapss κΉŒλ¦¬ν•œλ°μš”

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.

νŒŒμ΄μ¬μ€ λ¬Έμžμ—΄ μš”μ†Œ 변경이 μ•ˆλ˜μ„œ, λ”°λ‘œ 체크할 수 μžˆλ„λ‘ μ²˜λ¦¬ν•΄μ£Όμ–΄μ•Όν•˜λŠ”λ°...

visited(방문체크)λ₯Ό λ§Œλ“€κ±°λ‚˜ mapsλ₯Ό μ‚°μ‚°νžˆ λΆ„ν•΄ν•˜λŠ” 방법이 μžˆμŠ΅λ‹ˆλ‹€...

μ €λŠ” mapsλ₯Ό ν•˜λ‚˜μ”© λΆ„λ¦¬μ‹œμΌœμ€¬μŠ΅λ‹ˆλ‹€!

μ½”λ“œ

def solution(maps):
    
    answer = []    
    offset = [(-1, 0), (0, 1), (1, 0), (0, -1)]

    # μ—¬κΈ°μ„œ maps을 뢄리해주고, XλŠ” 0으둜 μΉ˜ν™˜ν•΄μ€¬μŠ΅λ‹ˆλ‹€.
    memo = [[int(i) if not i == 'X' else 0 for i in row] for row in maps]

    h, w = len(maps), len(maps[0])

    def bfs(x, y):
        total = memo[x][y]
        memo[x][y] = 0

        dq = [(x, y)]

        while dq:
            x, y = dq.pop()

            for dx, dy in offset:
                px = x + dx
                py = y + dy

                if not 0 <= px < h or not 0 <= py < w:
                    continue

                if (value := memo[px][py]) == 0:
                    continue

                total += value
                memo[px][py] = 0
                dq.append((px, py))

        return total

    for x in range(h):
        for y in range(w):
            if memo[x][y] == 0:
                continue
            
            answer.append(bfs(x, y))

    if not answer:
        answer.append(-1)

    return sorted(answer)

@9kyo-hwang 9kyo-hwang merged commit 73f84d2 into main Sep 19, 2024
7 checks passed
@9kyo-hwang 9kyo-hwang deleted the 62-9kyo-hwang branch September 19, 2024 05:32
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