-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteste1.cpp
More file actions
109 lines (85 loc) · 2.09 KB
/
Copy pathteste1.cpp
File metadata and controls
109 lines (85 loc) · 2.09 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
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
int pontos[1010][1010],X,Y;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
struct data{
int x,y;
int d,ini;
data(int x_,int y_,int d_,int ini_): x(x_), y(y_), d(d_), ini(ini_) {}
};
inline int calc(pair<int,int> a, pair<int,int> b){
return abs(a.first-b.first)+abs(a.second-b.second);
}
inline bool valid(int x,int y){
if(y < 0 or y >= Y) return false;
if(x < 0 or x >= X) return false;
return true;
}
inline bool compare(data a,data b){
return a.ini == b.ini? a.d > b.d: a.ini < b.ini;
}
int bfs(int x,int y, int d){
queue<pair<int,pair<int,int> > > q;
bool visi[X][X];
memset(visi,false,sizeof visi);
q.push(make_pair(0,make_pair(x,y)));
visi[x][y] = true;
while(!q.empty()){
int dis = q.front().first;
int xi = q.front().second.first;
int yi = q.front().second.second;
q.pop();
if(pontos[xi][yi] == 2)
return dis;
for(int i=0;i<4;i++){
int xa = xi+dx[i];
int ya = yi+dy[i];
if(!visi[xa][ya] and valid(xa,ya) and dis+1 < d){
visi[xa][ya] = true;
q.push(make_pair(dis+1,make_pair(xa,ya)));
}
}
}
return d;
}
pair<int,int> dijkstra(int xi,int yi, int xf, int yf,int d){
priority_queue<data, vector<data> , function<bool(data,data)> > pq(compare);
pontos[xi][yi] = 1;
pq.push(data(xi,yi,0,d));
while(!pq.empty()){
int dis = pq.top().d;
int xv = pq.top().x;
int yv = pq.top().y;
int ini = pq.top().ini;
pq.pop();
if(xv == xf and yv==yf)
return make_pair(ini,dis);
for(int i=0;i<4;i++){
int xa = xv+dx[i];
int ya = yv+dy[i];
if(pontos[xa][ya]==0 and valid(xa,ya)){
pontos[xa][ya] = 1;
pq.push(data(xa,ya,dis+1,bfs(xa,ya,ini)));
}
}
}
}
main(){
int k,n,xi,yi,xf,yf,d,x,y;
cin >> k;
for(int z=0;z<k;z++){
memset(pontos,0,sizeof pontos);
cin >> n >> X >> Y;
cin >> xi >> yi >> xf >> yf;
d = 10101010;
pair<int,int> aux = make_pair(xi,yi);
for(int i=0;i<n;i++){
cin >> x >> y;
pontos[x][y] = 2;
d = min(d,calc(make_pair(x,y),aux));
}
pair<int,int> ans = dijkstra(xi,yi,xf,yf,d);
cout << ans.first << " " << ans.second << endl;
}
}