// Krzysztof Baranski (2021.12.10) #include <cstdio> #include <vector> #include <set> #include <unordered_set> #include <unordered_map> using namespace std; #define ENCODE_XY(x,y) (((x)<<15)|(y)) #define DECODE_X(h) ((h)>>15) #define DECODE_Y(h) ((h)&0b111111111111111) const int INF = 1000000001; const long long LINF = 1000000000000000001; inline int min(int x, int y) { return (x < y ? x : y); } struct TrafficLight { char configuration; char cycle; void parse(const char* configuration) { int i = 0; this->configuration = 0; for(; configuration[i] != '\0'; ++i) if(configuration[i] == '1') this->configuration|=((char)(1<<i)); this->cycle = i; } bool is_green_we(int time) { return (this->configuration&(1<<(time%this->cycle))) > 0; } bool is_green_ns(int time) { return (this->configuration&(1<<(time%this->cycle))) == 0; } int count_delay_ns(long long current_time) { int current_time_mod = ((int)(current_time%this->cycle)); for(int i=0; i<8; ++i) { if(is_green_ns(current_time_mod+i)) return i; } return INF; } int count_delay_we(long long current_time) { int current_time_mod = ((int)(current_time%this->cycle)); for(int i=0; i<8; ++i) { if(is_green_we(current_time_mod+i)) return i; } return INF; } }; struct Bajtopolis { int n; int m; vector<vector<TrafficLight> > traffic_lights; Bajtopolis(int n, int m) : n(n), m(m) { traffic_lights.clear(); } void load_traffic_lights_configurations() { char buffer[10]; for(int i=0; i<this->n; ++i) { vector<TrafficLight> lane; for(int j=0; j<this->m; ++j) { scanf("%s", buffer); TrafficLight traffic_light; traffic_light.parse(buffer); lane.push_back(traffic_light); } this->traffic_lights.push_back(lane); } } long long load_and_execute_query(int query_number) { int t, a, b, c, d; scanf("%d %d %d %d %d", &t, &a, &b, &c, &d); long long arrived_at = find_shortest_time_between(a, b, c, d, t); return arrived_at; } int count_delay_ns(int i, int j, long long current_time) { if(i<0 || i>=this->n || j<0 || j>=this->m) return INF; return this->traffic_lights[i][j].count_delay_ns(current_time); } int count_delay_we(int i, int j, long long current_time) { if(i<0 || i>=this->n || j<0 || j>=this->m) return INF; return this->traffic_lights[i][j].count_delay_we(current_time); } long long find_shortest_time_between(int a_x, int a_y, int b_x, int b_y, int start_time) { set<pair<long long, int> > queue; unordered_set<int> visited; unordered_map<int, long long> shortest_distances; queue.clear(); visited.clear(); shortest_distances.clear(); shortest_distances[ENCODE_XY(a_x, a_y)] = start_time; queue.insert(make_pair(start_time, ENCODE_XY(a_x, a_y))); while(!queue.empty()) { int encoded_c = queue.begin()->second; int c_x = DECODE_X(encoded_c); int c_y = DECODE_Y(encoded_c); queue.erase(queue.begin()); if(c_x == b_x && c_y == b_y) return shortest_distances[encoded_c]; if(!visited.count(encoded_c)) { visited.insert(encoded_c); long long current_time = shortest_distances[encoded_c]; if(c_y-1 >= 0) { int delay_on_trafic_lights_1 = count_delay_we(c_x-1, c_y-1, current_time); int delay_on_trafic_lights_2 = count_delay_we(c_x, c_y-1, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x; int d_y = c_y - 1; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } if(c_x-1 >= 0) { int delay_on_trafic_lights_1 = count_delay_ns(c_x-1, c_y-1, current_time); int delay_on_trafic_lights_2 = count_delay_ns(c_x-1, c_y, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x - 1; int d_y = c_y; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } if(c_y+1 <= this->m) { int delay_on_trafic_lights_1 = count_delay_we(c_x-1, c_y, current_time); int delay_on_trafic_lights_2 = count_delay_we(c_x, c_y, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x; int d_y = c_y + 1; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } if(c_x+1 <= this->n) { int delay_on_trafic_lights_1 = count_delay_ns(c_x, c_y-1, current_time); int delay_on_trafic_lights_2 = count_delay_ns(c_x, c_y, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x + 1; int d_y = c_y; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } } } return shortest_distances[ENCODE_XY(b_x, b_y)]; } }; int main() { int n, m, q; scanf("%d %d %d\n", &n, &m, &q); Bajtopolis bajtopolis(n, m); bajtopolis.load_traffic_lights_configurations(); for(int i=1; i<=q; ++i) { long long result = bajtopolis.load_and_execute_query(i); printf("%lld\n", result); } 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 220 221 222 | // Krzysztof Baranski (2021.12.10) #include <cstdio> #include <vector> #include <set> #include <unordered_set> #include <unordered_map> using namespace std; #define ENCODE_XY(x,y) (((x)<<15)|(y)) #define DECODE_X(h) ((h)>>15) #define DECODE_Y(h) ((h)&0b111111111111111) const int INF = 1000000001; const long long LINF = 1000000000000000001; inline int min(int x, int y) { return (x < y ? x : y); } struct TrafficLight { char configuration; char cycle; void parse(const char* configuration) { int i = 0; this->configuration = 0; for(; configuration[i] != '\0'; ++i) if(configuration[i] == '1') this->configuration|=((char)(1<<i)); this->cycle = i; } bool is_green_we(int time) { return (this->configuration&(1<<(time%this->cycle))) > 0; } bool is_green_ns(int time) { return (this->configuration&(1<<(time%this->cycle))) == 0; } int count_delay_ns(long long current_time) { int current_time_mod = ((int)(current_time%this->cycle)); for(int i=0; i<8; ++i) { if(is_green_ns(current_time_mod+i)) return i; } return INF; } int count_delay_we(long long current_time) { int current_time_mod = ((int)(current_time%this->cycle)); for(int i=0; i<8; ++i) { if(is_green_we(current_time_mod+i)) return i; } return INF; } }; struct Bajtopolis { int n; int m; vector<vector<TrafficLight> > traffic_lights; Bajtopolis(int n, int m) : n(n), m(m) { traffic_lights.clear(); } void load_traffic_lights_configurations() { char buffer[10]; for(int i=0; i<this->n; ++i) { vector<TrafficLight> lane; for(int j=0; j<this->m; ++j) { scanf("%s", buffer); TrafficLight traffic_light; traffic_light.parse(buffer); lane.push_back(traffic_light); } this->traffic_lights.push_back(lane); } } long long load_and_execute_query(int query_number) { int t, a, b, c, d; scanf("%d %d %d %d %d", &t, &a, &b, &c, &d); long long arrived_at = find_shortest_time_between(a, b, c, d, t); return arrived_at; } int count_delay_ns(int i, int j, long long current_time) { if(i<0 || i>=this->n || j<0 || j>=this->m) return INF; return this->traffic_lights[i][j].count_delay_ns(current_time); } int count_delay_we(int i, int j, long long current_time) { if(i<0 || i>=this->n || j<0 || j>=this->m) return INF; return this->traffic_lights[i][j].count_delay_we(current_time); } long long find_shortest_time_between(int a_x, int a_y, int b_x, int b_y, int start_time) { set<pair<long long, int> > queue; unordered_set<int> visited; unordered_map<int, long long> shortest_distances; queue.clear(); visited.clear(); shortest_distances.clear(); shortest_distances[ENCODE_XY(a_x, a_y)] = start_time; queue.insert(make_pair(start_time, ENCODE_XY(a_x, a_y))); while(!queue.empty()) { int encoded_c = queue.begin()->second; int c_x = DECODE_X(encoded_c); int c_y = DECODE_Y(encoded_c); queue.erase(queue.begin()); if(c_x == b_x && c_y == b_y) return shortest_distances[encoded_c]; if(!visited.count(encoded_c)) { visited.insert(encoded_c); long long current_time = shortest_distances[encoded_c]; if(c_y-1 >= 0) { int delay_on_trafic_lights_1 = count_delay_we(c_x-1, c_y-1, current_time); int delay_on_trafic_lights_2 = count_delay_we(c_x, c_y-1, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x; int d_y = c_y - 1; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } if(c_x-1 >= 0) { int delay_on_trafic_lights_1 = count_delay_ns(c_x-1, c_y-1, current_time); int delay_on_trafic_lights_2 = count_delay_ns(c_x-1, c_y, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x - 1; int d_y = c_y; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } if(c_y+1 <= this->m) { int delay_on_trafic_lights_1 = count_delay_we(c_x-1, c_y, current_time); int delay_on_trafic_lights_2 = count_delay_we(c_x, c_y, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x; int d_y = c_y + 1; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } if(c_x+1 <= this->n) { int delay_on_trafic_lights_1 = count_delay_ns(c_x, c_y-1, current_time); int delay_on_trafic_lights_2 = count_delay_ns(c_x, c_y, current_time); int delay = min(delay_on_trafic_lights_1, delay_on_trafic_lights_2); int d_x = c_x + 1; int d_y = c_y; int encoded_d = ENCODE_XY(d_x, d_y); long long current_distance_to_d = shortest_distances.count(encoded_d) ? shortest_distances[encoded_d] : LINF; long long relaxed_distance = current_time + delay; if(current_distance_to_d > relaxed_distance) { shortest_distances[encoded_d] = relaxed_distance; queue.insert(make_pair(relaxed_distance, encoded_d)); } } } } return shortest_distances[ENCODE_XY(b_x, b_y)]; } }; int main() { int n, m, q; scanf("%d %d %d\n", &n, &m, &q); Bajtopolis bajtopolis(n, m); bajtopolis.load_traffic_lights_configurations(); for(int i=1; i<=q; ++i) { long long result = bajtopolis.load_and_execute_query(i); printf("%lld\n", result); } return 0; } |