#include <sstream> #include <vector> #include <iostream> #include <list> #include <algorithm> #include <queue> #include <fstream> #include <chrono> #include <string.h> using namespace std; /* 6 8 1 00100110 110110 110110 00100110 110110 00100110 110110 110110 110110 00100110 00100110 00100110 110110 00100110 110110 110110 110110 00100110 110110 00100110 110110 110110 110110 110110 00100110 00100110 00100110 110110 110110 00100110 00100110 110110 110110 00100110 00100110 110110 00100110 00100110 00100110 00100110 110110 110110 110110 00100110 110110 00100110 00100110 110110 943222083 2 3 2 8 */ class Point { public: int x; int y; long moment; }; struct Comp { bool operator()(const Point &a, const Point &b) { return a.moment > b.moment; } }; long calcCostGoVertically(string word, long now) { for (int i = 0; i < word.size(); i++) { if (word[(now + i) % word.size()] == '0') { return i; } } return -1; //should never happen }; long calcCostGoHorizontally(string word, long now) { for (int i = 0; i < word.size(); i++) { if (word[(now + i) % word.size()] == '1') { return i; } } return -1; //should never happen }; int min(int a, int b) { if (a > b) { return b; } return a; } long calcTime(int n, int m, string **lights, int t, int start_x, int start_y, int end_x, int end_y) { long **graph = new long*[n + 1]; for (int i = 0; i < n + 1; i++) { graph[i] = new long[m + 1]; for (int j = 0; j < m + 1; j++) { graph[i][j] = -1; } } priority_queue<Point, vector<Point>, Comp> queue; Point startPoint; startPoint.x = start_x; startPoint.y = start_y; startPoint.moment = t; queue.push(startPoint); while (!queue.empty()) { Point current = queue.top(); queue.pop(); if (current.x == end_x && current.y == end_y) { // for (int i = 0; i < n + 1; i++) { // for (int j = 0; j < m + 1; j++) { // cout << graph[i][j] - t << " "; // } // cout << endl; // } return current.moment; } if(graph[current.x][current.y] == -1 || current.moment < graph[current.x][current.y]) { graph[current.x][current.y] = current.moment; // cout << "Putting " << current.moment << " in x=" << current.x << "and y= " << current.y << endl; // else { // cout << "ERROR " << graph[current.x][current.y] << " x=" << current.x << " y= " << current.y << endl; // } // cout << "popping Point, x= " << current.x << " y= " << current.y << " moment = " << current.moment << endl; if (current.x > 0) { long costGoUp = 1000; if (current.y > 0) { costGoUp = calcCostGoVertically(lights[current.x - 1][current.y - 1], current.moment); } if (current.y < m) { costGoUp = min(costGoUp, calcCostGoVertically(lights[current.x - 1][current.y], current.moment)); } if (graph[current.x - 1][current.y] == -1 || (graph[current.x - 1][current.y] > (current.moment + costGoUp))) { // graph[current.x - 1][current.y] = current.moment + costGoUp; Point newPoint; newPoint.x = current.x - 1; newPoint.y = current.y; newPoint.moment = current.moment + costGoUp; queue.push(newPoint); // cout << "Adding UP Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } if (current.x < n) { long costGoDown = 1000; if (current.y > 0) { costGoDown = calcCostGoVertically(lights[current.x][current.y - 1], current.moment); } if (current.y < m) { costGoDown = min(costGoDown, calcCostGoVertically(lights[current.x][current.y], current.moment)); } if (graph[current.x + 1][current.y] == -1 || (graph[current.x + 1][current.y] > (current.moment + costGoDown))) { // graph[current.x + 1][current.y] = current.moment + costGoDown; Point newPoint; newPoint.x = current.x + 1; newPoint.y = current.y; newPoint.moment = current.moment + costGoDown; queue.push(newPoint); // cout << "Adding DOWN Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } if (current.y > 0) { long costGoLeft = 1000; if (current.x > 0) { costGoLeft = calcCostGoHorizontally(lights[current.x - 1][current.y - 1], current.moment); } if (current.x < n) { costGoLeft = min(costGoLeft, calcCostGoHorizontally(lights[current.x][current.y - 1], current.moment)); } if (graph[current.x][current.y - 1] == -1 || (graph[current.x][current.y - 1] > (current.moment + costGoLeft))) { // graph[current.x][current.y - 1] = current.moment + costGoLeft; Point newPoint; newPoint.x = current.x; newPoint.y = current.y - 1; newPoint.moment = current.moment + costGoLeft; queue.push(newPoint); // cout << "Adding LEFT Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } if (current.y < m) { long costGoRight = 1000; if (current.x > 0) { costGoRight = calcCostGoHorizontally(lights[current.x - 1][current.y], current.moment); } if (current.x < n - 1) { costGoRight = min(costGoRight, calcCostGoHorizontally(lights[current.x][current.y], current.moment)); } if (graph[current.x][current.y + 1] == -1 || (graph[current.x][current.y + 1] > (current.moment + costGoRight))) { // graph[current.x][current.y + 1] = current.moment + costGoRight; Point newPoint; newPoint.x = current.x; newPoint.y = current.y + 1; newPoint.moment = current.moment + costGoRight; queue.push(newPoint); // cout << "Adding RIGHT Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } } } return 44; } int main() { // std::ifstream in_file("/Users/pro15/Downloads/tests_skr/in/4.in"); // std::ifstream ans_file("/Users/pro15/Downloads/tests_skr/out/4.out"); // // std::cin.rdbuf(in_file.rdbuf()); //redirect std::cin to in_file! int n, m, q, i, j; int t, start_x, start_y, end_x, end_y; cin >> n; cin >> m; cin >> q; string **lights = new string*[n]; for (i = 0; i < n; i++) { lights[i] = new string[m]; for (j = 0; j < m; j++) { cin >> lights[i][j]; } } int results[q]; for (i = 0; i < q; i++) { cin >> t; cin >> start_x; cin >> start_y; cin >> end_x; cin >> end_y; results[i] = calcTime(n, m, lights, t, start_x, start_y, end_x, end_y); } for (i = 0; i < q; i++) { cout << results[i] << endl; } // std::cin.rdbuf(ans_file.rdbuf()); //redirect std::cin to in_file! // int correctAnswer; // int incorrect = 0; // cout << "Number of tests : " << q << endl; // for (i = 0; i < q; i++) { // cin >> correctAnswer; // int result = results[i]; // if (result != correctAnswer) { // incorrect++; // cout << "INCORRECT " << i << " algo=" << result << " correct=" << correctAnswer << endl; // } // if(incorrect > 100) { // break; // } // } // auto stop = chrono::high_resolution_clock::now(); // auto duration = chrono::duration_cast<chrono::microseconds>(stop - start); // cout << "time micro seconds: " << duration.count() << 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 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | #include <sstream> #include <vector> #include <iostream> #include <list> #include <algorithm> #include <queue> #include <fstream> #include <chrono> #include <string.h> using namespace std; /* 6 8 1 00100110 110110 110110 00100110 110110 00100110 110110 110110 110110 00100110 00100110 00100110 110110 00100110 110110 110110 110110 00100110 110110 00100110 110110 110110 110110 110110 00100110 00100110 00100110 110110 110110 00100110 00100110 110110 110110 00100110 00100110 110110 00100110 00100110 00100110 00100110 110110 110110 110110 00100110 110110 00100110 00100110 110110 943222083 2 3 2 8 */ class Point { public: int x; int y; long moment; }; struct Comp { bool operator()(const Point &a, const Point &b) { return a.moment > b.moment; } }; long calcCostGoVertically(string word, long now) { for (int i = 0; i < word.size(); i++) { if (word[(now + i) % word.size()] == '0') { return i; } } return -1; //should never happen }; long calcCostGoHorizontally(string word, long now) { for (int i = 0; i < word.size(); i++) { if (word[(now + i) % word.size()] == '1') { return i; } } return -1; //should never happen }; int min(int a, int b) { if (a > b) { return b; } return a; } long calcTime(int n, int m, string **lights, int t, int start_x, int start_y, int end_x, int end_y) { long **graph = new long*[n + 1]; for (int i = 0; i < n + 1; i++) { graph[i] = new long[m + 1]; for (int j = 0; j < m + 1; j++) { graph[i][j] = -1; } } priority_queue<Point, vector<Point>, Comp> queue; Point startPoint; startPoint.x = start_x; startPoint.y = start_y; startPoint.moment = t; queue.push(startPoint); while (!queue.empty()) { Point current = queue.top(); queue.pop(); if (current.x == end_x && current.y == end_y) { // for (int i = 0; i < n + 1; i++) { // for (int j = 0; j < m + 1; j++) { // cout << graph[i][j] - t << " "; // } // cout << endl; // } return current.moment; } if(graph[current.x][current.y] == -1 || current.moment < graph[current.x][current.y]) { graph[current.x][current.y] = current.moment; // cout << "Putting " << current.moment << " in x=" << current.x << "and y= " << current.y << endl; // else { // cout << "ERROR " << graph[current.x][current.y] << " x=" << current.x << " y= " << current.y << endl; // } // cout << "popping Point, x= " << current.x << " y= " << current.y << " moment = " << current.moment << endl; if (current.x > 0) { long costGoUp = 1000; if (current.y > 0) { costGoUp = calcCostGoVertically(lights[current.x - 1][current.y - 1], current.moment); } if (current.y < m) { costGoUp = min(costGoUp, calcCostGoVertically(lights[current.x - 1][current.y], current.moment)); } if (graph[current.x - 1][current.y] == -1 || (graph[current.x - 1][current.y] > (current.moment + costGoUp))) { // graph[current.x - 1][current.y] = current.moment + costGoUp; Point newPoint; newPoint.x = current.x - 1; newPoint.y = current.y; newPoint.moment = current.moment + costGoUp; queue.push(newPoint); // cout << "Adding UP Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } if (current.x < n) { long costGoDown = 1000; if (current.y > 0) { costGoDown = calcCostGoVertically(lights[current.x][current.y - 1], current.moment); } if (current.y < m) { costGoDown = min(costGoDown, calcCostGoVertically(lights[current.x][current.y], current.moment)); } if (graph[current.x + 1][current.y] == -1 || (graph[current.x + 1][current.y] > (current.moment + costGoDown))) { // graph[current.x + 1][current.y] = current.moment + costGoDown; Point newPoint; newPoint.x = current.x + 1; newPoint.y = current.y; newPoint.moment = current.moment + costGoDown; queue.push(newPoint); // cout << "Adding DOWN Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } if (current.y > 0) { long costGoLeft = 1000; if (current.x > 0) { costGoLeft = calcCostGoHorizontally(lights[current.x - 1][current.y - 1], current.moment); } if (current.x < n) { costGoLeft = min(costGoLeft, calcCostGoHorizontally(lights[current.x][current.y - 1], current.moment)); } if (graph[current.x][current.y - 1] == -1 || (graph[current.x][current.y - 1] > (current.moment + costGoLeft))) { // graph[current.x][current.y - 1] = current.moment + costGoLeft; Point newPoint; newPoint.x = current.x; newPoint.y = current.y - 1; newPoint.moment = current.moment + costGoLeft; queue.push(newPoint); // cout << "Adding LEFT Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } if (current.y < m) { long costGoRight = 1000; if (current.x > 0) { costGoRight = calcCostGoHorizontally(lights[current.x - 1][current.y], current.moment); } if (current.x < n - 1) { costGoRight = min(costGoRight, calcCostGoHorizontally(lights[current.x][current.y], current.moment)); } if (graph[current.x][current.y + 1] == -1 || (graph[current.x][current.y + 1] > (current.moment + costGoRight))) { // graph[current.x][current.y + 1] = current.moment + costGoRight; Point newPoint; newPoint.x = current.x; newPoint.y = current.y + 1; newPoint.moment = current.moment + costGoRight; queue.push(newPoint); // cout << "Adding RIGHT Point, x= " << newPoint.x << " y= " << newPoint.y << " moment = " << newPoint.moment << endl; } } } } return 44; } int main() { // std::ifstream in_file("/Users/pro15/Downloads/tests_skr/in/4.in"); // std::ifstream ans_file("/Users/pro15/Downloads/tests_skr/out/4.out"); // // std::cin.rdbuf(in_file.rdbuf()); //redirect std::cin to in_file! int n, m, q, i, j; int t, start_x, start_y, end_x, end_y; cin >> n; cin >> m; cin >> q; string **lights = new string*[n]; for (i = 0; i < n; i++) { lights[i] = new string[m]; for (j = 0; j < m; j++) { cin >> lights[i][j]; } } int results[q]; for (i = 0; i < q; i++) { cin >> t; cin >> start_x; cin >> start_y; cin >> end_x; cin >> end_y; results[i] = calcTime(n, m, lights, t, start_x, start_y, end_x, end_y); } for (i = 0; i < q; i++) { cout << results[i] << endl; } // std::cin.rdbuf(ans_file.rdbuf()); //redirect std::cin to in_file! // int correctAnswer; // int incorrect = 0; // cout << "Number of tests : " << q << endl; // for (i = 0; i < q; i++) { // cin >> correctAnswer; // int result = results[i]; // if (result != correctAnswer) { // incorrect++; // cout << "INCORRECT " << i << " algo=" << result << " correct=" << correctAnswer << endl; // } // if(incorrect > 100) { // break; // } // } // auto stop = chrono::high_resolution_clock::now(); // auto duration = chrono::duration_cast<chrono::microseconds>(stop - start); // cout << "time micro seconds: " << duration.count() << endl; return 0; } |