-
Notifications
You must be signed in to change notification settings - Fork 48
/
1111.c
105 lines (67 loc) · 1.65 KB
/
1111.c
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
/*
@author: Malbolge;
@date: 26/02/2020;
@name: Desrugenstein;
*/
#include <stdio.h>
#include <stdbool.h>
#define INF 0x3f3f3f3fU
#define MAXSIZE 123
typedef unsigned uint;
uint g[MAXSIZE][MAXSIZE];
void fw(int);
uint min(uint, uint);
int main(int argc, char **argv)
{
int u, v, w, n, z, x;
while (scanf("%d%*c", &n), n)
{
for (int i = 0; i < n * n; ++i)
{
for (int j = 0; j < n * n; ++j)
g[i][j] = INF;
g[i][i] = 0U;
}
for (int i = n - 1; i >= 0; --i)
{
for (int j = 0; j < n; ++j)
{
scanf("%d %d %d %d", &u, &v, &z, &w);
if (u == 1)
g[i * n + j][(i + 1) * n + j] = 1U;
if (v == 1)
g[i * n + j][(i - 1) * n + j] = 1U;
if (z == 1)
g[i * n + j][i * n - 1 + j] = 1U;
if (w == 1)
g[i * n + j][i * n + 1 + j] = 1U;
}
}
x = n;
fw(n * n);
scanf("%d", &n);
while (n--)
{
scanf("%d %d %d %d", &u, &v, &z, &w);
u = u + v * x;
z = z + w * x;
if (g[u][z] != INF)
printf("%u\n", g[u][z]);
else
puts("Impossible");
}
putchar_unlocked('\n');
}
return 0;
}
void fw(int n)
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k)
g[j][k] = min(g[j][k], g[j][i] + g[i][k]);
}
inline uint min(uint a, uint b)
{
return a < b ? a : b;
}