Submission #691652
Source Code Expand
#include <queue> using namespace std; #define INF (1 << 8) #define OK '.' #define NG '#' #define UPDATE_CASE(yy, xx, cost) \ if (puzzle[(yy)][(xx)] == OK && shortest_paths[(yy)][(xx)] > (cost) + 1) { \ shortest_paths[(yy)][(xx)] = (cost) + 1; \ possitions.push(make_pair((yy), (xx))); \ if (yy == gy - 1 && xx == gx - 1) break; \ } int main(void) { unsigned int R, C; unsigned int sy, sx, gy, gx; cin >> R >> C; cin >> sy >> sx >> gy >> gx; unsigned char** puzzle = new unsigned char*[R]; unsigned int** shortest_paths = new unsigned int*[R]; for (int i = 0; i < R; ++i) { puzzle[i] = new unsigned char[C]; shortest_paths[i] = new unsigned int[C]; for (int j = 0; j < C; ++j) { cin >> puzzle[i][j]; shortest_paths[i][j] = INF; } } queue<pair<unsigned int, unsigned int>> possitions; possitions.push(make_pair(sy - 1, sx - 1)); // add the start position shortest_paths[sy - 1][sx - 1] = 0; while (!possitions.empty()) { auto pos = possitions.front(); possitions.pop(); const unsigned int y = pos.first; const unsigned int x = pos.second; unsigned int current_cost = shortest_paths[y][x]; UPDATE_CASE(y - 1, x, current_cost); // above UPDATE_CASE(y, x - 1, current_cost); // left UPDATE_CASE(y, x + 1, current_cost); // right UPDATE_CASE(y + 1, x, current_cost); // bottom } cout << shortest_paths[gy - 1][gx - 1] << endl; for (int i = 0; i < R; ++i) { delete[] puzzle[i]; delete[] shortest_paths[i]; } delete[] puzzle; delete[] shortest_paths; return 0; }
Submission Info
Submission Time | |
---|---|
Task | A - 幅優先探索 |
User | narit |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1751 Byte |
Status | CE |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:18:5: error: ‘cin’ was not declared in this scope cin >> R >> C; ^ ./Main.cpp:49:5: error: ‘cout’ was not declared in this scope cout << shortest_paths[gy - 1][gx - 1] << endl; ^ ./Main.cpp:49:47: error: ‘endl’ was not declared in this scope cout << shortest_paths[gy - 1][gx - 1] << endl; ^