#include <iostream> #include <vector> using namespace std; const int MAX_PERIOD = 840; // NWW(2, 3, 4, 5, 6, 7, 8) = 3*5*7*8 = 840 struct Light { int period; vector<bool> data; void input() { string s; cin>>s; period = s.size(); data.resize(period); for(int i = 0; i < period; ++i) { data[i] = s[i] == '0'; } } bool is_vertical(int t) { return data[t % period]; } }; struct Time { bool is_vertical; vector<int> starts; int find_start(int x) { int l = 0, r = starts.size() - 1; while(l < r) { int mid = (l + r + 1) >> 1; if(starts[mid] > x) { r = mid - 1; }else { l = mid; } } return l; } }; struct Query { int t, x1, y1, x2, y2; void input() { cin>>t>>x1>>y1>>x2>>y2; } }; class Solution { int n, m, q; vector<vector<Light> > lights; vector<Time> time; vector<Query> queries; vector<int> result; vector<vector<bool> > visited; void dfs(int x, int y, int t) { if(visited[x][y]) return; visited[x][y] = true; vector<bool> free = calc_possible(x, y, t); if(free[0]) { dfs(x - 1, y, t); } if(free[1]) { dfs(x, y - 1, t); } if(free[2]) { dfs(x + 1, y, t); } if(free[3]) { dfs(x, y + 1, t); } } vector<bool> calc_possible(int x, int y, int t) { vector<bool> free(4, false); if(x > 0 && y < m && lights[x - 1][y].is_vertical(t)) free[0] = true; if(x > 0 && y > 0 && lights[x - 1][y - 1].is_vertical(t)) free[0] = true; if(y > 0 && x < n && !lights[x][y - 1].is_vertical(t)) free[1] = true; if(y > 0 && x > 0 && !lights[x - 1][y - 1].is_vertical(t)) free[1] = true; if(x < n && y < m && lights[x][y].is_vertical(t)) free[2] = true; if(x < n && y > 0 && lights[x][y - 1].is_vertical(t)) free[2] = true; if(y < m && x < n && !lights[x][y].is_vertical(t)) free[3] = true; if(y < m && x > 0 && !lights[x - 1][y].is_vertical(t)) free[3] = true; return free; } public: void input() { cin>>n>>m>>q; lights.assign(n, vector<Light>(m)); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { lights[i][j].input(); } } queries.resize(q); for(int i = 0; i < q; ++i) { queries[i].input(); } } void calculate() { // preprocess for each t % MAX_PERIOD time.resize(MAX_PERIOD); for(int t = 0; t < MAX_PERIOD; ++t) { visited.assign(n + 1, vector<bool>(m + 1, false)); time[t].starts.push_back(0); dfs(0, 0, t); if(visited[n][0]) { time[t].is_vertical = true; for(int i = 0; i <= m; ++i) { if(!visited[0][i]) { time[t].starts.push_back(i); dfs(0, i, t); } } }else { time[t].is_vertical = false; for(int i = 0; i <= n; ++i) { if(!visited[i][0]) { time[t].starts.push_back(i); dfs(i, 0, t); } } } } // calculate queries result.resize(q); for(int i = 0; i < q; ++i) { int t = queries[i].t % MAX_PERIOD; int t_real = queries[i].t; bool is_vertical = time[t].is_vertical; int a, b; if(is_vertical) { a = queries[i].y1, b = queries[i].y2; }else { a = queries[i].x1, b = queries[i].x2; } int s_a = time[t].find_start(a); int s_b = time[t].find_start(b); while(s_a != s_b) { if(a < b) { a = time[t].starts[s_a + 1] - 1; }else { a = time[t].starts[s_a]; } t = (t + 1) % MAX_PERIOD; ++t_real; if(time[t].is_vertical != is_vertical) { // now we can get wherever we want break; } s_a = time[t].find_start(a); s_b = time[t].find_start(b); } result[i] = t_real; } } void print() { for(int i = 0; i < q; ++i) { cout<<result[i]<<'\n'; } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); Solution s; s.input(); s.calculate(); s.print(); } /* 2 2 7 01 1100 001 10 0 0 0 2 2 1 0 1 2 1 5 2 1 0 0 0 0 2 2 2 15 1 1 0 0 16 1 1 0 0 7 2 2 2 2 */
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 | #include <iostream> #include <vector> using namespace std; const int MAX_PERIOD = 840; // NWW(2, 3, 4, 5, 6, 7, 8) = 3*5*7*8 = 840 struct Light { int period; vector<bool> data; void input() { string s; cin>>s; period = s.size(); data.resize(period); for(int i = 0; i < period; ++i) { data[i] = s[i] == '0'; } } bool is_vertical(int t) { return data[t % period]; } }; struct Time { bool is_vertical; vector<int> starts; int find_start(int x) { int l = 0, r = starts.size() - 1; while(l < r) { int mid = (l + r + 1) >> 1; if(starts[mid] > x) { r = mid - 1; }else { l = mid; } } return l; } }; struct Query { int t, x1, y1, x2, y2; void input() { cin>>t>>x1>>y1>>x2>>y2; } }; class Solution { int n, m, q; vector<vector<Light> > lights; vector<Time> time; vector<Query> queries; vector<int> result; vector<vector<bool> > visited; void dfs(int x, int y, int t) { if(visited[x][y]) return; visited[x][y] = true; vector<bool> free = calc_possible(x, y, t); if(free[0]) { dfs(x - 1, y, t); } if(free[1]) { dfs(x, y - 1, t); } if(free[2]) { dfs(x + 1, y, t); } if(free[3]) { dfs(x, y + 1, t); } } vector<bool> calc_possible(int x, int y, int t) { vector<bool> free(4, false); if(x > 0 && y < m && lights[x - 1][y].is_vertical(t)) free[0] = true; if(x > 0 && y > 0 && lights[x - 1][y - 1].is_vertical(t)) free[0] = true; if(y > 0 && x < n && !lights[x][y - 1].is_vertical(t)) free[1] = true; if(y > 0 && x > 0 && !lights[x - 1][y - 1].is_vertical(t)) free[1] = true; if(x < n && y < m && lights[x][y].is_vertical(t)) free[2] = true; if(x < n && y > 0 && lights[x][y - 1].is_vertical(t)) free[2] = true; if(y < m && x < n && !lights[x][y].is_vertical(t)) free[3] = true; if(y < m && x > 0 && !lights[x - 1][y].is_vertical(t)) free[3] = true; return free; } public: void input() { cin>>n>>m>>q; lights.assign(n, vector<Light>(m)); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { lights[i][j].input(); } } queries.resize(q); for(int i = 0; i < q; ++i) { queries[i].input(); } } void calculate() { // preprocess for each t % MAX_PERIOD time.resize(MAX_PERIOD); for(int t = 0; t < MAX_PERIOD; ++t) { visited.assign(n + 1, vector<bool>(m + 1, false)); time[t].starts.push_back(0); dfs(0, 0, t); if(visited[n][0]) { time[t].is_vertical = true; for(int i = 0; i <= m; ++i) { if(!visited[0][i]) { time[t].starts.push_back(i); dfs(0, i, t); } } }else { time[t].is_vertical = false; for(int i = 0; i <= n; ++i) { if(!visited[i][0]) { time[t].starts.push_back(i); dfs(i, 0, t); } } } } // calculate queries result.resize(q); for(int i = 0; i < q; ++i) { int t = queries[i].t % MAX_PERIOD; int t_real = queries[i].t; bool is_vertical = time[t].is_vertical; int a, b; if(is_vertical) { a = queries[i].y1, b = queries[i].y2; }else { a = queries[i].x1, b = queries[i].x2; } int s_a = time[t].find_start(a); int s_b = time[t].find_start(b); while(s_a != s_b) { if(a < b) { a = time[t].starts[s_a + 1] - 1; }else { a = time[t].starts[s_a]; } t = (t + 1) % MAX_PERIOD; ++t_real; if(time[t].is_vertical != is_vertical) { // now we can get wherever we want break; } s_a = time[t].find_start(a); s_b = time[t].find_start(b); } result[i] = t_real; } } void print() { for(int i = 0; i < q; ++i) { cout<<result[i]<<'\n'; } } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); Solution s; s.input(); s.calculate(); s.print(); } /* 2 2 7 01 1100 001 10 0 0 0 2 2 1 0 1 2 1 5 2 1 0 0 0 0 2 2 2 15 1 1 0 0 16 1 1 0 0 7 2 2 2 2 */ |