-
Notifications
You must be signed in to change notification settings - Fork 2
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
31-alstjr7437 #218
Conversation
There was a problem hiding this 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์ธ๋ฐ?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๊ฑด ํ์ด์ฌ ์ฝ๋๊ฐ ์๋๋ฐ ํ์ฅ์๊ฐ ํ์ด์ฌ์ด๋ค?!
There was a problem hiding this 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;
}
}
๐ ๋ฌธ์ ๋งํฌ
๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ
โ๏ธ ์์๋ ์๊ฐ
20๋ถ(๋๋ฌด ์ฌ์ด ๋ฌธ์ ๋ฅผ ํด๋ฒ๋ ธ๋ค์,, ํ๋ฃจ ์ข ์ผ ์ฝ๋ฉ์ ํ๋๋ ์ฒด๋ ฅ์ด ์์ด์ ์ฃ์กํฉ๋๋คใ ,,)
โจ ์๋ ์ฝ๋
๋ฌธ์ ๋ฅผ ๋ณด๊ณ ์ด,,, ๋๋ฌด ์ฌ์ด๋ฐ,,? ๊ทธ๋์ ํ์ด์ฌ์ผ๋ก ํ 10๋ถ๋ง์ ํ๊ณ
๋๋ฌด ๋ ๋จน์ธ ๊ฒ ๊ฐ๊ณ ,, Swift๋ก๋ ๊ฐ๋ฅํ ๊ฒ ๊ฐ์์ Swift๋ก๋ ์ง๋ดค์ต๋๋ค!!
๋ฐ๋ก ๋ก์ง์ ์ ๋ง ๊ฐ๋จํ BFS๋ผ ์ค๋ช ํ ๊ฒ ๋ ์๋ ์์ค์ ๋๋ค!!
๊ทธ๋ฅ ์์์นธ๋ถํฐ 1๋ก ์์ํด์ ๋ช์นธ์ ์ด๋ํด์ ๋์ฐฉ์ ์ ๊ฐ๋์ง ์ธ๋ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค!!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ด๋ฒ์ ๋ค๋ถ์บ ์ฒซ๋ ์ ํ๋ฉด์ inout Parameters์ ๋ํด ๋ฐฐ์ฐ๊ฒ ๋์ ๋ ธ์ ์ ์ ๋ฆฌํ์ต๋๋ค.(๋ค๋ถ์บ ์ ์ถ์ฉ๋ ์๊ณ ,,,,)