-
백준 16948번 데스 나이트PS 2020. 3. 19. 11:43
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)로 이동할 수 있다. 크기가 N×N인 체스판과 두 칸 (r1, c1), (r2, c2)가 주어진다. 데스 나이트가 (r1, c1)에서 (r2, c2)로 이동하는 최소 이동 횟수를 구해보자. 체스판의 행과 열은 0번부
www.acmicpc.net
<접근방법>
bfs 문제입니다.
이 문제는 이동방향이 상, 하, 좌, 우가 아닙니다.
문제에서 주어진대로 이동방향을 정의하면 됩니다.
<느낀 점>
<코드>
#include <iostream> #include <algorithm> #include <queue> using namespace std; struct point { int y, x; }; int n, ay, ax, by, bx, map[200][200], res; bool visit[200][200]; int dy[6] = {-2, -2, 0, 0, 2, 2}; int dx[6] = {-1, 1, -2, 2, -1, 1}; queue<point> q; int bfs() { q.push({ay, ax}); visit[ay][ax] = true; while (!q.empty()) { int sz = q.size(); while (sz--) { point t = q.front(); q.pop(); if (t.y == by && t.x == bx) { return res; } for (int i = 0; i < 6; i++) { int ny = t.y + dy[i]; int nx = t.x + dx[i]; if (ny < 0 || nx < 0 || ny >= n || nx >= n || visit[ny][nx]) continue; visit[ny][nx] = true; q.push({ ny, nx}); } } res++; } return -1; } int main() { cin >> n; cin >> ay >> ax >> by >> bx; if (ay == by && ax == bx) { cout << 0 << '\n'; exit(0); } cout << bfs() << '\n'; return 0; }
반응형'PS' 카테고리의 다른 글
백준 16931번 겉넓이 구하기 (0) 2020.03.21 백준 16974번 레벨 햄버거 (0) 2020.03.19 백준 16928번 뱀과 사다리 게임 (0) 2020.03.19 백준 16968번 차량 번호판1 (0) 2020.03.19 백준 17085번 십자가 2개 놓기 (1) 2020.03.18