#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; int nww(int a, int b) { return a * (b / __gcd(a, b)); } int n, m, quest; bool check(int i, int j) { if (i > 0 && i <= n && j > 0 && j <= m) return true; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m>>quest; int cykl = 2; vector<vector<string>> skrz(n + 1, vector<string>(m + 1, "")); string s; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin>>s; cykl = nww(cykl, s.length()); skrz[i][j] = s; } } //cerr<<"cykl = "<<cykl<<endl; //cerr<<"quest = "<<quest<<endl; for (int l = 0; l < quest; l++) { int t, a, b, c, d; cin>>t>>a>>b>>c>>d; int init = t; t %= cykl; // czas startu int tpocz = t; bool found = false; queue<pair<int, int>> q; queue<pair<int, int>> q2; vector<vector<bool>> vis(n + 7, vector<bool>(m + 7, false)); q.push({a, b}); vis[a][b] = true; while (!found) { while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); q2.push(p); int i = p.first; int j = p.second; if (i == c && j == d) { found = true; // wrzucone do kolejki w poprzedniej fazie break; } if ((check(i, j) && skrz[i][j][t % skrz[i][j].length()] == '1') || (check(i + 1, j) && skrz[i + 1][j][t % skrz[i + 1][j].length()] == '1')) { if (vis[i][j - 1] == false) { //cerr<<"wrzucam "<<i<<" "<<j - 1<<" w czasie "<<t<<endl; vis[i][j - 1] = true; q.push({i, j - 1}); } } if ((check(i, j + 1) && skrz[i][j + 1][t % skrz[i][j + 1].length()] == '1') || (check(i + 1, j + 1) && skrz[i + 1][j + 1][t % skrz[i + 1][j + 1].length()] == '1')) { if (vis[i][j + 1] == false) { //cerr<<"wrzucam "<<i<<" "<<j + 1<<" w czasie "<<t<<endl; vis[i][j + 1] = true; q.push({i, j + 1}); } } if ((check(i, j) && skrz[i][j][t % skrz[i][j].length()] == '0') || (check(i, j + 1) && skrz[i][j + 1][t % skrz[i][j + 1].length()] == '0')) { if (vis[i - 1][j] == false) { //cerr<<"wrzucam "<<i - 1<<" "<<j<<" w czasie "<<t<<endl; vis[i - 1][j] = true; q.push({i - 1, j}); } } if ((check(i + 1, j) && skrz[i + 1][j][t % skrz[i + 1][j].length()] == '0') || (check(i + 1, j + 1) && skrz[i + 1][j + 1][t % skrz[i + 1][j + 1].length()] == '0')) { if (vis[i + 1][j] == false) { //cerr<<"wrzucam "<<i + 1<<" "<<j<<" w czasie "<<t<<endl; vis[i + 1][j] = true; q.push({i + 1, j}); } } } swap(q, q2); t++; } cout<<init + t - tpocz - 1<<"\n"; } }
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 | #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; int nww(int a, int b) { return a * (b / __gcd(a, b)); } int n, m, quest; bool check(int i, int j) { if (i > 0 && i <= n && j > 0 && j <= m) return true; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>m>>quest; int cykl = 2; vector<vector<string>> skrz(n + 1, vector<string>(m + 1, "")); string s; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin>>s; cykl = nww(cykl, s.length()); skrz[i][j] = s; } } //cerr<<"cykl = "<<cykl<<endl; //cerr<<"quest = "<<quest<<endl; for (int l = 0; l < quest; l++) { int t, a, b, c, d; cin>>t>>a>>b>>c>>d; int init = t; t %= cykl; // czas startu int tpocz = t; bool found = false; queue<pair<int, int>> q; queue<pair<int, int>> q2; vector<vector<bool>> vis(n + 7, vector<bool>(m + 7, false)); q.push({a, b}); vis[a][b] = true; while (!found) { while (!q.empty()) { pair<int, int> p = q.front(); q.pop(); q2.push(p); int i = p.first; int j = p.second; if (i == c && j == d) { found = true; // wrzucone do kolejki w poprzedniej fazie break; } if ((check(i, j) && skrz[i][j][t % skrz[i][j].length()] == '1') || (check(i + 1, j) && skrz[i + 1][j][t % skrz[i + 1][j].length()] == '1')) { if (vis[i][j - 1] == false) { //cerr<<"wrzucam "<<i<<" "<<j - 1<<" w czasie "<<t<<endl; vis[i][j - 1] = true; q.push({i, j - 1}); } } if ((check(i, j + 1) && skrz[i][j + 1][t % skrz[i][j + 1].length()] == '1') || (check(i + 1, j + 1) && skrz[i + 1][j + 1][t % skrz[i + 1][j + 1].length()] == '1')) { if (vis[i][j + 1] == false) { //cerr<<"wrzucam "<<i<<" "<<j + 1<<" w czasie "<<t<<endl; vis[i][j + 1] = true; q.push({i, j + 1}); } } if ((check(i, j) && skrz[i][j][t % skrz[i][j].length()] == '0') || (check(i, j + 1) && skrz[i][j + 1][t % skrz[i][j + 1].length()] == '0')) { if (vis[i - 1][j] == false) { //cerr<<"wrzucam "<<i - 1<<" "<<j<<" w czasie "<<t<<endl; vis[i - 1][j] = true; q.push({i - 1, j}); } } if ((check(i + 1, j) && skrz[i + 1][j][t % skrz[i + 1][j].length()] == '0') || (check(i + 1, j + 1) && skrz[i + 1][j + 1][t % skrz[i + 1][j + 1].length()] == '0')) { if (vis[i + 1][j] == false) { //cerr<<"wrzucam "<<i + 1<<" "<<j<<" w czasie "<<t<<endl; vis[i + 1][j] = true; q.push({i + 1, j}); } } } swap(q, q2); t++; } cout<<init + t - tpocz - 1<<"\n"; } } |