본문 바로가기
백준

백준 16948 데스 나이트

by 콩순이냉장고 2020. 7. 31.

문제 URL : 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를 이용할줄 알고 bfs로 문제 조건대로 갈수있는 방향을 가면 되기 때문에

그대로 하드 코딩해도 문제가 쉽게 풀립니다. 굳이 하나 제약조건 이있다면 범위가 좌표 범위가 0<=y<n , 0<= x<n 이라는것 외에는 따로 설명드릴게 없습니다.

 

소스코드 : 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int visit[201][201];
int r1, c1, r2, c2;
int n;
int dy[6= { -2-20022 };
int dx[6= { -11-22-11 };
bool isrange(int y, int x)
{
    if (0 <= y&&< n && 0 <= x&&< n)
        return true;
    return false;
}
int bfs()
{
    int cnt = 0;
    queue<pair<intint>> q;
    q.push({ r1, c1 });
    visit[r1][c1] = 1;
    while (!q.empty())
    {
        int qsize = q.size();
        while (qsize--){
            int y = q.front().first;
            int x = q.front().second;
            q.pop();
            if (y == r2&&== c2)//도착지점
                return cnt;
            for (int i = 0; i < 6; i++){
                int ny = y + dy[i];
                int nx = x + dx[i];
                if (isrange(ny, nx) && !visit[ny][nx]){
                    visit[ny][nx] = 1;
                    q.push({ ny, nx });
                }
            }
        }
        cnt++;
    }
    return -1;//갈수없음
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    cin >> r1 >> c1 >> r2 >> c2;
    cout << bfs() << "\n";
}
cs

 

궁금한점이나 모르는 점이있따면 언제든지 댓글을 이용해주시길 바랍니다.

 

'백준' 카테고리의 다른 글

백준 16197 두 동전  (0) 2020.07.31
백준 14225 부분수열의 합  (0) 2020.07.31
백준 1463 1로 만들기  (0) 2020.07.27
백준 10825 국영수  (0) 2020.07.27
백준 17834 사자와 토끼  (0) 2020.07.26