#include <bits/stdc++.h> using namespace std; const int TIME = 8 * 7 * 5 * 3; int n, m, q; #define id3(a, b, c) (((c)%TIME)*(n+1)*(m+1) + ((a)*(m+1) + (b))) // inna kolejnosc? #define id2(a, b) ((a)*m + (b)) set<int> horizontallyBlocked[TIME]; set<int> verticallyBlocked[TIME]; void checkAndMark(bool* notPossible, set<string> &configs, int length, char notAllowed){ for(int i = 0; i < length; i++){ for(auto config: configs){ if(config[i] == notAllowed){ for(int j = i; j < TIME; j+=length){ notPossible[j] = true; } break; } } } } void fillUpHorizontal(vector<string> &configs){ for(int i = 0; i < n; i++){ set<string> set5; set<string> set6; set<string> set7; set<string> set8; for(int j = 0; j < m; j++){ if(configs[id2(i, j)].length() == 2) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)] + configs[id2(i, j)]+ configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 3) { set6.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 4) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 5) { set5.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 6) { set6.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 7) { set7.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 8) { set8.insert(configs[id2(i, j)]); } } bool* notPossible = new bool[TIME]; for(int j = 0; j < TIME; j++) notPossible[j] = false; checkAndMark(notPossible, set5, 5, '0'); checkAndMark(notPossible, set6, 6, '0'); checkAndMark(notPossible, set7, 7, '0'); checkAndMark(notPossible, set8, 8, '0'); for(int j = 0; j < TIME; j++){ if(!notPossible[j]){ horizontallyBlocked[j].insert(i); } } delete [] notPossible; } } void fillUpVertical(vector<string> &configs){ for(int j = 0; j < m; j++){ set<string> set5; set<string> set6; set<string> set7; set<string> set8; for(int i = 0; i < n; i++){ if(configs[id2(i, j)].length() == 2) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)] + configs[id2(i, j)]+ configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 3) { set6.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 4) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 5) { set5.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 6) { set6.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 7) { set7.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 8) { set8.insert(configs[id2(i, j)]); } } bool* notPossible = new bool[TIME]; for(int i = 0; i < TIME; i++) notPossible[i] = false; checkAndMark(notPossible, set5, 5, '1'); checkAndMark(notPossible, set6, 6, '1'); checkAndMark(notPossible, set7, 7, '1'); checkAndMark(notPossible, set8, 8, '1'); for(int i = 0; i < TIME; i++){ if(!notPossible[i]){ verticallyBlocked[i].insert(j); } } delete [] notPossible; } } int getSolution(int t, int a, int b, int c, int d){ int lastVertically = -1; int lastHorizontally = -1; while (true) { if (verticallyBlocked[t%TIME].empty() and horizontallyBlocked[t%TIME].empty()) { return t; } if (horizontallyBlocked[t%TIME].empty()) { // sa tylko pionowe if(lastVertically > -1) return t; if(b == d) return t; if(b < d) { auto it = verticallyBlocked[t%TIME].upper_bound(b-1); if(it == verticallyBlocked[t%TIME].end() or (*it) >= d) return t; b = (*it); lastHorizontally = (*it); } else { auto it = verticallyBlocked[t%TIME].lower_bound(b); if(it == verticallyBlocked[t%TIME].begin()){ return t; } lastHorizontally = *prev(it); if(lastHorizontally < d) return t; b = lastHorizontally+1; } } else { // sa tylko poziome if(lastHorizontally > -1) return t; if(a == c) return t; if(a < c) { auto it = horizontallyBlocked[t%TIME].upper_bound(a-1); if(it == horizontallyBlocked[t%TIME].end() or (*it) >= c) return t; a = (*it); lastVertically = (*it); } else { auto it = horizontallyBlocked[t%TIME].lower_bound(a); if(it == horizontallyBlocked[t%TIME].begin()){ return t; } lastVertically = *prev(it); if(lastVertically < c) return t; a = lastVertically+1; } } t++; } } int main() { std::ios_base::sync_with_stdio(false); cin >> n >> m >> q; vector<string> trafficConfigs; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { string nextConfig; cin >> nextConfig; trafficConfigs.push_back(nextConfig); } } fillUpHorizontal(trafficConfigs); fillUpVertical(trafficConfigs); // cout<<"DONE"<<endl; // cout<<endl; // for(int i = 0; i < 4; i++){ // cout<<"TIME: "<<i<<endl; // for(auto elem: horizontallyBlocked[i]){ // cout<<elem<<" "; // } // cout<<endl; // } // for(int i = 0; i < 4; i++){ // cout<<"TIME: "<<i<<endl; // for(auto elem: verticallyBlocked[i]){ // cout<<elem<<" "; // } // cout<<endl; // } // cout<<"=============="<<endl; for(int i = 0; i < q; i++){ int t, a, b, c, d; cin >> t >> a >> b >> c >> d; cout << getSolution(t, a, b, c, d)<<endl; // getSolution(graph, tableForGraph, t%TIME, a, b, c, d); // cout << getSolution(graph, tableForGraph, t, a, b, c, d) << endl; // zrobic sprawdzanie :) } 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | #include <bits/stdc++.h> using namespace std; const int TIME = 8 * 7 * 5 * 3; int n, m, q; #define id3(a, b, c) (((c)%TIME)*(n+1)*(m+1) + ((a)*(m+1) + (b))) // inna kolejnosc? #define id2(a, b) ((a)*m + (b)) set<int> horizontallyBlocked[TIME]; set<int> verticallyBlocked[TIME]; void checkAndMark(bool* notPossible, set<string> &configs, int length, char notAllowed){ for(int i = 0; i < length; i++){ for(auto config: configs){ if(config[i] == notAllowed){ for(int j = i; j < TIME; j+=length){ notPossible[j] = true; } break; } } } } void fillUpHorizontal(vector<string> &configs){ for(int i = 0; i < n; i++){ set<string> set5; set<string> set6; set<string> set7; set<string> set8; for(int j = 0; j < m; j++){ if(configs[id2(i, j)].length() == 2) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)] + configs[id2(i, j)]+ configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 3) { set6.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 4) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 5) { set5.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 6) { set6.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 7) { set7.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 8) { set8.insert(configs[id2(i, j)]); } } bool* notPossible = new bool[TIME]; for(int j = 0; j < TIME; j++) notPossible[j] = false; checkAndMark(notPossible, set5, 5, '0'); checkAndMark(notPossible, set6, 6, '0'); checkAndMark(notPossible, set7, 7, '0'); checkAndMark(notPossible, set8, 8, '0'); for(int j = 0; j < TIME; j++){ if(!notPossible[j]){ horizontallyBlocked[j].insert(i); } } delete [] notPossible; } } void fillUpVertical(vector<string> &configs){ for(int j = 0; j < m; j++){ set<string> set5; set<string> set6; set<string> set7; set<string> set8; for(int i = 0; i < n; i++){ if(configs[id2(i, j)].length() == 2) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)] + configs[id2(i, j)]+ configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 3) { set6.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 4) { set8.insert(configs[id2(i, j)] + configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 5) { set5.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 6) { set6.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 7) { set7.insert(configs[id2(i, j)]); } else if(configs[id2(i, j)].length() == 8) { set8.insert(configs[id2(i, j)]); } } bool* notPossible = new bool[TIME]; for(int i = 0; i < TIME; i++) notPossible[i] = false; checkAndMark(notPossible, set5, 5, '1'); checkAndMark(notPossible, set6, 6, '1'); checkAndMark(notPossible, set7, 7, '1'); checkAndMark(notPossible, set8, 8, '1'); for(int i = 0; i < TIME; i++){ if(!notPossible[i]){ verticallyBlocked[i].insert(j); } } delete [] notPossible; } } int getSolution(int t, int a, int b, int c, int d){ int lastVertically = -1; int lastHorizontally = -1; while (true) { if (verticallyBlocked[t%TIME].empty() and horizontallyBlocked[t%TIME].empty()) { return t; } if (horizontallyBlocked[t%TIME].empty()) { // sa tylko pionowe if(lastVertically > -1) return t; if(b == d) return t; if(b < d) { auto it = verticallyBlocked[t%TIME].upper_bound(b-1); if(it == verticallyBlocked[t%TIME].end() or (*it) >= d) return t; b = (*it); lastHorizontally = (*it); } else { auto it = verticallyBlocked[t%TIME].lower_bound(b); if(it == verticallyBlocked[t%TIME].begin()){ return t; } lastHorizontally = *prev(it); if(lastHorizontally < d) return t; b = lastHorizontally+1; } } else { // sa tylko poziome if(lastHorizontally > -1) return t; if(a == c) return t; if(a < c) { auto it = horizontallyBlocked[t%TIME].upper_bound(a-1); if(it == horizontallyBlocked[t%TIME].end() or (*it) >= c) return t; a = (*it); lastVertically = (*it); } else { auto it = horizontallyBlocked[t%TIME].lower_bound(a); if(it == horizontallyBlocked[t%TIME].begin()){ return t; } lastVertically = *prev(it); if(lastVertically < c) return t; a = lastVertically+1; } } t++; } } int main() { std::ios_base::sync_with_stdio(false); cin >> n >> m >> q; vector<string> trafficConfigs; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { string nextConfig; cin >> nextConfig; trafficConfigs.push_back(nextConfig); } } fillUpHorizontal(trafficConfigs); fillUpVertical(trafficConfigs); // cout<<"DONE"<<endl; // cout<<endl; // for(int i = 0; i < 4; i++){ // cout<<"TIME: "<<i<<endl; // for(auto elem: horizontallyBlocked[i]){ // cout<<elem<<" "; // } // cout<<endl; // } // for(int i = 0; i < 4; i++){ // cout<<"TIME: "<<i<<endl; // for(auto elem: verticallyBlocked[i]){ // cout<<elem<<" "; // } // cout<<endl; // } // cout<<"=============="<<endl; for(int i = 0; i < q; i++){ int t, a, b, c, d; cin >> t >> a >> b >> c >> d; cout << getSolution(t, a, b, c, d)<<endl; // getSolution(graph, tableForGraph, t%TIME, a, b, c, d); // cout << getSolution(graph, tableForGraph, t, a, b, c, d) << endl; // zrobic sprawdzanie :) } return 0; } |