Codevs 1215 迷宫 裸BFS

上一次打是在寒假,现在代码风格都有了很大的改变。
当时还是跟着伟大的红太阳 DQS(%%)学的呢。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

int n;
char maps[233][233];
bool v[233][233];
int dx[] = {0,1,0,-1,0};
int dy[] = {0,0,1,0,-1};
int sx, sy, ex, ey;

struct loc{
    int x, y;
};

queue <loc> q;
void bfs(int sx, int sy)
{
    q.push((loc){sx,sy});
    v[sx][sy] = 1;
    while(q.size())
    {
        loc nw = q.front();
        q.pop();
        if(nw.x == ex && nw.y == ey){
            cout << "YES" << endl;
            return;
        }
        for(int i = 1; i <= 4; i ++)
        {
            int x = nw.x + dx[i];
            int y = nw.y + dy[i];
            if(x > 0 && x <= n && y > 0 && y <= n && maps[x][y] != '#' && !v[x][y])
            {
                q.push((loc){x,y});
                v[x][y] = 1;
            }
        }
    }
    cout << "NO" << endl;
}

int main()
{
    int t;
    cin >> t;
    cin >> n;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++){
            cin >> maps[i][j];
            if(maps[i][j] == 's')
                sx = i,sy = j;
            else if(maps[i][j] == 'e')
                ex = i; ey = j;
        }
    bfs(sx,sy);
    return 0;
}
以上:2016-06-11 21:33:37

2016-9-28 17:04:55
重打:

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 20;
const int dx[] = {0,0,-1,0,1};
const int dy[] = {0,-1,0,1,0};
int n, sx, sy;
char maps[MAXN][MAXN];

struct Point{
    int x, y;
};

queue <Point> q;
void bfs(int sx, int sy)
{
    q.push((Point){sx,sy});
    maps[sx][sy] = '#';
    while(q.size())
    {
        Point now = q.front();
        q.pop(); 
        for(int i = 1; i <= 4; i ++)
        {
            int nx = now.x + dx[i];
            int ny = now.y + dy[i];
            if(maps[nx][ny] == 'e')
            {
                puts("YES");
                return;
            }
            if(nx >= 0 && nx <= n && ny >= 0 && ny <= n && maps[nx][ny] == '.')
            {
                maps[nx][ny] = '#';
                q.push((Point){nx, ny});
            }
        }
    }
    puts("NO");
}

void init()
{
    while(q.size())
        q.pop();
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        init();
        cin >> n;
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
            {
                char c = getchar();
                while(c != 's' && c != 'e' && c != '.' && c != '#')
                    c = getchar();
                maps[i][j] = c;
                if(c == 's')
                    sx = i, sy = j;
            }
        bfs(sx,sy);
    }
    return 0;
}
### 迷宫问题 BFS 算法模板 以下是基于引用内容构建的一个完整的迷宫问题 BFS 算法模板。该模板适用于求解从起点 `(sx, sy)` 到终点 `(ex, ey)` 的最短路径长度。 #### 数据结构定义 为了实现 BFS,通常需要一个队列来存储当前状态节点的信息。这里可以采用 `struct` 定义节点的状态,并通过数组模拟队列操作[^2]。 ```c++ #include <bits/stdc++.h> using namespace std; // 定义方向向量(上下左右) const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; // 定义节点结构体 struct Node { int x, y, step; Node(int _x, int _y, int _step) : x(_x), y(_y), step(_step) {} }; bool isValid(int nx, int ny, int n, int m, vector<vector<int>> &maze, vector<vector<bool>> &visited) { if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] == 0 && !visited[nx][ny]) { return true; } return false; } ``` #### 主函数逻辑 在主函数中初始化队列并执行 BFS 遍历过程。每次扩展时判断新位置是否合法且未访问过,如果是,则将其加入队列继续遍历[^3]。 ```c++ int bfs(int sx, int sy, int ex, int ey, vector<vector<int>> &maze) { int n = maze.size(); int m = maze[0].size(); // 初始化 visited 数组防止重复访问 vector<vector<bool>> visited(n, vector<bool>(m, false)); // 创建队列并将起始点入队 queue<Node> q; q.push(Node(sx, sy, 0)); visited[sx][sy] = true; while (!q.empty()) { Node current = q.front(); q.pop(); // 如果到达目标点则返回步数 if (current.x == ex && current.y == ey) { return current.step; } // 尝试四个方向移动 for (int i = 0; i < 4; ++i) { int nx = current.x + dx[i]; int ny = current.y + dy[i]; if (isValid(nx, ny, n, m, maze, visited)) { visited[nx][ny] = true; q.push(Node(nx, ny, current.step + 1)); } } } // 若无法抵达终点,返回 -1 return -1; } ``` #### 使用示例 下面是一个简单的测试案例展示如何调用上述 BFS 函数: ```c++ int main() { // 输入迷宫地图 vector<vector<int>> maze = { {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0} }; int sx = 0, sy = 0; // 起点坐标 int ex = 3, ey = 3; // 终点坐标 int result = bfs(sx, sy, ex, ey, maze); cout << "Minimum steps required: " << result << endl; return 0; } ``` 此代码片段展示了如何利用 BFS 来计算迷宫中最短路径所需的最少步数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值