A - Ocean Currents UVA - 11573 -0-1 BFS

  • A - Ocean Currents

  •  UVA - 11573 
  • 题意:给定一个海面,数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量。
  • 每次询问给一个起点一个终点,问起点到终点耗费的最小能量
  • 思路:广搜,队列用优先队列,每次取能量最低的点出来进行状态的转移(或者用deque 01 bfs的思路)
  • 注意这里 不是路径最短所以不能根据bfs顺序去标记,无需vis数组,直接根据时候更新最小能量判断是否入队
  • #include<bits/stdc++.h>
    using namespace std;
    #define inf 0x3f3f3f3f
    #define maxn 1523
    char mmp[maxn][maxn];
    int n,rs,cs,rd,cd,r,c;
    int dis[maxn][maxn];
    struct node
    {
        int x,y,w;
        bool operator<(const node&b)const
        {
            return w>b.w;
        }
    } top,temp;
    int to[9][2]= {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
    bool judge(int x,int y)
    {
        if(x<0||y<0||x>=r||y>=c)return false;
        return true;
    }
    void bfs(int x,int y)
    {
        memset(dis,inf,sizeof(dis));
        priority_queue<node>q;
        dis[x][y]=0;
        q.push(node{x,y,0});
        while(!q.empty())
        {
            top=q.top();
            q.pop();
            if(top.x==rd&&top.y==cd)break;
            for(int i=0; i<8; i++)
            {
                temp=top;
                temp.x+=to[i][0];
                temp.y+=to[i][1];
                if(judge(temp.x,temp.y))
                {
                    if(mmp[top.x][top.y]-'0'!=i)
                        temp.w++;
                    if(dis[temp.x][temp.y]<=temp.w)continue;
                    dis[temp.x][temp.y]=temp.w;
                    q.push(temp);
                }
            }
        }
        return;
    }
    int main()
    {
        while(scanf("%d%d",&r,&c)==2)
        {
            for(int i=0; i<r; i++)
                scanf("%s",mmp[i]);
            scanf("%d",&n);
            while(n--)
            {
                scanf("%d%d%d%d",&rs,&cs,&rd,&cd);
                rs--,cs--,rd--,cd--;
                bfs(rs,cs);
                printf("%d\n",dis[rd][cd]);
            }
        }
        return 0;
    }

    01BFS时间更优:

  • #include<bits/stdc++.h>
    using namespace std;
    #define inf 0x3f3f3f3f
    #define maxn 1523
    char mmp[maxn][maxn];
    int n,rs,cs,rd,cd,r,c;
    int dis[maxn][maxn];
    struct node
    {
        int x,y,w;
    } top,temp;
    int to[9][2]= {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
    bool judge(int x,int y)
    {
        if(x<0||y<0||x>=r||y>=c)return false;
        return true;
    }
    void bfs(int x,int y)
    {
        memset(dis,inf,sizeof(dis));
        deque<node>q;
        dis[x][y]=0;
        q.push_front(node{x,y,0});
        while(!q.empty())
        {
            top=q.front();
            q.pop_front();
            if(top.x==rd&&top.y==cd)break;
            for(int i=0; i<8; i++)
            {
                temp=top;
                temp.x+=to[i][0];
                temp.y+=to[i][1];
                if(judge(temp.x,temp.y))
                {
                    if(mmp[top.x][top.y]-'0'!=i)
                        temp.w++;
                    if(dis[temp.x][temp.y]<=temp.w)continue;
                    dis[temp.x][temp.y]=temp.w;
                    if(temp.w>top.w)
                        q.push_back(temp);
                    else
                        q.push_front(temp);
                }
            }
        }
        return;
    }
    int main()
    {
        while(scanf("%d%d",&r,&c)==2)
        {
            for(int i=0; i<r; i++)
                scanf("%s",mmp[i]);
            scanf("%d",&n);
            while(n--)
            {
                scanf("%d%d%d%d",&rs,&cs,&rd,&cd);
                rs--,cs--,rd--,cd--;
                bfs(rs,cs);
                printf("%d\n",dis[rd][cd]);
            }
        }
        return 0;
    }
    

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/84279849