일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- kaggle
- Class
- 시바견
- Substring with Concatenation of All Words
- Python Code
- Python
- attribute
- Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- 컴퓨터의 구조
- 715. Range Module
- DWG
- Decorator
- LeetCode
- 파이썬
- 운영체제
- shiba
- 30. Substring with Concatenation of All Words
- iterator
- concurrency
- 109. Convert Sorted List to Binary Search Tree
- t1
- Protocol
- 밴픽
- Python Implementation
- 315. Count of Smaller Numbers After Self
- Regular Expression
- data science
- Generator
- 프로그래머스
Archives
- Today
- Total
Scribbling
[Programmers] 게임 맵 최단거리 본문
https://school.programmers.co.kr/learn/courses/30/lessons/1844?language=python3
1. Python
from collections import deque
def solution(maps):
m, n = len(maps), len(maps[0])
q = deque()
q.append((1, 0, 0))
visited = set()
visited.add((0, 0))
dy, dx = [0, 1, 0, -1], [1, 0, -1, 0]
while q:
t, y, x = q.popleft()
if y == m - 1 and x == n - 1:
return t
for d in range(4):
ny, nx = y + dy[d], x + dx[d]
if ny < 0 or ny >= m or nx < 0 or nx >= n:
continue
if maps[ny][nx] == 1 and (ny, nx) not in visited:
q.append((t + 1, ny, nx))
visited.add((ny, nx))
return -1
2. C++
int solution(vector<vector<int>> maps)
{
int m = maps.size();
int n = maps[0].size();
deque<vector<int>> q;
q.push_back({1, 0, 0});
set<vector<int>> visited;
visited.insert({0, 0});
int dy[4] {0, 1, 0, -1};
int dx[4] {1, 0, -1, 0};
while (not q.empty()) {
vector<int> tmp = q.front();
int t = tmp[0];
int y = tmp[1];
int x = tmp[2];
q.pop_front();
if (y == m - 1 and x == n - 1) {
return t;
}
for (int d=0; d<4; d++) {
int ny = y + dy[d];
int nx = x + dx[d];
if (ny < 0 or ny >= m or nx < 0 or nx >= n) continue;
if (maps[ny][nx] == 0) continue;
if (visited.find({ny, nx}) == visited.end()) {
q.push_back({t+1, ny, nx});
visited.insert({ny, nx});
}
}
}
return -1;
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[Programmers] 아이템 줍기 (0) | 2024.06.21 |
---|---|
[Programmers] 단어 변환 (0) | 2024.06.20 |
[Programmers] 네트워크 (0) | 2024.06.20 |
[Programmers] 타겟 넘버 (0) | 2024.06.20 |
[Programmers] 도둑질 (0) | 2024.06.18 |