UVA ~ 11573 ~ Ocean Currents (0-1 BFS)

题意

N*M的地图,每个点有水流方向,如果你在当前点不按照当前点的水流方向走就会花费1的体力,Q次询问,每次询问从(sx,sy)到(ex,ey)的最小花费

 

思路

01BFS,也可以用优先队列,优先队列会慢一点。

01BFS(deque):

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
const int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, m, d[MAXN][MAXN];
char MAP[MAXN][MAXN];
struct Node
{
    int x, y;
    Node(int x, int y) : x(x), y(y) {}
};
int BFS(int sx, int sy, int ex, int ey)
{
    memset(d, INF, sizeof(d));
    deque<Node> Q;
    d[sx][sy] = 0;
    Q.push_front(Node(sx, sy));
    while (!Q.empty())
    {
        Node u = Q.front();
        Q.pop_front();
        for (int i = 0; i < 8; i++)
        {
            int xx = u.x + dx[i], yy = u.y + dy[i];
            int flag = 0;
            if (MAP[u.x][u.y]-'0' != i)
                flag = 1;
            if (xx < 0 || xx >= n || yy < 0 || yy >= m)
                continue;
            if (d[xx][yy] > d[u.x][u.y] + flag)
            {
                d[xx][yy] = d[u.x][u.y] + flag;
                flag ? Q.push_back(Node(xx, yy)) : Q.push_front(Node(xx, yy));
            }
        }
    }
    return d[ex][ey];
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%s", MAP[i]);
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int sx, sy, ex, ey;
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        sx--, sy--, ex--, ey--;
        int ans = BFS(sx, sy, ex, ey);
        printf("%d\n", ans);
    }
    return 0;
}
/*
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
*/

 

优先队列:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
const int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int n, m, d[MAXN][MAXN];
char MAP[MAXN][MAXN];
struct Node
{
    int x, y, step;
    Node(int x, int y, int step) : x(x), y(y), step(step) {}
    bool operator < (const Node& that) const
    {
        return that.step < step;
    }
};
int BFS(int sx, int sy, int ex, int ey)
{
    memset(d, INF, sizeof(d));
    priority_queue<Node> Q;
    d[sx][sy] = 0;
    Q.push(Node(sx, sy, 0));
    while (!Q.empty())
    {
        Node u = Q.top();
        Q.pop();
        for (int i = 0; i < 8; i++)
        {
            int xx = u.x + dx[i], yy = u.y + dy[i], step = u.step;
            if (xx < 0 || xx >= n || yy < 0 || yy >= m)
                continue;
            int flag = 0;
            if (MAP[u.x][u.y]-'0' != i)
                flag = 1, step++;
            if (d[xx][yy] > d[u.x][u.y] + flag)
            {
                d[xx][yy] = d[u.x][u.y] + flag;
                Q.push(Node(xx, yy, step));
            }
        }
    }
    return d[ex][ey];
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%s", MAP[i]);
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int sx, sy, ex, ey;
        scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
        sx--, sy--, ex--, ey--;
        int ans = BFS(sx, sy, ex, ey);
        printf("%d\n", ans);
    }
    return 0;
}
/*
5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值