-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuva-11352.cpp
More file actions
98 lines (81 loc) · 2.08 KB
/
uva-11352.cpp
File metadata and controls
98 lines (81 loc) · 2.08 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//uva 11352
//Crazy King
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
int BFS(vector < vector <bool> > & Graph, int xA, int yA, int xB, int yB);
int main(void)
{
int T = 0;
cin >> T;
while(T--){
int m, n;
cin >> m >> n;
vector <string> board(m);
for(int i = 0; i < m; ++i){
string tmp;
cin >> tmp;
board[i] = tmp;
}
int xA, yA;
int xB, yB;
vector < vector <bool> > Graph(m, vector <bool> (n, true));
//convert board to graph
int horse_x[] = {-2, -2, -1, -1, 0, 1, 1, 2, 2};
int horse_y[] = {-1, 1, -2, 2, 0, -2, 2, -1, 1};
int size = sizeof(horse_x) / sizeof(int);
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
if(board[i][j] == 'Z')
for(int k = 0; k < size; ++k){
int x1 = i + horse_x[k];
int y1 = j + horse_y[k];
if(x1 >= 0 && x1 < m && y1 >= 0 && y1 < n)
Graph[x1][y1] = 0;
}
else if(board[i][j] == 'A'){
xA = i;
yA = j;
}
else if(board[i][j] == 'B'){
xB = i;
yB = j;
}
Graph[xA][yA] = Graph[xB][yB] = 1;//moving to A and B is always possible
int result = BFS(Graph, xA, yA, xB, yB);
if(result == -1)
cout << "King Peter, you can't go now!" << endl;
else
cout << "Minimal possible length of a trip is " << result << endl;
}
return 0;
}
int BFS(vector < vector <bool> > & Graph, int xA, int yA, int xB, int yB)
{
int m = Graph.size();
int n = Graph[0].size();
queue < pair <int, pair <int, int> > > Queue;
Queue.push(make_pair(0, make_pair(xA, yA)));
int king_x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int king_y[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int size = sizeof(king_x) / sizeof(int);
while(!Queue.empty()){
int l = Queue.front().first;
int x = Queue.front().second.first;
int y = Queue.front().second.second;
Queue.pop();
if(xB == x && yB == y)
return l;
for(int i = 0; i < size; ++i){
int x1 = x + king_x[i];
int y1 = y + king_y[i];
if(x1 >= 0 && x1 < m && y1 >= 0 && y1 < n && Graph[x1][y1]){
Graph[x1][y1] = 0;
Queue.push(make_pair(l + 1, make_pair(x1, y1)));
}
}
}
return -1;
}