-
[백준/BOJ] 16948번 데스 나이트 (C++)알고리즘 문제풀이/백준 2021. 2. 9. 21:56
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를 통해 해결할 수 있는 간단한 문제였습니다.
#include <iostream> #include <queue> #include <cstring> using namespace std; int n, r1, c1, r2, c2; int map[201][201]; int dx[6] = {-2, -2, 0, 0, 2, 2}; int dy[6] = {-1, 1, -2, 2, -1, 1}; int BFS() { memset(map, -1, sizeof(map)); queue<pair<int, int > > q; map[r1][c1] = 0; q.push({ r1, c1 }); while(!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); if(x == r2 && y == c2) return map[x][y]; for(int i = 0; i < 6; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if(map[nx][ny] != -1 || nx < 0 || nx >= n || ny < 0 || ny >= n) continue; map[nx][ny] = map[x][y] + 1; q.push({ nx, ny }); } } return -1; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> r1 >> c1 >> r2 >> c2; cout << BFS(); }
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준/BOJ] 9251번 LCS (C++) (0) 2021.02.14 [백준/BOJ] 14502번 연구소 (C++) (0) 2021.02.10 [백준/BOJ] 16928번 뱀과 사다리 게임 (C++) (0) 2021.02.08 [백준/BOJ] 16198번 에너지 모으기 (C++) (0) 2021.02.04 [백준/BOJ] 2748번 피보나치 수 2 (C++) (0) 2021.02.03