기술 블로그

7562번 나이트의 이동 본문

알고리즘 문제/BOJ

7562번 나이트의 이동

parkit 2018. 8. 22. 11:19
728x90
반응형

아주 기본적인 BFS 문제이다.


이런 문제는 틀리면 안 된다.



https://www.acmicpc.net/problem/7562




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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
 
using namespace std;
 
int T = 0// 테스트 케이스
 
int l = 0// 한 변의 길이
 
int BFS(int sy, int sx, int ey, int ex)
{
    queue<pair<intint> > q;
 
    bool visit[301][301= { false, };
 
    int ret = 0;
 
    q.push({ sy, sx });
 
    visit[sy][sx] = true;
 
    while (!q.empty())
    {
        int qSize = q.size();
 
        while (qSize--)
        {
            int y = q.front().first;
            int x = q.front().second;
 
            q.pop();
 
            if (y == ey && x == ex)
            {
                return ret;
            }
 
            if (x - 1 >= 0 && y - 2 >= 0 && !visit[y - 2][x - 1])
            {
                q.push({ y - 2, x - 1 });
                visit[y - 2][x - 1= true;
            }
 
            if (x - 2 >= 0 && y - 1 >= 0 && !visit[y - 1][x - 2])
            {
                q.push({ y - 1, x - 2 });
                visit[y - 1][x - 2= true;
            }
 
            if (x - 2 >= 0 && y + 1 < l && !visit[y + 1][x - 2])
            {
                q.push({ y + 1, x - 2 });
                visit[y + 1][x - 2= true;
            }
 
            if (x - 1 >= 0 && y + 2 < l && !visit[y + 2][x - 1])
            {
                q.push({ y + 2, x - 1 });
                visit[y + 2][x - 1= true;
            }
 
            if (x + 1 < l && y + 2 < l && !visit[y + 2][x + 1])
            {
                q.push({ y + 2, x + 1 });
                visit[y + 2][x + 1= true;
            }
 
            if (x + 2 < l && y + 1 < l && !visit[y + 1][x + 2])
            {
                q.push({ y + 1, x + 2 });
                visit[y + 1][x + 2= true;
            }
 
            if (x + 2 < l && y - 1 >= 0 && !visit[y - 1][x + 2])
            {
                q.push({ y - 1, x + 2 });
                visit[y - 1][x + 2= true;
            }
 
            if (x + 1 < l && y - 2 >= 0 && !visit[y - 2][x + 1])
            {
                q.push({ y - 2, x + 1 });
                visit[y - 2][x + 1= true;
            }
        }
 
        ++ret;
    }
}
 
int main(void)
{
    int start[2= { 0, }; // [0] : y(행), [1] : x(열)
 
    int end[2= { 0, }; // 나이트가 이동하려는 칸(도착지)
 
    scanf("%d"&T);
 
    while (T--)
    {
        scanf("%d"&l);
 
        scanf("%d %d"&start[0], &start[1]);
 
        scanf("%d %d"&end[0], &end[1]);
 
        printf("%d\n", BFS(start[0], start[1], end[0], end[1]));
    }
 
    return 0;
}
 
cs


728x90
반응형

'알고리즘 문제 > BOJ' 카테고리의 다른 글

15683번 감시  (0) 2018.08.22
11053번 가장 긴 증가하는 부분 수열  (0) 2018.08.22
2206번 벽 부수고 이동하기  (0) 2018.08.21
10989번 수 정렬하기 3  (0) 2018.08.21
13460번 구슬 탈출 2  (0) 2018.08.21