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

31-alstjr7437 #218

Merged
merged 2 commits into from
Aug 6, 2024
Merged

31-alstjr7437 #218

merged 2 commits into from
Aug 6, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Jul 15, 2024

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

๊ฒŒ์ž„ ๋งต ์ตœ๋‹จ๊ฑฐ๋ฆฌ

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

20๋ถ„(๋„ˆ๋ฌด ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ํ•ด๋ฒ„๋ ธ๋„ค์š”,, ํ•˜๋ฃจ ์ข…์ผ ์ฝ”๋”ฉ์„ ํ–ˆ๋”๋‹ˆ ์ฒด๋ ฅ์ด ์—†์–ด์„œ ์ฃ„์†กํ•ฉ๋‹ˆ๋‹คใ… ,,)

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

๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์–ด,,, ๋„ˆ๋ฌด ์‰ฌ์šด๋ฐ,,? ๊ทธ๋ž˜์„œ ํŒŒ์ด์ฌ์œผ๋กœ ํ•œ 10๋ถ„๋งŒ์— ํ’€๊ณ 

๋„ˆ๋ฌด ๋‚ ๋จน์ธ ๊ฒƒ ๊ฐ™๊ณ ,, Swift๋กœ๋„ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™์•„์„œ Swift๋กœ๋„ ์งœ๋ดค์Šต๋‹ˆ๋‹ค!!

๋”ฐ๋กœ ๋กœ์ง์€ ์ •๋ง ๊ฐ„๋‹จํ•œ BFS๋ผ ์„ค๋ช…ํ•  ๊ฒƒ ๋„ ์—†๋Š” ์ˆ˜์ค€์ž…๋‹ˆ๋‹ค!!

๊ทธ๋ƒฅ ์‹œ์ž‘์นธ๋ถ€ํ„ฐ 1๋กœ ์‹œ์ž‘ํ•ด์„œ ๋ช‡์นธ์„ ์ด๋™ํ•ด์„œ ๋„์ฐฉ์ ์— ๊ฐ€๋Š”์ง€ ์„ธ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค!!

from collections import deque
def solution(maps):
    answer = 0

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

    def bfs(x, y):
        queue = deque()
        queue.append((x, y))

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

            for i in range(4):
                nx = x + dx[i]
                ny = y + dy[i]

                if nx < 0 or nx >= len(maps) or ny < 0 or ny >= len(maps[0]): continue

                if maps[nx][ny] == 0:  continue

                if maps[nx][ny] == 1:
                    maps[nx][ny] = maps[x][y] + 1
                    queue.append((nx, ny))

        return maps[len(maps)-1][len(maps[0])-1]

    answer = bfs(0, 0)
    return -1 if answer == 1 else answer    
import Foundation

func solution(_ maps: inout [[Int]]) -> Int {
    let dx = [-1, 1, 0, 0]
    let dy = [0, 0, -1, 1]
    let rowCount = maps.count
    let colCount = maps[0].count
    
    func bfs(_ x: Int, _ y: Int) -> Int {
        var queue = [(x, y)]
        var index = 0
        
        while index < queue.count {
            let (x, y) = queue[index]
            index += 1
            
            for i in 0..<4 {
                let nx = x + dx[i]
                let ny = y + dy[i]
                
                if nx < 0 || nx >= rowCount || ny < 0 || ny >= colCount { continue }
                
                if maps[nx][ny] == 0 { continue }
                
                if maps[nx][ny] == 1 {
                    maps[nx][ny] = maps[x][y] + 1
                    queue.append((nx, ny))
                }
            }
        }
        
        return maps[rowCount - 1][colCount - 1]
    }
    
    let answer = bfs(0, 0)
    return answer == 1 ? -1 : answer
}

// ์˜ˆ์‹œ ํ˜ธ์ถœ
var maps = [
    [1, 0, 1, 1, 1],
    [1, 0, 1, 0, 1],
    [1, 0, 1, 1, 1],
    [1, 1, 1, 0, 1],
    [0, 0, 0, 0, 1] 
]
print(solution(&maps)) // ์ถœ๋ ฅ: 11

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

์ด๋ฒˆ์— ๋„ค๋ถ€์บ  ์ฒซ๋‚ ์„ ํ•˜๋ฉด์„œ inout Parameters์— ๋Œ€ํ•ด ๋ฐฐ์šฐ๊ฒŒ ๋˜์„œ ๋…ธ์…˜์— ์ •๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.(๋„ค๋ถ€์บ  ์ œ์ถœ์šฉ๋„ ์žˆ๊ณ ,,,,)

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.

from collections import deque

def solution(maps):    
    dx = [0, 0, -1, 1]
    dy = [-1, 1, 0, 0]
    
    visited = {(0,0),}
    deq = deque()
    deq.append((0,0,1))
    while deq:
        now_x, now_y, now_count = deq.popleft()
        
        if (now_x, now_y) == (len(maps[0])-1, len(maps)-1):
            return now_count
        
        for dir in range(4):
            new_x = now_x + dx[dir]
            new_y = now_y + dy[dir]
            
            if new_x < 0 or new_x >= len(maps[0]): continue
            if new_y < 0 or new_y >= len(maps): continue
            if (new_x, new_y) in visited: continue
            if maps[new_y][new_x] == 0: continue
            
            visited.add((new_x, new_y))
            deq.append((new_x, new_y, now_count+1))
        
    return -1

์งœ์ž”!!!
์•„๋‹ˆ ์ด๊ฑฐ ๊ทธ๋ƒฅ BFS DFS์ธ๋ฐ?!

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

@SeongHoonC SeongHoonC 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 java.util.*;
import java.awt.*;

class Solution {
    
    int[] dx = { 0,0,1,-1};
    int[] dy = { 1,-1,0,0};
    
    public int solution(int[][] maps) {
        int answer = 0;
        ArrayDeque<Point> q = new ArrayDeque<Point>();
        var n = maps.length;
        var m = maps[0].length;
        
        q.add(new Point(0,0));
        while(!q.isEmpty()){
            Point now = q.removeFirst();    
            
            for(int i = 0; i < 4; i++) {
                int nextX = now.x + dx[i];
                int nextY = now.y + dy[i];
                
                if(nextX >= n || nextX < 0 || nextY >= m || nextY <0) {
                    continue;
                } 
                
                if(maps[nextX][nextY] != 1) {
                    continue;
                }
                
                if(nextX == n - 1 && nextY == m - 1) {
                    return maps[now.x][now.y] + 1;
                }
                maps[nextX][nextY] = maps[now.x][now.y] + 1;
                q.add(new Point(nextX, nextY));
                
            }
        }
        return -1;
    }
}

@alstjr7437 alstjr7437 merged commit 0d211e0 into main Aug 6, 2024
7 checks passed
@alstjr7437 alstjr7437 deleted the 31-alstjr7437 branch August 6, 2024 14:12
@alstjr7437 alstjr7437 restored the 31-alstjr7437 branch August 15, 2024 06:43
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.

3 participants