
本文共 3437 字,大约阅读时间需要 11 分钟。
������������������������������������
������������
������������������������������������������������������������������������������������5��5������������������������������1���������������0���������������������������������������������������������������������(0,0)������������������������(4,4)������������������������������������������������������������������������������������
������������
������������������������������������������������������������������BFS������������BFS���������������������������������������������������������������������������������������������������������������������������������������������
���������������������������BFS������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������
BFS������������������������
#include#include #include #include #include using namespace std;#define MAX_SIZE 5#define WALL 1#define EMPTY 0#define DX {1, -1, 0, 0}#define DY {0, 0, 1, -1}typedef struct { int x, y;} Point;vector dir = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }; bool isWall(int x, int y) { return (x < 0 || x >= MAX_SIZE || y < 0 || y >= MAX_SIZE || grid[x][y] == WALL); } void bfs() { vector > grid = readGrid(); vector path; queue q; // ��������������������������� Point start = {MAX_SIZE - 1, MAX_SIZE - 1}; q.push(start); markAllVisited as false; visited[start.x][start.y] = true; while (!q.empty()) { Point now = q.front(); q.pop(); path.push_back(now); for (each direction in dir) { Point next = now + direction; if (!isWall(next) && !visited[next]) { visited[next] = true; q.push(next); } } if (now == start) continue; // ��������������������������������������������������������������� // ������������������������ if (now.x == 0 && now.y == 0) { reversePath(); break; } } // BFS��������������������������������������������������������������������� } // ��������������������������������� void readInput() { for (int x = 0; x < MAX_SIZE; x++) { for (int y = 0; y < MAX_SIZE; y++) { char c; cin >> c; grid[x][y] = c == '1' ? WALL : EMPTY; } } } // ������������ void reversePath() { for (int i = path.size() - 1; i >= 0; i--) { output.push_back(path[i]); } } int main() { readInput(); bfs(); for (const auto& point : output) { cout << "(" << point.x << ", " << point.y << ")" << endl; } return 0; }
������
������������������������������������������������������������������������������������������������������������������BFS���������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������
������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
