两次bfs

Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible.

Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000).  

The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery.  

In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block.  

It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.
Input
Line 1: Two space-separated integers: W and H.  

Lines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row.  

The integers that describe the map come from this set:  
0: Square through which Bessie can travel  
1: Impassable square that Bessie cannot traverse  
2: Bessie's starting location  
3: Location of the Knights of Ni  
4: Location of a shrubbery
Output
Line 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.
Sample Input
8 4
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 4 1 1 1 0
Sample Output
11
Hint
Explanation of the sample:  

Width=8, height=4. Bessie starts on the third row, only a few squares away from the Knights.  

Bessie can move in this pattern to get a shrubbery for the Knights: N, W, N, S, E, E, N, E, E, S, S. She gets the shrubbery in the northwest corner and then makes her away around the barriers to the east
and then south to the Knights.

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<queue>
using namespace std;
int w,h,map[1010][1010],st[1010][1010],ed[1010][1010],sx,sy,ex,ey;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
#define inf 0x3f3f3f3f
/*struct node
{
    int x,y,ans;
};
int isbound(int x,int y)
{
    if(x>=1&&x<=h&&y>=1&&y<=w) return 1;
    return 0;
}
int bfs1(node b)
{
    queue<node>q1;
    node now1,next1;
    q1.push(b);
     while(!q1.empty())
     {
         now1=q1.front();
         q1.pop();
         if(map[now1.x][now1.y]==3)
            return now1.ans;
         for(int i=0;i<4;i++)
         {
            next1.x=now1.x+dx[i];
            next1.y=now1.y+dy[i];
            if(isbound(next1.x,next1.y)&&map[next1.x][next1.y]!=1)
            {
                next1.ans=now1.ans+1;
                q1.push(next1);
            }
         }
     }
}
int bfs(node a)
{
    queue<node>q;
    node now,next;
    q.push(a);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(map[now.x][now.y]==4)
            return bfs1(now);
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(isbound(next.x,next.y)&&map[next.x][next.y]!=1&&map[next.x][next.y]!=3)
            {
                next.ans=now.ans+1;
                //if(map[next.x][next.y]==4)
                //{ bfs1(next);q.clear();}
                q.push(next);
            }
        }
    }
}
int main()
{
    cin>>w>>h;
    node a;
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
        {
            //cin>>map[i][j];
            scanf("%d",&map[i][j]);
            if(map[i][j]==2)
            {
                a.x=i;a.y=j;a.ans=0;
            }
        }
    }
    cout<<bfs(a)<<endl;
}*/
//虽然写了两个bfs但是思路有点问题,我是求的2到4的最短距离,然后接着从4到3,虽然样例过了,但是并不能保正
//此时最小,应当求出2到所有点的最小距离,和3到所有点的最小距离然后相加求出一个最小的来,注意怎么求从某一点出发到所有点的最小距离,以前做的提都 是到一个定点的,到所有点的只需设一个数组来保存即可,数组下标表示从起点到该店的最小步数,初始化这个数组为最大,和上一次到达该点的路径比较
int isbound(int x,int y)
{
    if(x>=1&&x<=h&&y>=1&&y<=w) return 1;
    return 0;
}
struct node
{
    int x,y,stemp;
};
int min(int a,int b)
{
    if(a<b) return a;
    return b;
}
void bfs1(int x,int y)
{
    st[x][y]=0;
    node now,next;
    now.x=x;now.y=y;now.stemp=0;
    queue<node>q;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(isbound(next.x,next.y)&&map[next.x][next.y]!=1&&map[next.x][next.y]!=3&&now.stemp+1<st[next.x][next.y])
            {
                next.stemp=now.stemp+1;
                st[next.x][next.y]=next.stemp;
                q.push(next);
            }

        }
    }
}
void bfs2(int x,int y)
{
    ed[x][y]=0;
    node now,next;
    queue<node>q;
    now.x=x;now.y=y;now.stemp=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            if(isbound(next.x,next.y)&&map[next.x][next.y]!=1&&now.stemp+1<ed[next.x][next.y])
            {
                next.stemp=now.stemp+1;
                ed[next.x][next.y]=next.stemp;
                q.push(next);
            }
        }
    }
}
int main()
{
    cin>>w>>h;
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
        {
            scanf("%d",&map[i][j]);
            if(map[i][j]==2)
            {
                sx=i;sy=j;
            }
            if(map[i][j]==3)
            {
                ex=i;ey=j;
            }
            st[i][j]=ed[i][j]=inf;
        }
    }
    bfs1(sx,sy);
    bfs2(ex,ey);
    int minn=inf;
    for(int i=1;i<=h;i++)
    {
        for(int j=1;j<=w;j++)
        {
            if(map[i][j]==4)
            {
                minn=min(minn,st[i][j]+ed[i][j]);
            }
        }
    }
    cout<<minn<<endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值