#include <iostream> #include <unordered_set> #include <set> typedef std::unordered_set<std::string> ss; typedef std::set<int> si; const int N = 15005; int n, m, q; ss rows[N]; ss columns[N]; void input() { std::cin >> n >> m >> q; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { std::string s; std::cin >> s; rows[i].insert(s); columns[j].insert(s); } } } const int T = 840; // LCM(2,..,8) si horizontal[T]; si vertical[T]; char get(std::string s, int t) { return s[t % s.length()]; } void find_barriers() { // horizontal - search for all 1 for (int i = 0; i < n; i++) { for (int t = 0; t < T; t++) { bool blocks = true; for (auto & s : rows[i]) { if (get(s, t) == '0') { blocks = false; break; } } if (blocks) { horizontal[t].insert(i); } } } // vertical - search for all 0 for (int i = 0; i < m; i++) { for (int t = 0; t < T; t++) { bool blocks = true; for (auto & s : columns[i]) { if (get(s, t) == '1') { blocks = false; break; } } if (blocks) { vertical[t].insert(i); } } } } int sgn(int i) { if (i == 0) { return 0; } return i > 0 ? 1 : -1; } // TODO find next_barier in 4 directions // TODO create M*M + N*N arrays with time needed // rather M * (M / 10) add fill the gap by hand // so we need a function that fills the gap => it could also work as a POC int duration(si (&barriers)[T], int t, int from, int to) { int curr = from; int direction = sgn(to - from); // std::cout << "direction " << direction << " from " << from << " to " << to << " t " << t << "\n"; while (sgn(to - curr) == direction) { if (barriers[t % T].empty() || to == curr) { break; } // std::cout << "start iteration\n"; // if (direction > 0) { // std::cout << "direction > 0\n"; // } // if (direction > 0 && it == barriers[t % T].end()) { // std::cout << "cond2\n"; // } // if (direction < 0 && it == barriers[t % T].begin()) { // std::cout << "cond3\n"; // } if (direction > 0) { auto it = barriers[t % T].lower_bound(curr); if (it == barriers[t % T].end()) { break; } curr = *it; if (curr >= to) { break; } } // if (direction > 0 && (it == barriers[t % T].end() || *it == to)) { // break; // } if (direction < 0) { // std::cout << "searching for " << curr - 1 << "\n"; auto it = barriers[t % T].lower_bound(curr - 1); if (it == barriers[t % T].begin() && *it != curr - 1) { // std::cout << "no such barriers\n"; break; } else if (*it != curr - 1) { --it; } curr = *it + 1; // std::cout << "new value " << curr << "\n"; if (curr <= to) { break; } } t++; // curr = *it; // std::cout << "moved to " << curr << " at " << t << "\n"; } // std::cout << "arrived at " << t << "\n"; return t; } int find_duration() { int t, a, b, c, d; std::cin >> t >> a >> b >> c >> d; // std::cout << "going from (" << a << ", " << b << ") to (" << c << ", " << d << ") at " << t << "\n"; if (vertical[t % T].size() > 0) { // std::cout << "consider vertical barriers\n"; return duration(vertical, t, b, d); } else if (horizontal[t % T].size() > 0) { // std::cout << "consider horizontal barriers\n"; return duration(horizontal, t, a, c); } return t; } void solve() { find_barriers(); for (int i = 0; i < q; i++) { // std::cout << "TEST CASE #" << i << "\n"; std::cout << find_duration() << "\n"; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); input(); // for (int i = 0; i < n; i++) { // std::cout << "row " << i << "\n"; // for (auto & s : rows[i]) { // std::cout << s << " "; // } // std::cout << "\n"; // } // for (int i = 0; i < m; i++) { // std::cout << "column " << i << "\n"; // for (auto & s : columns[i]) { // std::cout << s << " "; // } // std::cout << "\n"; // } solve(); // for (int t = 0; t < 20; t++) { // std::cout << "t=" << t << "\n"; // if (horizontal[t].size() > 0) { // std::cout << "HORIZONTAL: "; // for (auto & x : horizontal[t]) { // std::cout << x << " "; // } // std::cout << "\n"; // } // if (vertical[t].size() > 0) { // std::cout << "VERTICAL: "; // for (auto & x : vertical[t]) { // std::cout << x << " "; // } // std::cout << "\n"; // } // } 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 | #include <iostream> #include <unordered_set> #include <set> typedef std::unordered_set<std::string> ss; typedef std::set<int> si; const int N = 15005; int n, m, q; ss rows[N]; ss columns[N]; void input() { std::cin >> n >> m >> q; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { std::string s; std::cin >> s; rows[i].insert(s); columns[j].insert(s); } } } const int T = 840; // LCM(2,..,8) si horizontal[T]; si vertical[T]; char get(std::string s, int t) { return s[t % s.length()]; } void find_barriers() { // horizontal - search for all 1 for (int i = 0; i < n; i++) { for (int t = 0; t < T; t++) { bool blocks = true; for (auto & s : rows[i]) { if (get(s, t) == '0') { blocks = false; break; } } if (blocks) { horizontal[t].insert(i); } } } // vertical - search for all 0 for (int i = 0; i < m; i++) { for (int t = 0; t < T; t++) { bool blocks = true; for (auto & s : columns[i]) { if (get(s, t) == '1') { blocks = false; break; } } if (blocks) { vertical[t].insert(i); } } } } int sgn(int i) { if (i == 0) { return 0; } return i > 0 ? 1 : -1; } // TODO find next_barier in 4 directions // TODO create M*M + N*N arrays with time needed // rather M * (M / 10) add fill the gap by hand // so we need a function that fills the gap => it could also work as a POC int duration(si (&barriers)[T], int t, int from, int to) { int curr = from; int direction = sgn(to - from); // std::cout << "direction " << direction << " from " << from << " to " << to << " t " << t << "\n"; while (sgn(to - curr) == direction) { if (barriers[t % T].empty() || to == curr) { break; } // std::cout << "start iteration\n"; // if (direction > 0) { // std::cout << "direction > 0\n"; // } // if (direction > 0 && it == barriers[t % T].end()) { // std::cout << "cond2\n"; // } // if (direction < 0 && it == barriers[t % T].begin()) { // std::cout << "cond3\n"; // } if (direction > 0) { auto it = barriers[t % T].lower_bound(curr); if (it == barriers[t % T].end()) { break; } curr = *it; if (curr >= to) { break; } } // if (direction > 0 && (it == barriers[t % T].end() || *it == to)) { // break; // } if (direction < 0) { // std::cout << "searching for " << curr - 1 << "\n"; auto it = barriers[t % T].lower_bound(curr - 1); if (it == barriers[t % T].begin() && *it != curr - 1) { // std::cout << "no such barriers\n"; break; } else if (*it != curr - 1) { --it; } curr = *it + 1; // std::cout << "new value " << curr << "\n"; if (curr <= to) { break; } } t++; // curr = *it; // std::cout << "moved to " << curr << " at " << t << "\n"; } // std::cout << "arrived at " << t << "\n"; return t; } int find_duration() { int t, a, b, c, d; std::cin >> t >> a >> b >> c >> d; // std::cout << "going from (" << a << ", " << b << ") to (" << c << ", " << d << ") at " << t << "\n"; if (vertical[t % T].size() > 0) { // std::cout << "consider vertical barriers\n"; return duration(vertical, t, b, d); } else if (horizontal[t % T].size() > 0) { // std::cout << "consider horizontal barriers\n"; return duration(horizontal, t, a, c); } return t; } void solve() { find_barriers(); for (int i = 0; i < q; i++) { // std::cout << "TEST CASE #" << i << "\n"; std::cout << find_duration() << "\n"; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); input(); // for (int i = 0; i < n; i++) { // std::cout << "row " << i << "\n"; // for (auto & s : rows[i]) { // std::cout << s << " "; // } // std::cout << "\n"; // } // for (int i = 0; i < m; i++) { // std::cout << "column " << i << "\n"; // for (auto & s : columns[i]) { // std::cout << s << " "; // } // std::cout << "\n"; // } solve(); // for (int t = 0; t < 20; t++) { // std::cout << "t=" << t << "\n"; // if (horizontal[t].size() > 0) { // std::cout << "HORIZONTAL: "; // for (auto & x : horizontal[t]) { // std::cout << x << " "; // } // std::cout << "\n"; // } // if (vertical[t].size() > 0) { // std::cout << "VERTICAL: "; // for (auto & x : vertical[t]) { // std::cout << x << " "; // } // std::cout << "\n"; // } // } return 0; } |