POJ 3170 Knights of Ni (双向BFS打表记录)

23 篇文章 0 订阅
Knights of Ni
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2032 Accepted: 872

Description

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

大体题意:
你的角色从2出发,到3终止,但你要拿到一个4 给3,其中1不能走,0是可以走的路,问最短路的长度是多少?
思路:
可以两次BFS,  一次从起点2开始,记录每一个点到起点的最短距离, 
第二次BFS从终点3开始,记录每一个点到终点的最短距离,最后遍历4的位置,取个最小距离之和即可!
详细见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<queue>
using namespace std;
const int maxn = 1000 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
typedef long long ll;
int w,h;
struct Node{
    int x,y;
    Node():x(0),y(0){}
    Node(int x,int y):x(x),y(y){}
};
int dist[2][maxn][maxn];
int a[maxn][maxn];
int sx , sy,ex,ey;
vector<Node>gm;
queue<Node>q;
const int dx[] = {1,-1,0,0};
const int dy[] = {0,0,-1,1};
int vis[maxn][maxn];
bool init(int x,int y){
    return x >= 1 && x <= h && y >= 1 && y <= w;
}
void bfs(int x,int y,int t){
    memset(vis,0,sizeof vis);
    vis[x][y] = 1;
    while(!q.empty())q.pop();
    q.push(Node(x,y));

    dist[t][x][y] = 0;
    while(!q.empty()){
        Node u = q.front();
        q.pop();
        for (int i = 0; i < 4; ++i){
            int xx = u.x + dx[i];
            int yy = u.y + dy[i];
            if (!init(xx,yy))continue;
            if (vis[xx][yy] || a[xx][yy] == 1)continue;
            if (dist[t][xx][yy] > dist[t][u.x][u.y] + 1){
                dist[t][xx][yy] = dist[t][u.x][u.y] + 1;
                if (!vis[xx][yy]){
                    vis[xx][yy] = 1;
                    q.push(Node(xx,yy));
                }
            }
        }


    }



}
int main(){
    scanf("%d %d",&w,&h);
    for (int i = 1; i<=h; ++i){
        for (int j =1 ; j<= w; ++j){
            dist[0][i][j] = dist[1][i][j] = inf;
        }
    }
    for (int i = 1; i <= h; ++i){
        for (int j = 1; j <= w; ++j){
            scanf("%d",&a[i][j]);
            if (a[i][j] == 2)sx = i,sy = j;
            if (a[i][j] == 3)ex = i,ey = j;
            if (a[i][j] == 4)gm.push_back(Node(i,j));
        }
    }

    bfs(sx,sy,0);
    bfs(ex,ey,1);
    int len = gm.size();
    int ans=  inf;
    for (int i = 0; i < len; ++i){
        Node u = gm[i];
        ans=  min(ans,dist[0][u.x][u.y] + dist[1][u.x][u.y]);
    }
    printf("%d\n",ans);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值