-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.cpp
70 lines (54 loc) · 1.63 KB
/
A.cpp
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
/*
A - 马的遍历
*/
#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int, int>;
const vector<Pair> directions = { Pair( 1, 2 ), Pair( 2, 1 ), Pair( 2, -1 ),
Pair( 1, -2 ), Pair( -1, -2 ), Pair( -2, -1 ),
Pair( -2, 1 ), Pair( -1, 2 ) };
const int N = 9;
// 该点是否被访问
vector<vector<bool>> visited( N, vector<bool>( N, false ) );
// 到该点要走几步
vector<vector<int>> steps( N, vector<int>( N, -1 ) );
void traverse( Pair &start, Pair &end ) {
visited[ start.first ][ start.second ] = true;
steps[ start.first ][ start.second ] = 0;
queue<Pair> q;
q.push( start );
bool found = false;
while ( !q.empty() ) {
Pair next = q.front();
q.pop();
for ( const auto d : directions ) {
int x = next.first + d.first;
int y = next.second + d.second;
if ( x <= 0 || x >= N || y <= 0 || y >= N ) continue;
if ( !visited[ x ][ y ] ) {
visited[ x ][ y ] = true;
steps[ x ][ y ] = steps[ next.first ][ next.second ] + 1;
if ( x == end.first && y == end.second ) {
found = true;
break;
}
Pair pre( x, y );
q.push( pre );
}
}
if ( found ) break;
}
}
int main() {
Pair start, end;
scanf( "%d%d%d%d", &start.first, &start.second, &end.first, &end.second );
traverse( start, end );
printf( "%d\n", steps[ end.first ][ end.second ] );
return 0;
}
/*
Sample Input
5 2 5 4
Sample Output
2
*/