#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(const T &t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(const std::pair<T1, T2> &t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(const std::vector<T> &t) { for (auto a : t) print(a); std::cout << std::endl; } const std::vector<vi> table{{1, 1}, {1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}}; std::vector<vi> Rm, Lm, Rn, Ln; std::vector<vi> takenN, takenM; int nww; int solve(int t, int a, int b, int &c, int &d) { //print(vi{t, a, b, c, d}); if (a > c) { int temp = Ln[a - 1][t % nww]; if (takenN[temp][t % nww]) temp++; a = std::max(temp, c); } else if (a < c) { int temp = Rn[a][t % nww]; if (!takenN[temp][t % nww]) temp++; a = std::min(temp, c); } if (b > d) { int temp = Lm[b - 1][t % nww]; if (takenM[temp][t % nww]) temp++; b = std::max(temp, d); } else if (b < d) { int temp = Rm[b][t % nww]; if (!takenM[temp][t % nww]) temp++; b = std::min(temp, d); } //print(vi{t, a, b, c, d}); if (a == c && b == d) return t; return (solve(t + 1, a, b, c, d)); } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; //std::cin >> Z; while (Z--) { int m, n, q; std::cin >> n >> m >> q; std::vector<std::vector<std::string>> V(n, std::vector<std::string>(m)); nww = 1; for (auto &u : V) for (auto &a : u) { std::cin >> a; nww = (nww * a.size()) / std::__gcd(nww, int(a.size())); } std::vector<std::vector<vi>> modM(m, table), modN(n, table); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) for (int k = 0; k < V[i][j].size(); k++) { if (V[i][j][k] == '0') modN[i][V[i][j].size() - 2][k] = 0; else modM[j][V[i][j].size() - 2][k] = 0; } takenN.resize(n, vi(nww, 0)); takenM.resize(m, vi(nww, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < nww; j++) { bool temp = 1; for (int k = 2; k < 9; k++) temp = temp && modN[i][k - 2][j % k]; if (temp) takenN[i][j] = 1; } for (int i = 0; i < m; i++) for (int j = 0; j < nww; j++) { bool temp = 1; for (int k = 2; k < 9; k++) temp = temp && modM[i][k - 2][j % k]; if (temp) takenM[i][j] = 1; } Rm.resize(m, vi(nww, m - 1)); Lm.resize(m, vi(nww, 0)); Rn.resize(n, vi(nww, n - 1)); Ln.resize(n, vi(nww, 0)); for (int j = 0; j < nww; j++) { for (int i = m - 2; i >= 0; i--) if (takenM[i][j]) Rm[i][j] = i; else Rm[i][j] = Rm[i + 1][j]; for (int i = 1; i < m; i++) if (takenM[i][j]) Lm[i][j] = i; else Lm[i][j] = Lm[i - 1][j]; for (int i = n - 2; i >= 0; i--) if (takenN[i][j]) Rn[i][j] = i; else Rn[i][j] = Rn[i + 1][j]; for (int i = 1; i < n; i++) if (takenN[i][j]) Ln[i][j] = i; else Ln[i][j] = Ln[i - 1][j]; } //print(Ln); //print(Lm); while (q--) { int t, a, b, c, d; std::cin >> t >> a >> b >> c >> d; std::cout << solve(t, a, b, c, d) << std::endl; } } }
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 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(const T &t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(const std::pair<T1, T2> &t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(const std::vector<T> &t) { for (auto a : t) print(a); std::cout << std::endl; } const std::vector<vi> table{{1, 1}, {1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}}; std::vector<vi> Rm, Lm, Rn, Ln; std::vector<vi> takenN, takenM; int nww; int solve(int t, int a, int b, int &c, int &d) { //print(vi{t, a, b, c, d}); if (a > c) { int temp = Ln[a - 1][t % nww]; if (takenN[temp][t % nww]) temp++; a = std::max(temp, c); } else if (a < c) { int temp = Rn[a][t % nww]; if (!takenN[temp][t % nww]) temp++; a = std::min(temp, c); } if (b > d) { int temp = Lm[b - 1][t % nww]; if (takenM[temp][t % nww]) temp++; b = std::max(temp, d); } else if (b < d) { int temp = Rm[b][t % nww]; if (!takenM[temp][t % nww]) temp++; b = std::min(temp, d); } //print(vi{t, a, b, c, d}); if (a == c && b == d) return t; return (solve(t + 1, a, b, c, d)); } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; //std::cin >> Z; while (Z--) { int m, n, q; std::cin >> n >> m >> q; std::vector<std::vector<std::string>> V(n, std::vector<std::string>(m)); nww = 1; for (auto &u : V) for (auto &a : u) { std::cin >> a; nww = (nww * a.size()) / std::__gcd(nww, int(a.size())); } std::vector<std::vector<vi>> modM(m, table), modN(n, table); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) for (int k = 0; k < V[i][j].size(); k++) { if (V[i][j][k] == '0') modN[i][V[i][j].size() - 2][k] = 0; else modM[j][V[i][j].size() - 2][k] = 0; } takenN.resize(n, vi(nww, 0)); takenM.resize(m, vi(nww, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < nww; j++) { bool temp = 1; for (int k = 2; k < 9; k++) temp = temp && modN[i][k - 2][j % k]; if (temp) takenN[i][j] = 1; } for (int i = 0; i < m; i++) for (int j = 0; j < nww; j++) { bool temp = 1; for (int k = 2; k < 9; k++) temp = temp && modM[i][k - 2][j % k]; if (temp) takenM[i][j] = 1; } Rm.resize(m, vi(nww, m - 1)); Lm.resize(m, vi(nww, 0)); Rn.resize(n, vi(nww, n - 1)); Ln.resize(n, vi(nww, 0)); for (int j = 0; j < nww; j++) { for (int i = m - 2; i >= 0; i--) if (takenM[i][j]) Rm[i][j] = i; else Rm[i][j] = Rm[i + 1][j]; for (int i = 1; i < m; i++) if (takenM[i][j]) Lm[i][j] = i; else Lm[i][j] = Lm[i - 1][j]; for (int i = n - 2; i >= 0; i--) if (takenN[i][j]) Rn[i][j] = i; else Rn[i][j] = Rn[i + 1][j]; for (int i = 1; i < n; i++) if (takenN[i][j]) Ln[i][j] = i; else Ln[i][j] = Ln[i - 1][j]; } //print(Ln); //print(Lm); while (q--) { int t, a, b, c, d; std::cin >> t >> a >> b >> c >> d; std::cout << solve(t, a, b, c, d) << std::endl; } } } |