문제 출처 : https://www.acmicpc.net/problem/7562
<C++Code>
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
|
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int v[301][301];
int d[301][301];
int x[] = {-2,-1,1,2,2,1,-1,-2};
int y[] = {1,2,2,1,-1,-2,-2,-1};
int main(void)
{
int k;
cin>>k;
while(k--)
{
memset(v,-1,sizeof(v));
int n;
int si,sj,ei,ej;
cin>>n;
cin>>si>>sj;
cin>>ei>>ej;
queue<pair<int,int> > q;
q.push({si,sj});
v[si][sj]=0;
while(!q.empty())
{
int pox=q.front().first;
int poy=q.front().second;
q.pop();
if(pox==ei&&poy==ej)
{
break;
}
for(int i=0;i<8;i++)
{
int nx=pox+x[i];
int ny=poy+y[i];
if(nx<0||nx>=n||ny<0||ny>=n) continue;
if(v[nx][ny]==-1)
{
v[nx][ny]=v[pox][poy]+1;
q.push({nx,ny});
}
}
}
cout<<v[ei][ej]<<endl;
}
return 0;
}
|
cs |
'백준 온라인 저지 > BFS, DFS (그래프)' 카테고리의 다른 글
16947번_서울 지하철 2호선 (0) | 2019.12.14 |
---|---|
16929번_Two Dots (DFS) (0) | 2019.12.14 |
1707번_이분그래프 (0) | 2019.12.14 |
12851번_숨바꼭질 2 (0) | 2019.12.03 |
13549번_숨바꼭질 3 (0) | 2019.12.03 |