안선생의 개발 블로그

C++ 16948 데스나이트 본문

백준

C++ 16948 데스나이트

안선생 2023. 2. 24. 22:22
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
#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
using namespace std;
 
 
int g[501][501= {0};
bool visit[501][501= {0};
int dx[6= { -2,-2,0,0,2 ,2};
int dy[6= { -1,1,-2,2 ,-1,1};
int n, m,k,r1,r2,c1,c2;
int wolf = 0;
int sheep = 0;
int bfs(int x,int y)
{
    queue <pair<int,int>> a;
    a.push({ x, y });
    visit[x][y] = 1;
    while (!a.empty())
    {
        int xx = a.front().first;
        int yy = a.front().second;
        if(xx == r2 && yy == c2)
            return g[xx][yy];
        a.pop();
        for(int i =0;i<6; i++)
        {
            int x1 = xx + dx[i];
            int y1 = yy + dy[i];
            if (x1 <0 || x1>|| y1<0 || y1 > n || visit[x1][y1]) continue;       
                
            a.push({ x1, y1 });
            visit[x1][y1] = 1;
            g[x1][y1] = g[xx][yy] + 1;
            
        }
        
    }
    return -1;
}
 
 
int main()
{
    cin >> n >> r1 >> c1 >> r2>> c2;
   
    cout << bfs(r1, c1);
    return 0;
}
cs

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

 

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

C++ 16953 A ->B  (0) 2023.02.25
C++ 11123 양 한마리... 양 두마리...  (0) 2023.02.24
C++ 3184 양  (1) 2023.02.24
C++ 1697 숨바꼭질  (0) 2023.02.23
C++ 7576 토마토  (0) 2023.02.18