일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- data structure
- Database
- algogritim
- Programmers
- language
- D3
- 운영체제
- BOJ
- 알고리즘
- c++
- swea
- algorithm
- 재테크/투자
- OS
- 데이터베이스
- 감상문
- 독서
- 문제풀이
- 네트워크
- D2
- 프로그래머스
- 법의학
- 자료구조
- cs
- db
- SW Expert Academy
- network
- Computer Science
- 백준
- LeetCode
Archives
- Today
- Total
선택은 나의 것
[BOJ 백준] 16948번 데스 나이트 본문
문제
BOJ 16948 : https://www.acmicpc.net/problem/16948
16948번: 데스 나이트
게임을 좋아하는 큐브러버는 체스에서 사용할 새로운 말 "데스 나이트"를 만들었다. 데스 나이트가 있는 곳이 (r, c)라면, (r-2, c-1), (r-2, c+1), (r, c-2), (r, c+2), (r+2, c-1), (r+2, c+1)로 이동할 수 있다. 크
www.acmicpc.net
접근
BFS를 통해 데스 나이트가 이동 가능한 경로를 모두 탐색하여 (r1, c1)에서 (r2, c2)로 이동이 가능한지 검사해 보았다.
코드
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n;
pair<int, int> a, b;
cin >> n >> a.first >> a.second >> b.first >> b.second;
int dx[] = {-2, -2, 0, 0, 2, 2};
int dy[] = {-1, 1, -2, 2, -1, 1};
int map[200][200];
int dist[200][200];
queue<pair<int, int>> q;
dist[a.first][a.second] = 1;
q.push(a);
while (!q.empty()) {
pair<int, int> front = q.front();
q.pop();
for (int i = 0; i < 6; i++) {
int newx = front.first + dx[i];
int newy = front.second + dy[i];
if (0 <= newx && newx < n && 0 <= newy && newy < n && !dist[newx][newy]) {
if (newx == b.first && newy == b.second) {
cout << dist[front.first][front.second];
return 0;
}
q.push({newx, newy});
dist[newx][newy] = dist[front.first][front.second] + 1;
}
}
}
cout << -1;
return 0;
}
'☽ Algorithm > BOJ' 카테고리의 다른 글
[BOJ 백준] 1668번 트로피 진열 (python) (0) | 2020.08.03 |
---|---|
[BOJ 백준] 2857번 FBI (python) (0) | 2020.08.03 |
[BOJ 백준] 14225번 부분수열의 합 (0) | 2020.07.31 |
[BOJ 백준] 16917번 양념 반 후라이드 반 (0) | 2020.07.29 |
[BOJ 백준] 16968번 차량 번호판 1 (0) | 2020.07.29 |
Comments