sdnuoj1086
发布日期:2021-05-15 01:02:47 浏览次数:21 分类:精选文章

本文共 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���������������������������������������������������������������������������������������������������������������������������������������������������������������

������������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������

    上一篇:KMP算法
    下一篇:sdnuoj 1193.火柴棒等式

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月06日 13时40分50秒