-
Notifications
You must be signed in to change notification settings - Fork 1
/
[백준 알고리즘] 나이트의 이동.txt
72 lines (58 loc) · 1.65 KB
/
[백준 알고리즘] 나이트의 이동.txt
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
//나이트의 이동
//1. 그냥 방향타만 바꾸면됌.
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef struct cell{
int r,c;
int count;
}Cell;
int rdir[8] = {-1,-2,-2,-1,1,2,2,1};
int cdir[8] = {-2,-1,1,2,2,1,-1,-2};
bool visit[303][303];
int result=0;
int main(){
int test = 0;
scanf("%d", &test);
for(int t = 0; t<test; t++){
Cell start;
Cell target;
int n;
scanf("%d", &n);
scanf("%d %d", &start.r, &start.c);
scanf("%d %d", &target.r, &target.c);
for(int i =0;i<n; i++){
for(int j =0; j<n; j++){
visit[i][j] = false;
}
}
queue<Cell> root;
root.push({start.r, start.c, 0});
visit[start.r][start.c] = true;
while(!root.empty()){
Cell temp = root.front();
root.pop();
// printf("%d %d\n", temp.r, temp.c);
if(temp.r== target.r && temp.c == target.c){
result = temp.count;
}
else{
for(int i=0; i<8; i++){
int nowR = temp.r + rdir[i];
int nowC = temp.c + cdir[i];
if(0<=nowR && nowR<n && 0<= nowC && nowC<n && visit[nowR][nowC] == false){
visit[nowR][nowC] = true;
root.push({nowR, nowC,temp.count+1});
}
}
}
}
while(!root.empty()){
root.pop();
}
printf("%d\n", result);
}
return 0;
}