博客
关于我
Problem N HDU 2612 Find a way (两次BFS求最值)
阅读量:796 次
发布时间:2023-03-04

本文共 2995 字,大约阅读时间需要 9 分钟。

这道题的目标是通过计算Y和M到同一个KFC的最短路径之和,找出最小的总时间。以下是详细的解题步骤和代码实现。

解题思路

  • 问题分析:Y和M分别从起点出发,移动到同一个KFC,计算总时间。每一步移动固定为11分钟,相当于网格图中的单位距离。
  • 算法选择:使用广度优先搜索(BFS)分别计算Y和M到每个KFC的最短路径。
  • 复杂度分析:网格大小为200x200,BFS时间复杂度为O(n*m),每个测试用例的处理时间是可接受的。
  • 实现步骤
    • 读取输入,找到Y和M的位置。
    • BFS计算Y到所有KFC的最短距离。
    • BFS计算M到所有KFC的最短距离。
    • 遍历所有KFC,计算距离之和并找出最小值。
  • 优化注意:避免重复计算,确保处理多个测试用例时效率高。
  • 代码实现

    #include 
    #include
    #include
    #include
    using namespace std;#define INF 99999999#define MAX 233struct node { int x, y;};queue
    Q;int x1, y1, x2, y2;char grid[MAX][MAX];int book[MAX][MAX][2];int nxt[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};int can_move(int x, int y, int tag) { if (x < 0 || x >= n || y < 0 || y >= m) return 0; if (book[x][y][tag] != 0) return 0; if (grid[x][y] == 'y' || grid[x][y] == 'm' || grid[x][y] == '#') return 0; return 1;}void bfs(node start, int tag) { book[start.x][start.y][tag] = 0; Q.push(start); while (!Q.empty()) { node current = Q.front(); Q.pop(); for (int i = 0; i < 4; ++i) { int tx = current.x + nxt[i][0]; int ty = current.y + nxt[i][1]; if (can_move(tx, ty, tag)) { if (book[tx][ty][tag] == INF) { book[tx][ty][tag] = book[current.x][current.y][tag] + 1; node new_node; new_node.x = tx; new_node.y = ty; Q.push(new_node); } } } }}int main() { while (scanf("%d%d", &n, &m) != EOF) { for (int i = 0; i < n; ++i) { scanf("%s", grid[i]); for (int j = 0; j < m; ++j) { if (grid[i][j] == 'Y') { x1 = i, y1 = j; } else if (grid[i][j] == 'M') { x2 = i, y2 = j; } else if (grid[i][j] == '@') { int k = i; int l = j; // Skip processing KFC for now } } } node start_y; start_y.x = x1, start_y.y = y1; bfs(start_y, 0); node start_m; start_m.x = x2, start_m.y = y2; bfs(start_m, 1); int min_total = INF; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] == '@') { if (book[i][j][0] != 0 && book[i][j][1] != 0) { int total = book[i][j][0] + book[i][j][1]; if (total < min_total) { min_total = total; } } } } } printf("%d\n", min_total * 11); memset(book, 0, sizeof(book)); } return 0;}

    代码解释

  • 结构体定义node结构体用于存储坐标。
  • 队列定义:用于BFS遍历,存储当前的位置和步数。
  • 移动判断函数:检查是否可以移动到目标位置,避免越界和障碍物。
  • BFS函数:计算从起点到各个位置的最短距离,标记为book数组。
  • 主函数:读取输入,找到Y和M的位置,调用BFS计算最短距离,遍历KFC计算最短总距离并输出结果。
  • 总结

    通过以上步骤,我们可以高效地解决问题,找到Y和M到同一个KFC的最短路径之和,并输出结果。该方法利用BFS算法,确保了计算的准确性和效率。

    转载地址:http://lqxfk.baihongyu.com/

    你可能感兴趣的文章
    POJ 3670 DP LIS?
    查看>>
    POJ 3683 Priest John's Busiest Day (算竞进阶习题)
    查看>>
    POJ 3988 Selecting courses
    查看>>
    POJ 4020 NEERC John's inversion 贪心+归并求逆序对
    查看>>
    poj 4044 Score Sequence(暴力)
    查看>>
    POJ 基础数据结构
    查看>>
    POJ 题目3020 Antenna Placement(二分图)
    查看>>
    Poj(1797) Dijkstra对松弛条件的变形
    查看>>
    POJ--2391--Ombrophobic Bovines【分割点+Floyd+Dinic优化+二分法答案】最大网络流量
    查看>>
    Qt笔记——SQLite初探QSqlDatabase QSqlQuery
    查看>>
    POJ-1163-The Triangle
    查看>>
    POJ-Fence Repair 哈夫曼树
    查看>>
    poj1061 - 同余方程,二元一次不定方程
    查看>>
    Qt笔记——SQLite再探
    查看>>
    poj1068Parencodings
    查看>>
    poj1182(带权并查集)
    查看>>
    POJ1182(带权并查集)
    查看>>
    Qt笔记——Qt初探、PyQt5和Qt5
    查看>>
    poj1190生日蛋糕
    查看>>
    POJ1218 HDU1337 ZOJ1350 UVALive2557 THE DRUNK JAILER
    查看>>