-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-04 (1).cpp
More file actions
75 lines (61 loc) · 1.66 KB
/
Copy path4-04 (1).cpp
File metadata and controls
75 lines (61 loc) · 1.66 KB
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
//7562
#include <iostream>
#include <algorithm>
#include <queue>
#define fastio ios::sync_with_stdio(0),cout.tie(0),cin.tie(0)
using namespace std;
struct pos{
int x;
int y;
};
int arr[301][301];
bool visited[301][301];
int dx[8] = { -1,-2,-1,-2,1,1,2,2 };
int dy[8] = { -2,-1,2,1,2,-2,1,-1 };
void bfs(int size, pos start,pos finish){
pos nowpos,newpos;
queue <pos> pq;
pq.push(start);
visited[start.x][start.y] = true;
arr[start.x][start.y] = 0;
while(!pq.empty()){
if(visited[finish.x][finish.y] == true)
break;
nowpos = pq.front();
pq.pop();
for(int i = 0 ; i < 8 ; i++)
{
newpos.x = nowpos.x + dx[i];
newpos.y = nowpos.y + dy[i];
if(newpos.x >= 0 && newpos.x <= size-1 && newpos.y >= 0 && newpos.y <= size-1 )
{
if(visited[newpos.x][newpos.y] == false)
{
visited[newpos.x][newpos.y] = true;
arr[newpos.x][newpos.y] = arr[nowpos.x][nowpos.y] +1;
pq.push(newpos);
}
}
}
}
cout<<arr[finish.x][finish.y]<<"\n";
}
int main(void)
{
int n,size;
cin>>n;
pos start,finish;
for(int i =0 ; i < n ; i++)
{
cin>>size;
for(int i = 0 ; i < size ; i++)
for(int j = 0 ; j < size ; j++)
{
visited[i][j] = false;
arr[i][j] = 0;
}
cin>>start.x>>start.y;
cin>>finish.x>>finish.y;
bfs(size,start,finish);
}
}