#include <bits/stdc++.h> #include <algorithm> #include <numeric> using namespace std; #define pb(x) push_back(x) #define f first #define s second int n, m, q; bool dl[10]; int stans; // liczba roznych stanow = NWW(dlugosci s_(i, j)) vector<vector<string>> S; // wejscie S[i][j] = s_(i,j) vector<vector<vector<pair<int, int>>>> STANY; vector<vector<pair<int, int>>> A; // pojedynczy stan vector<vector<pair<int, int>>> B; int LCM(int a, int b) { return (a * b) / __gcd(a, b); } void zacznij() { string a; cin >> n >> m >> q; for (int i = 0; i < n; i++) { vector<string> t; for (int j = 0; j < m; j++) { cin >> a; t.pb(a); dl[a.length()] = true; } S.pb(t); } for (int i = 1; i <= 8; i++) if (dl[i]) { stans = i; break; } for (int i = 1; i <= 8; i++) if (dl[i]) stans = LCM(stans, i); return; } pair<int, int> Find(pair<int, int> x) { if (A[x.f][x.s] != x) A[x.f][x.s] = Find(A[x.f][x.s]); return A[x.f][x.s]; } void Union(pair<int, int> x, pair<int, int> y) { pair<int, int> p = Find(x); A[p.f][p.s] = Find(y); return; } pair<int, int> Find_B(pair<int, int> x) { if (B[x.f][x.s] != x) B[x.f][x.s] = Find_B(B[x.f][x.s]); return B[x.f][x.s]; } void Union_B(pair<int, int> x, pair<int, int> y) { pair<int, int> p = Find_B(x); B[p.f][p.s] = Find_B(y); return; } bool zielone_p(int i, int j, int k) { string s; if (i < n) { s = S[i][j]; if (s[k % s.length()] == '1') return true; } if (i > 0) { s = S[i - 1][j]; if (s[k % s.length()] == '1') return true; } return false; } bool zielone_d(int i, int j, int k) { string s; if (j < m) { s = S[i][j]; if (s[k % s.length()] == '0') return true; } if (j > 0) { s = S[i][j - 1]; if (s[k % s.length()] == '0') return true; } return false; } void make_stan(int k) { A.clear(); for (int i = 0; i <= n; i++) { vector<pair<int, int>> t; for (int j = 0; j <= m; j++) t.pb(make_pair(i, j)); A.pb(t); } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (j < m && zielone_p(i, j, k)) Union({i, j}, {i, j + 1}); if (i < n && zielone_d(i, j, k)) Union({i, j}, {i + 1, j}); } } STANY.push_back(A); return; } long long ROZW(int t0, pair<int, int> a, pair<int, int> b) { int ile = 0; A = STANY[t0]; while (Find(a) != Find(b)) { ile++; B = STANY[(t0 + ile) % stans]; pair<int, int> fa = Find(a); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (Find({i, j}) == fa) Union_B({i, j}, a); } } A = B; } return ile; } void odpowiadaj() { long long t; int a, b, c, d; for (int i = 0; i < q; i++) { cin >> t >> a >> b >> c >> d; pair<int, int> p1 = {a, b}; pair<int, int> p2 = {c, d}; cout << t + ROZW(t % stans, p1, p2) << "\n"; } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); zacznij(); for (int i = 0; i < stans; i++) make_stan(i); odpowiadaj(); return 0; }
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 200 201 202 203 204 205 206 207 | #include <bits/stdc++.h> #include <algorithm> #include <numeric> using namespace std; #define pb(x) push_back(x) #define f first #define s second int n, m, q; bool dl[10]; int stans; // liczba roznych stanow = NWW(dlugosci s_(i, j)) vector<vector<string>> S; // wejscie S[i][j] = s_(i,j) vector<vector<vector<pair<int, int>>>> STANY; vector<vector<pair<int, int>>> A; // pojedynczy stan vector<vector<pair<int, int>>> B; int LCM(int a, int b) { return (a * b) / __gcd(a, b); } void zacznij() { string a; cin >> n >> m >> q; for (int i = 0; i < n; i++) { vector<string> t; for (int j = 0; j < m; j++) { cin >> a; t.pb(a); dl[a.length()] = true; } S.pb(t); } for (int i = 1; i <= 8; i++) if (dl[i]) { stans = i; break; } for (int i = 1; i <= 8; i++) if (dl[i]) stans = LCM(stans, i); return; } pair<int, int> Find(pair<int, int> x) { if (A[x.f][x.s] != x) A[x.f][x.s] = Find(A[x.f][x.s]); return A[x.f][x.s]; } void Union(pair<int, int> x, pair<int, int> y) { pair<int, int> p = Find(x); A[p.f][p.s] = Find(y); return; } pair<int, int> Find_B(pair<int, int> x) { if (B[x.f][x.s] != x) B[x.f][x.s] = Find_B(B[x.f][x.s]); return B[x.f][x.s]; } void Union_B(pair<int, int> x, pair<int, int> y) { pair<int, int> p = Find_B(x); B[p.f][p.s] = Find_B(y); return; } bool zielone_p(int i, int j, int k) { string s; if (i < n) { s = S[i][j]; if (s[k % s.length()] == '1') return true; } if (i > 0) { s = S[i - 1][j]; if (s[k % s.length()] == '1') return true; } return false; } bool zielone_d(int i, int j, int k) { string s; if (j < m) { s = S[i][j]; if (s[k % s.length()] == '0') return true; } if (j > 0) { s = S[i][j - 1]; if (s[k % s.length()] == '0') return true; } return false; } void make_stan(int k) { A.clear(); for (int i = 0; i <= n; i++) { vector<pair<int, int>> t; for (int j = 0; j <= m; j++) t.pb(make_pair(i, j)); A.pb(t); } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (j < m && zielone_p(i, j, k)) Union({i, j}, {i, j + 1}); if (i < n && zielone_d(i, j, k)) Union({i, j}, {i + 1, j}); } } STANY.push_back(A); return; } long long ROZW(int t0, pair<int, int> a, pair<int, int> b) { int ile = 0; A = STANY[t0]; while (Find(a) != Find(b)) { ile++; B = STANY[(t0 + ile) % stans]; pair<int, int> fa = Find(a); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (Find({i, j}) == fa) Union_B({i, j}, a); } } A = B; } return ile; } void odpowiadaj() { long long t; int a, b, c, d; for (int i = 0; i < q; i++) { cin >> t >> a >> b >> c >> d; pair<int, int> p1 = {a, b}; pair<int, int> p2 = {c, d}; cout << t + ROZW(t % stans, p1, p2) << "\n"; } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); zacznij(); for (int i = 0; i < stans; i++) make_stan(i); odpowiadaj(); return 0; } |