#include<iostream> #include<string> #include<vector> #include<algorithm> #include<list> using namespace std; int MOD = 840; bool can_vertical(vector<vector<string*>>& lights, long long t, int a, int b) { string* light = lights[a][b]; return light->at(t % light->size()) == '0'; } bool can_horizontal(vector<vector<string*>>& lights, long long t, int a, int b) { string* light = lights[a][b]; return light->at(t % light->size()) == '1'; } bool can_up(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (a <= 0) return false; bool res = false; if (b < m) { res = res || can_vertical(lights, t, a - 1, b); } if (b > 0) { res = res || can_vertical(lights, t, a - 1, b - 1); } return res; } bool can_down(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (a >= n) return false; bool res = false; if (b < m) { res = res || can_vertical(lights, t, a, b); } if (b > 0) { res = res || can_vertical(lights, t, a, b - 1); } return res; } bool can_left(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (b <= 0) return false; bool res = false; if (a < n) { res = res || can_horizontal(lights, t, a, b - 1); } if (a > 0) { res = res || can_horizontal(lights, t, a - 1, b - 1); } return res; } bool can_right(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (b >= m) return false; bool res = false; if (a < n) { res = res || can_horizontal(lights, t, a, b); } if (a > 0) { res = res || can_horizontal(lights, t, a - 1, b); } return res; } class entry { public: int i, j, counter; entry(int i, int j, int counter) { this->i = i; this->j = j; this->counter = counter; } }; void dfs(vector<vector<int>>& squares, vector<vector<string*>>& lights, list<entry> &queue, int q, long long t, int i, int j, int n, int m) { if (can_left(lights, t, i, j, n, m) && squares[i][j - 1] != q) { queue.push_back(entry(i, j - 1, 8)); squares[i][j - 1] = q; dfs(squares, lights, queue, q, t, i, j - 1, n, m); } if (can_right(lights, t, i, j, n, m) && squares[i][j + 1] != q) { queue.push_back(entry(i, j + 1, 8)); squares[i][j + 1] = q; dfs(squares, lights, queue, q, t, i, j + 1, n, m); } if (can_up(lights, t, i, j, n, m) && squares[i - 1][j] != q) { queue.push_back(entry(i - 1, j, 8)); squares[i - 1][j] = q; dfs(squares, lights, queue, q, t, i - 1, j, n, m); } if (can_down(lights, t, i, j, n, m) && squares[i + 1][j] != q) { queue.push_back(entry(i + 1, j, 8)); squares[i + 1][j] = q; dfs(squares, lights, queue, q, t, i + 1, j, n, m); } } long long explore(vector<vector<int>>& squares, vector<vector<string*>>& lights, int q, long long t, int a, int b, int x, int y, int n, int m) { squares[a][b] = q; list<entry> queue; list<entry> appnd; queue.push_back(entry(a, b, 8)); while (true) { // cout << t << endl; list<entry>::iterator it = queue.begin(); while (it != queue.end()) { if (it->counter >= 0) { it->counter--; dfs(squares, lights, appnd, q, t, it->i, it->j, n, m); ++it; } else { it = queue.erase(it); } } for (it = appnd.begin(); it != appnd.end(); ++it) { queue.push_back(*it); } appnd.clear(); if (squares[x][y] == q) { break; } t += 1; } return t; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<string*>> lights; for (int i = 0; i < n; i++) { vector<string*> row; for (int j = 0; j < m; j++) { string* s = new string(); cin >> *s; row.push_back(s); } lights.push_back(row); } vector<vector<int>> squares; for (int i = 0; i <= n; i++) { vector<int> row(m + 1); squares.push_back(row); } for (int i = 1; i <= q; i++) { long long t; int a, b, x, y; cin >> t >> a >> b >> x >> y; long long res = explore(squares, lights, i, t, a, b, x, y, n, m); cout << res << 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 | #include<iostream> #include<string> #include<vector> #include<algorithm> #include<list> using namespace std; int MOD = 840; bool can_vertical(vector<vector<string*>>& lights, long long t, int a, int b) { string* light = lights[a][b]; return light->at(t % light->size()) == '0'; } bool can_horizontal(vector<vector<string*>>& lights, long long t, int a, int b) { string* light = lights[a][b]; return light->at(t % light->size()) == '1'; } bool can_up(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (a <= 0) return false; bool res = false; if (b < m) { res = res || can_vertical(lights, t, a - 1, b); } if (b > 0) { res = res || can_vertical(lights, t, a - 1, b - 1); } return res; } bool can_down(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (a >= n) return false; bool res = false; if (b < m) { res = res || can_vertical(lights, t, a, b); } if (b > 0) { res = res || can_vertical(lights, t, a, b - 1); } return res; } bool can_left(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (b <= 0) return false; bool res = false; if (a < n) { res = res || can_horizontal(lights, t, a, b - 1); } if (a > 0) { res = res || can_horizontal(lights, t, a - 1, b - 1); } return res; } bool can_right(vector<vector<string*>>& lights, long long t, int a, int b, int n, int m) { if (b >= m) return false; bool res = false; if (a < n) { res = res || can_horizontal(lights, t, a, b); } if (a > 0) { res = res || can_horizontal(lights, t, a - 1, b); } return res; } class entry { public: int i, j, counter; entry(int i, int j, int counter) { this->i = i; this->j = j; this->counter = counter; } }; void dfs(vector<vector<int>>& squares, vector<vector<string*>>& lights, list<entry> &queue, int q, long long t, int i, int j, int n, int m) { if (can_left(lights, t, i, j, n, m) && squares[i][j - 1] != q) { queue.push_back(entry(i, j - 1, 8)); squares[i][j - 1] = q; dfs(squares, lights, queue, q, t, i, j - 1, n, m); } if (can_right(lights, t, i, j, n, m) && squares[i][j + 1] != q) { queue.push_back(entry(i, j + 1, 8)); squares[i][j + 1] = q; dfs(squares, lights, queue, q, t, i, j + 1, n, m); } if (can_up(lights, t, i, j, n, m) && squares[i - 1][j] != q) { queue.push_back(entry(i - 1, j, 8)); squares[i - 1][j] = q; dfs(squares, lights, queue, q, t, i - 1, j, n, m); } if (can_down(lights, t, i, j, n, m) && squares[i + 1][j] != q) { queue.push_back(entry(i + 1, j, 8)); squares[i + 1][j] = q; dfs(squares, lights, queue, q, t, i + 1, j, n, m); } } long long explore(vector<vector<int>>& squares, vector<vector<string*>>& lights, int q, long long t, int a, int b, int x, int y, int n, int m) { squares[a][b] = q; list<entry> queue; list<entry> appnd; queue.push_back(entry(a, b, 8)); while (true) { // cout << t << endl; list<entry>::iterator it = queue.begin(); while (it != queue.end()) { if (it->counter >= 0) { it->counter--; dfs(squares, lights, appnd, q, t, it->i, it->j, n, m); ++it; } else { it = queue.erase(it); } } for (it = appnd.begin(); it != appnd.end(); ++it) { queue.push_back(*it); } appnd.clear(); if (squares[x][y] == q) { break; } t += 1; } return t; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<string*>> lights; for (int i = 0; i < n; i++) { vector<string*> row; for (int j = 0; j < m; j++) { string* s = new string(); cin >> *s; row.push_back(s); } lights.push_back(row); } vector<vector<int>> squares; for (int i = 0; i <= n; i++) { vector<int> row(m + 1); squares.push_back(row); } for (int i = 1; i <= q; i++) { long long t; int a, b, x, y; cin >> t >> a >> b >> x >> y; long long res = explore(squares, lights, i, t, a, b, x, y, n, m); cout << res << endl; } return 0; } |