#include <cstdio> #include <vector> #include <string> using namespace std; #define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i)) struct Step { bool is_horizontal; vector<pair<int, int> > chunks; pair<int, int> find_chunk(int val) { int L = 0, R = chunks.size()-1; while (L<R) { int M = (L+R)>>1; if (val < chunks[M].first) R = M - 1; else if (val > chunks[M].second) L = M + 1; else return chunks[M]; } return chunks[L]; } void make_move(int &val, int &dest) { pair<int,int> chunk = find_chunk(val); if (dest < chunk.first) val = chunk.first; else if (dest > chunk.second) val = chunk.second; else val = dest; } }; int n, m, q; int cycle_length; vector<vector<string> > B; vector<Step> steps; bool horizontal_exists, vertical_exists; char S[10]; int t, a, b, c, d; vector<vector<char> > state; int handle_question(int t, int a, int b, int c, int d) { if (a == c && b == d) return 0; int result = 0, cycle_t = t%cycle_length; while(true) { Step &step = steps[cycle_t]; if (step.is_horizontal) { b = d; step.make_move(a, c); } else { a = c; step.make_move(b, d); } if (a == c && b == d) return result; ++result; ++cycle_t; if (cycle_t == cycle_length) cycle_t = 0; } } int main() { cycle_length = 1; scanf("%d %d %d", &n, &m, &q); state.resize(n); B.resize(n); FOR(y,0,n) { state[y].resize(m, '.'); B[y].resize(m); FOR(x,0,m) { scanf("%s", S); B[y][x] = string(S); int len = B[y][x].size(); if (len % 2 == 1) { if (cycle_length % len != 0) cycle_length *= len; } else if (len == 6) { if (cycle_length % 3 != 0) cycle_length *= 3; if (cycle_length % 2 != 0) cycle_length *= 2; } else { while (cycle_length % len != 0) cycle_length *= 2; } } } horizontal_exists = vertical_exists = false; steps.resize(cycle_length); FOR(i,0,cycle_length) { // Prepare state at time 'i' vector<int> H1(n, 0), V0(m, 0); FOR(y,0,n) FOR(x,0,m) { int pos = i % B[y][x].size(); char c = state[y][x] = B[y][x][pos]; if (c == '0') ++V0[x]; else ++H1[y]; } // Find step type int type = 0; FOR(y,0,n) if (H1[y] == n) { type = 1; break; } if (type == 0) FOR(x,0,m) if (V0[x] == n) { type = 2; break; } // Create step if (type <= 1) { steps[i].is_horizontal = true; horizontal_exists = true; int start = 0; FOR(y,0,n) { if (H1[y] == n) { steps[i].chunks.push_back(make_pair(start, y)); start = y + 1; } } steps[i].chunks.push_back(make_pair(start, n)); } else { steps[i].is_horizontal = false; vertical_exists = true; int start = 0; FOR(x,0,m) { if (V0[x] == m) { steps[i].chunks.push_back(make_pair(start, x)); start = x + 1; } } steps[i].chunks.push_back(make_pair(start, n)); } } FOR(i,0,q) { scanf("%d %d %d %d %d", &t, &a, &b, &c, &d); // (a,b) -> (c,d) int steps_needed = handle_question(t, a, b, c, d); printf("%d\n", t + steps_needed); } }
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 | #include <cstdio> #include <vector> #include <string> using namespace std; #define FOR(i,a,b) for(int (i)=(int)(a); (i)!=(int)(b); ++(i)) struct Step { bool is_horizontal; vector<pair<int, int> > chunks; pair<int, int> find_chunk(int val) { int L = 0, R = chunks.size()-1; while (L<R) { int M = (L+R)>>1; if (val < chunks[M].first) R = M - 1; else if (val > chunks[M].second) L = M + 1; else return chunks[M]; } return chunks[L]; } void make_move(int &val, int &dest) { pair<int,int> chunk = find_chunk(val); if (dest < chunk.first) val = chunk.first; else if (dest > chunk.second) val = chunk.second; else val = dest; } }; int n, m, q; int cycle_length; vector<vector<string> > B; vector<Step> steps; bool horizontal_exists, vertical_exists; char S[10]; int t, a, b, c, d; vector<vector<char> > state; int handle_question(int t, int a, int b, int c, int d) { if (a == c && b == d) return 0; int result = 0, cycle_t = t%cycle_length; while(true) { Step &step = steps[cycle_t]; if (step.is_horizontal) { b = d; step.make_move(a, c); } else { a = c; step.make_move(b, d); } if (a == c && b == d) return result; ++result; ++cycle_t; if (cycle_t == cycle_length) cycle_t = 0; } } int main() { cycle_length = 1; scanf("%d %d %d", &n, &m, &q); state.resize(n); B.resize(n); FOR(y,0,n) { state[y].resize(m, '.'); B[y].resize(m); FOR(x,0,m) { scanf("%s", S); B[y][x] = string(S); int len = B[y][x].size(); if (len % 2 == 1) { if (cycle_length % len != 0) cycle_length *= len; } else if (len == 6) { if (cycle_length % 3 != 0) cycle_length *= 3; if (cycle_length % 2 != 0) cycle_length *= 2; } else { while (cycle_length % len != 0) cycle_length *= 2; } } } horizontal_exists = vertical_exists = false; steps.resize(cycle_length); FOR(i,0,cycle_length) { // Prepare state at time 'i' vector<int> H1(n, 0), V0(m, 0); FOR(y,0,n) FOR(x,0,m) { int pos = i % B[y][x].size(); char c = state[y][x] = B[y][x][pos]; if (c == '0') ++V0[x]; else ++H1[y]; } // Find step type int type = 0; FOR(y,0,n) if (H1[y] == n) { type = 1; break; } if (type == 0) FOR(x,0,m) if (V0[x] == n) { type = 2; break; } // Create step if (type <= 1) { steps[i].is_horizontal = true; horizontal_exists = true; int start = 0; FOR(y,0,n) { if (H1[y] == n) { steps[i].chunks.push_back(make_pair(start, y)); start = y + 1; } } steps[i].chunks.push_back(make_pair(start, n)); } else { steps[i].is_horizontal = false; vertical_exists = true; int start = 0; FOR(x,0,m) { if (V0[x] == m) { steps[i].chunks.push_back(make_pair(start, x)); start = x + 1; } } steps[i].chunks.push_back(make_pair(start, n)); } } FOR(i,0,q) { scanf("%d %d %d %d %d", &t, &a, &b, &c, &d); // (a,b) -> (c,d) int steps_needed = handle_question(t, a, b, c, d); printf("%d\n", t + steps_needed); } } |