#include <algorithm> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; using LL = long long; int main() { int n, m, q; cin >> n >> m >> q; vector<vector<string>> s = vector<vector<string>>(n, vector<string>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> s[i][j]; } } vector<vector<vector<int>>> s_change = vector<vector<vector<int>>>(n, vector<vector<int>>(m, vector<int>())); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { for (int k = 0; k < s[i][j].length(); ++k) { int l = 0; while (s[i][j][(k + l) % s[i][j].length()] == s[i][j][k]) ++l; s_change[i][j].push_back(l); } } } for (int qq = 0; qq < q; ++qq) { LL t; int a, b, c, d; cin >> t >> a >> b >> c >> d; vector<vector<LL>> dist = vector<vector<LL>>(n + 1, vector<LL>(m + 1, 1000000000000000)); dist[a][b] = t; priority_queue<pair<LL, pair<int, int>>, vector<pair<LL, pair<int, int>>>, greater<pair<LL, pair<int, int>>>> pq; pq.push(make_pair(t, make_pair(a, b))); while (!pq.empty()) { LL time = pq.top().first; int x = pq.top().second.first; int y = pq.top().second.second; pq.pop(); if (dist[x][y] != time) continue; if (x == c && y == d) break; if (x > 0 && y > 0) { if (s[x - 1][y - 1][time % s[x - 1][y - 1].length()] == '0') { if (dist[x - 1][y] > time) { dist[x - 1][y] = time; pq.push(make_pair(time, make_pair(x - 1, y))); } int add = s_change[x - 1][y - 1][time % s[x - 1][y - 1].length()]; if (dist[x][y - 1] > time + add) { dist[x][y - 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y - 1))); } } else { if (dist[x][y - 1] > time) { dist[x][y - 1] = time; pq.push(make_pair(time, make_pair(x, y - 1))); } int add = s_change[x - 1][y - 1][time % s[x - 1][y - 1].length()]; if (dist[x - 1][y] > time + add) { dist[x - 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x - 1, y))); } } } if (x > 0 && y < m) { if (s[x - 1][y][time % s[x - 1][y].length()] == '0') { if (dist[x - 1][y] > time) { dist[x - 1][y] = time; pq.push(make_pair(time, make_pair(x - 1, y))); } int add = s_change[x - 1][y][time % s[x - 1][y].length()]; if (dist[x][y + 1] > time + add) { dist[x][y + 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y + 1))); } } else { if (dist[x][y + 1] > time) { dist[x][y + 1] = time; pq.push(make_pair(time, make_pair(x, y + 1))); } int add = s_change[x - 1][y][time % s[x - 1][y].length()]; if (dist[x - 1][y] > time + add) { dist[x - 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x - 1, y))); } } } if (x < n && y > 0) { if (s[x][y - 1][time % s[x][y - 1].length()] == '0') { if (dist[x + 1][y] > time) { dist[x + 1][y] = time; pq.push(make_pair(time, make_pair(x + 1, y))); } int add = s_change[x][y - 1][time % s[x][y - 1].length()]; if (dist[x][y - 1] > time + add) { dist[x][y - 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y - 1))); } } else { if (dist[x][y - 1] > time) { dist[x][y - 1] = time; pq.push(make_pair(time, make_pair(x, y - 1))); } int add = s_change[x][y - 1][time % s[x][y - 1].length()]; if (dist[x + 1][y] > time + add) { dist[x + 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x + 1, y))); } } } if (x < n && y < m) { if (s[x][y][time % s[x][y].length()] == '0') { if (dist[x + 1][y] > time) { dist[x + 1][y] = time; pq.push(make_pair(time, make_pair(x + 1, y))); } int add = s_change[x][y][time % s[x][y].length()]; if (dist[x][y + 1] > time + add) { dist[x][y + 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y + 1))); } } else { if (dist[x][y + 1] > time) { dist[x][y + 1] = time; pq.push(make_pair(time, make_pair(x, y + 1))); } int add = s_change[x][y][time % s[x][y].length()]; if (dist[x + 1][y] > time + add) { dist[x + 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x + 1, y))); } } } } cout << dist[c][d] << endl; } 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 | #include <algorithm> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; using LL = long long; int main() { int n, m, q; cin >> n >> m >> q; vector<vector<string>> s = vector<vector<string>>(n, vector<string>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> s[i][j]; } } vector<vector<vector<int>>> s_change = vector<vector<vector<int>>>(n, vector<vector<int>>(m, vector<int>())); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { for (int k = 0; k < s[i][j].length(); ++k) { int l = 0; while (s[i][j][(k + l) % s[i][j].length()] == s[i][j][k]) ++l; s_change[i][j].push_back(l); } } } for (int qq = 0; qq < q; ++qq) { LL t; int a, b, c, d; cin >> t >> a >> b >> c >> d; vector<vector<LL>> dist = vector<vector<LL>>(n + 1, vector<LL>(m + 1, 1000000000000000)); dist[a][b] = t; priority_queue<pair<LL, pair<int, int>>, vector<pair<LL, pair<int, int>>>, greater<pair<LL, pair<int, int>>>> pq; pq.push(make_pair(t, make_pair(a, b))); while (!pq.empty()) { LL time = pq.top().first; int x = pq.top().second.first; int y = pq.top().second.second; pq.pop(); if (dist[x][y] != time) continue; if (x == c && y == d) break; if (x > 0 && y > 0) { if (s[x - 1][y - 1][time % s[x - 1][y - 1].length()] == '0') { if (dist[x - 1][y] > time) { dist[x - 1][y] = time; pq.push(make_pair(time, make_pair(x - 1, y))); } int add = s_change[x - 1][y - 1][time % s[x - 1][y - 1].length()]; if (dist[x][y - 1] > time + add) { dist[x][y - 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y - 1))); } } else { if (dist[x][y - 1] > time) { dist[x][y - 1] = time; pq.push(make_pair(time, make_pair(x, y - 1))); } int add = s_change[x - 1][y - 1][time % s[x - 1][y - 1].length()]; if (dist[x - 1][y] > time + add) { dist[x - 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x - 1, y))); } } } if (x > 0 && y < m) { if (s[x - 1][y][time % s[x - 1][y].length()] == '0') { if (dist[x - 1][y] > time) { dist[x - 1][y] = time; pq.push(make_pair(time, make_pair(x - 1, y))); } int add = s_change[x - 1][y][time % s[x - 1][y].length()]; if (dist[x][y + 1] > time + add) { dist[x][y + 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y + 1))); } } else { if (dist[x][y + 1] > time) { dist[x][y + 1] = time; pq.push(make_pair(time, make_pair(x, y + 1))); } int add = s_change[x - 1][y][time % s[x - 1][y].length()]; if (dist[x - 1][y] > time + add) { dist[x - 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x - 1, y))); } } } if (x < n && y > 0) { if (s[x][y - 1][time % s[x][y - 1].length()] == '0') { if (dist[x + 1][y] > time) { dist[x + 1][y] = time; pq.push(make_pair(time, make_pair(x + 1, y))); } int add = s_change[x][y - 1][time % s[x][y - 1].length()]; if (dist[x][y - 1] > time + add) { dist[x][y - 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y - 1))); } } else { if (dist[x][y - 1] > time) { dist[x][y - 1] = time; pq.push(make_pair(time, make_pair(x, y - 1))); } int add = s_change[x][y - 1][time % s[x][y - 1].length()]; if (dist[x + 1][y] > time + add) { dist[x + 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x + 1, y))); } } } if (x < n && y < m) { if (s[x][y][time % s[x][y].length()] == '0') { if (dist[x + 1][y] > time) { dist[x + 1][y] = time; pq.push(make_pair(time, make_pair(x + 1, y))); } int add = s_change[x][y][time % s[x][y].length()]; if (dist[x][y + 1] > time + add) { dist[x][y + 1] = time + add; pq.push(make_pair(time + add, make_pair(x, y + 1))); } } else { if (dist[x][y + 1] > time) { dist[x][y + 1] = time; pq.push(make_pair(time, make_pair(x, y + 1))); } int add = s_change[x][y][time % s[x][y].length()]; if (dist[x + 1][y] > time + add) { dist[x + 1][y] = time + add; pq.push(make_pair(time + add, make_pair(x + 1, y))); } } } } cout << dist[c][d] << endl; } return 0; } |