//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //~ while (clock()<=69*CLOCKS_PER_SEC) #define ll long long #define ld long double #define pi pair<int,int> #define pd pair<ld,ld> #define ft first #define sd second #define st first #define nd second #define mp make_pair #define pb push_back #define eb emplace_back #define FOR(i,a,b) for(int i=(a); i<=(b);i++) #define F(i,a,b) FOR(i,(a),(b)-1) #define REV(i,a,b) for(int i=(a); i>=(b);i--) #define VI vector<int> #define VPI vector<pi> #define VPD vector<pd> #define PI 3.14159265 #define all(x) (x).begin(), (x).end() #define sz(a) (int)((a).size()) #define int long long template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long K = 1000; const long long KK = K*K; const long long MIL = KK*K; const long long INF = MIL*MIL; const long long MOD = 1e9 + 7; const long long N = 1e5 + 10, M=10; const int LCM = 820; // TODO zmienic int n, m, q; int hasz(int i, int j) { return i * (m + 1) + j; } pi odhasz(int i) { return {i / (m + 1), i % (m + 1)}; } vector< vector<string> > syg; vector< vector< VI > > spojne; vector< vector< VI > > G; vector< VI > odl; vector< VI > wartosci; int Find(int x, int c) { pi tmp = odhasz(x); int a = tmp.ft, b = tmp.sd; if (spojne[a][b][c] == hasz(a, b)) { return hasz(a, b); } else { return spojne[a][b][c] = Find(spojne[a][b][c], c); } } void Union(int a, int b, int c) { a = Find(a, c); b = Find(b, c); if (a != b) { pi pa = odhasz(a); pi pb = odhasz(b); spojne[pb.ft][pb.sd][c] = spojne[pa.ft][pa.sd][c]; } } struct wyd { int x, warstwa, odl; wyd(int a, int b, int c) : x(a), warstwa(b), odl(c) {} bool operator<(const wyd w) const { if (odl == w.odl) { return x < w.x; } return odl < w.odl; } }; void solve() { cin >> n >> m >> q; syg = vector< vector<string> >(n + 2, vector<string>(m + 2)); spojne = vector< vector< VI > >(n + 2, vector< VI > (m + 2, VI(LCM + 2))); wartosci = vector< VI >(LCM + 2); G = vector< vector< VI > >(LCM + 2); odl = vector< VI > (LCM + 2); FOR(i, 1, n) { FOR(j, 1, m) { cin >> syg[i][j]; } } FOR(i, 0, n) { FOR(j, 0, m) { F(k, 0, LCM) { spojne[i][j][k] = hasz(i, j); } } } FOR(i, 1, n) { FOR(j, 1, m) { F(k, 0, LCM) { if (syg[i][j][k % sz(syg[i][j])] == '0') { Union(hasz(i, j), hasz(i - 1, j), k); Union(hasz(i, j - 1), hasz(i - 1, j - 1), k); } else { Union(hasz(i - 1, j), hasz(i - 1, j - 1), k); Union(hasz(i , j), hasz(i, j - 1), k); } } } } FOR(i, 0, n) { FOR(j, 0, m) { F(k, 0, LCM) { if (spojne[i][j][k] == hasz(i, j)) { wartosci[k].eb(hasz(i, j)); } } } } F(k, 0, LCM) { sort(all(wartosci[k])); G[k] = vector<VI>(sz(wartosci[k]) + 1); odl[k] = VI(sz(wartosci[k]) + 1); } F(i, 0, LCM) { FOR(j, 0, n) { FOR(k, 0, m) { Find(hasz(j, k), i); // cout << Find(hasz(j, k), i) << " "; } // cout << "\n"; } // cout << "\n"; } // cout.flush(); FOR(i, 0, n) { FOR(j, 0, m) { F(k, 0, LCM) { int next = (k + 1) % LCM; int skad = lower_bound(all(wartosci[k]), Find(spojne[i][j][k], k)) - wartosci[k].begin(); int dokad = lower_bound(all(wartosci[next]), Find(spojne[i][j][next], next)) - wartosci[next].begin(); G[k][skad].eb(dokad); } } } F(i, 0, LCM) { F(j, 0, sz(G[i])) { sort(all(G[i][j])); G[i][j].resize(unique(all(G[i][j])) - G[i][j].begin()); } } while(q--) { int t, a, b, x, y; cin >> t >> a >> b >> x >> y; F(i, 0, LCM) { odl[i].assign(sz(odl[i]), INF); } int t_pocz = t; t %= LCM; int pocz = lower_bound(all(wartosci[t]), spojne[a][b][t]) - wartosci[t].begin(); odl[t][pocz] = 0; queue<wyd> q; int res = INF; q.emplace(pocz, t, 0); int cel = lower_bound(all(wartosci[t]), spojne[x][y][t]) - wartosci[t].begin(); if (cel == pocz) { cout << t_pocz << "\n"; continue; } while (sz(q)) { auto act = q.front(); q.pop(); if (act.odl != odl[act.warstwa][act.x]) { continue; } int nastepna = (act.warstwa + 1) % LCM; for (int dokad : G[act.warstwa][act.x]) { if (odl[act.warstwa][act.x] + 1 < odl[nastepna][dokad]) { odl[nastepna][dokad] = odl[act.warstwa][act.x] + 1; q.emplace(dokad, nastepna, odl[nastepna][dokad]); int cel = lower_bound(all(wartosci[nastepna]), spojne[x][y][nastepna]) - wartosci[nastepna].begin(); if (cel == dokad) { res = odl[nastepna][dokad]; break; } } } if (res != INF) break; } cout << t_pocz + res << "\n"; } } int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0); cout<<setprecision(9)<<fixed; cerr<<setprecision(6)<<fixed; int test = 1, f; // cin>>test; F(_test, 0, test){ //cout<<"Case #"<<_test + 1<<": "; solve(); // if(_test == 1) // return 0; } } /* 1 1 1 01 0 0 0 1 1 2 2 1 01 1100 001 10 0 0 2 2 2 */
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //~ while (clock()<=69*CLOCKS_PER_SEC) #define ll long long #define ld long double #define pi pair<int,int> #define pd pair<ld,ld> #define ft first #define sd second #define st first #define nd second #define mp make_pair #define pb push_back #define eb emplace_back #define FOR(i,a,b) for(int i=(a); i<=(b);i++) #define F(i,a,b) FOR(i,(a),(b)-1) #define REV(i,a,b) for(int i=(a); i>=(b);i--) #define VI vector<int> #define VPI vector<pi> #define VPD vector<pd> #define PI 3.14159265 #define all(x) (x).begin(), (x).end() #define sz(a) (int)((a).size()) #define int long long template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long K = 1000; const long long KK = K*K; const long long MIL = KK*K; const long long INF = MIL*MIL; const long long MOD = 1e9 + 7; const long long N = 1e5 + 10, M=10; const int LCM = 820; // TODO zmienic int n, m, q; int hasz(int i, int j) { return i * (m + 1) + j; } pi odhasz(int i) { return {i / (m + 1), i % (m + 1)}; } vector< vector<string> > syg; vector< vector< VI > > spojne; vector< vector< VI > > G; vector< VI > odl; vector< VI > wartosci; int Find(int x, int c) { pi tmp = odhasz(x); int a = tmp.ft, b = tmp.sd; if (spojne[a][b][c] == hasz(a, b)) { return hasz(a, b); } else { return spojne[a][b][c] = Find(spojne[a][b][c], c); } } void Union(int a, int b, int c) { a = Find(a, c); b = Find(b, c); if (a != b) { pi pa = odhasz(a); pi pb = odhasz(b); spojne[pb.ft][pb.sd][c] = spojne[pa.ft][pa.sd][c]; } } struct wyd { int x, warstwa, odl; wyd(int a, int b, int c) : x(a), warstwa(b), odl(c) {} bool operator<(const wyd w) const { if (odl == w.odl) { return x < w.x; } return odl < w.odl; } }; void solve() { cin >> n >> m >> q; syg = vector< vector<string> >(n + 2, vector<string>(m + 2)); spojne = vector< vector< VI > >(n + 2, vector< VI > (m + 2, VI(LCM + 2))); wartosci = vector< VI >(LCM + 2); G = vector< vector< VI > >(LCM + 2); odl = vector< VI > (LCM + 2); FOR(i, 1, n) { FOR(j, 1, m) { cin >> syg[i][j]; } } FOR(i, 0, n) { FOR(j, 0, m) { F(k, 0, LCM) { spojne[i][j][k] = hasz(i, j); } } } FOR(i, 1, n) { FOR(j, 1, m) { F(k, 0, LCM) { if (syg[i][j][k % sz(syg[i][j])] == '0') { Union(hasz(i, j), hasz(i - 1, j), k); Union(hasz(i, j - 1), hasz(i - 1, j - 1), k); } else { Union(hasz(i - 1, j), hasz(i - 1, j - 1), k); Union(hasz(i , j), hasz(i, j - 1), k); } } } } FOR(i, 0, n) { FOR(j, 0, m) { F(k, 0, LCM) { if (spojne[i][j][k] == hasz(i, j)) { wartosci[k].eb(hasz(i, j)); } } } } F(k, 0, LCM) { sort(all(wartosci[k])); G[k] = vector<VI>(sz(wartosci[k]) + 1); odl[k] = VI(sz(wartosci[k]) + 1); } F(i, 0, LCM) { FOR(j, 0, n) { FOR(k, 0, m) { Find(hasz(j, k), i); // cout << Find(hasz(j, k), i) << " "; } // cout << "\n"; } // cout << "\n"; } // cout.flush(); FOR(i, 0, n) { FOR(j, 0, m) { F(k, 0, LCM) { int next = (k + 1) % LCM; int skad = lower_bound(all(wartosci[k]), Find(spojne[i][j][k], k)) - wartosci[k].begin(); int dokad = lower_bound(all(wartosci[next]), Find(spojne[i][j][next], next)) - wartosci[next].begin(); G[k][skad].eb(dokad); } } } F(i, 0, LCM) { F(j, 0, sz(G[i])) { sort(all(G[i][j])); G[i][j].resize(unique(all(G[i][j])) - G[i][j].begin()); } } while(q--) { int t, a, b, x, y; cin >> t >> a >> b >> x >> y; F(i, 0, LCM) { odl[i].assign(sz(odl[i]), INF); } int t_pocz = t; t %= LCM; int pocz = lower_bound(all(wartosci[t]), spojne[a][b][t]) - wartosci[t].begin(); odl[t][pocz] = 0; queue<wyd> q; int res = INF; q.emplace(pocz, t, 0); int cel = lower_bound(all(wartosci[t]), spojne[x][y][t]) - wartosci[t].begin(); if (cel == pocz) { cout << t_pocz << "\n"; continue; } while (sz(q)) { auto act = q.front(); q.pop(); if (act.odl != odl[act.warstwa][act.x]) { continue; } int nastepna = (act.warstwa + 1) % LCM; for (int dokad : G[act.warstwa][act.x]) { if (odl[act.warstwa][act.x] + 1 < odl[nastepna][dokad]) { odl[nastepna][dokad] = odl[act.warstwa][act.x] + 1; q.emplace(dokad, nastepna, odl[nastepna][dokad]); int cel = lower_bound(all(wartosci[nastepna]), spojne[x][y][nastepna]) - wartosci[nastepna].begin(); if (cel == dokad) { res = odl[nastepna][dokad]; break; } } } if (res != INF) break; } cout << t_pocz + res << "\n"; } } int32_t main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0); cout<<setprecision(9)<<fixed; cerr<<setprecision(6)<<fixed; int test = 1, f; // cin>>test; F(_test, 0, test){ //cout<<"Case #"<<_test + 1<<": "; solve(); // if(_test == 1) // return 0; } } /* 1 1 1 01 0 0 0 1 1 2 2 1 01 1100 001 10 0 0 2 2 2 */ |