-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10067.cpp
More file actions
199 lines (189 loc) · 3.46 KB
/
10067.cpp
File metadata and controls
199 lines (189 loc) · 3.46 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<set>
using namespace std;
// Define Some Variables
#define eps 1e-14
#define si 100010
#define pi acos(-1.0)
#define inf (1<<30)-1
#define mod 1000000000 //10^9
//Define Some Functions
#define even(a) ((a)%2==0)
#define odd(a) ((a)%2==1)
#define max(a,b) (a>b ?a:b)
#define min(a,b) (a<b ?a:b)
#define sqr(a)((a)*(a))
#define area(x1,y1,x2,y2,x3,y3) (x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)) //Area of a triangle
#define distance(x1,y1,x2,y2) (sqr(x1-x2)+sqr(y1-y2)) //Distance between two points
#define mem(a,v) memset(a,v,sizeof(a))
inline bool compare( double a, double b ) { return fabs( a-b ) < 1e-9 ; }
#define fr(i,a,b) for(i=a;i<=b;i++)
#define rep(i,a,b) for(i=a;i<b;i++)
#define rev(i,a,b) for(i=a;i>=b;i--)
//typedef __int64 i64;
int dw[]={1,0,0,0,-1,0,0,0};
int dx[]={0,1,0,0,0,-1,0,0};
int dy[]={0,0,1,0,0,0,-1,0};
int dz[]={0,0,0,1,0,0,0,-1};
int i,j,l,n,fg,a1[si][15],a2[si][15],a3[si][15],a4[si][15],st[5],ed[5],cst[15][15][15][15],vis[15][15][15][15],k;
queue<int>qq;
/*
i64 bmod(i64 a,i64 b)
{
if(b==0)
return 1;
i64 x=bmod(a,b/2);
x=(x*x)%mod;
if(b%2==1)
x=(x*a)%mod;
return x;
}*/
int gcd(int a,int b)
{
while(b>0)
{
a%=b;
a^=b;
b^=a;
a^=b;
}
return a;
}
/*
i64 lcm(i64 a,i64 b)
{
return ((a*b)/gcd(a,b));
}
*/
void bfs(int p,int q,int r,int s)
{
int nwp,nwq,nwr,nws,fnd;
qq.push(p);
qq.push(q);
qq.push(r);
qq.push(s);
while(!qq.empty())
{
p=qq.front();
qq.pop();
q=qq.front();
qq.pop();
r=qq.front();
qq.pop();
s=qq.front();
qq.pop();
if(!fg)
{
rep(i,0,8)
{
nwp=p+dw[i];
nwq=q+dx[i];
nwr=r+dy[i];
nws=s+dz[i];
if(nwp>9)
nwp=0;
else if(nwp<0)
nwp=9;
if(nwq>9)
nwq=0;
else if(nwq<0)
nwq=9;
if(nwr>9)
nwr=0;
else if(nwr<0)
nwr=9;
if(nws>9)
nws=0;
else if(nws<0)
nws=9;
if(vis[nwp][nwq][nwr][nws]==0&&!fg)
{
vis[nwp][nwq][nwr][nws]=1;
fnd=1;
rep(j,0,n)
{
if(a1[j][nwp]&&a2[j][nwq]&&a3[j][nwr]&&a4[j][nws])
{
fnd=0;
break;
}
}
if(!fnd)
continue;
cst[nwp][nwq][nwr][nws]=cst[p][q][r][s]+1;
if(nwp==ed[0]&&nwq==ed[1]&&nwr==ed[2]&&nws==ed[3])
{
fg=1;
break;
}
qq.push(nwp);
qq.push(nwq);
qq.push(nwr);
qq.push(nws);
}
}
}
}
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t,p,q,r,s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&st[0],&st[1],&st[2],&st[3]);
scanf("%d%d%d%d",&ed[0],&ed[1],&ed[2],&ed[3]);
fg=0;
scanf("%d",&n);
rep(i,0,n)
{
rep(j,0,10)
a1[i][j]=a2[i][j]=a3[i][j]=a4[i][j]=0;
scanf("%d%d%d%d",&p,&q,&r,&s);
if(st[0]==p&&st[1]==q&&st[2]==r&&st[3]==s)
fg=1;
else if(ed[0]==p&&ed[1]==q&&ed[2]==r&&ed[3]==s)
fg=1;
a1[i][p]=a2[i][q]=a3[i][r]=a4[i][s]=1;
}
if(fg)
{
puts("-1");
continue;
}
if(st[0]==ed[0]&&st[1]==ed[1]&&st[2]==ed[2]&&st[3]==ed[3])
{
puts("0");
continue;
}
rep(i,0,10)
{
rep(j,0,10)
{
rep(k,0,10)
{
rep(l,0,10)
vis[i][j][k][l]=cst[i][j][k][l]=0;
}
}
}
bfs(st[0],st[1],st[2],st[3]);
if(fg)
printf("%d\n",cst[ed[0]][ed[1]][ed[2]][ed[3]]);
else
puts("-1");
}
return 0;
}