HDU-2612 Find a way(双向bfs)

本文介绍了一种在一维图中寻找两人见面最短路径的算法。通过两次BFS遍历,分别从两个起点出发,记录到达每个点的步数,最终找到两人能同时到达且步数和最小的点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Find a way

题意:给你一维图,两个人Y,M,约在kfc(@)见面,求两个人走的步数和最小的走法
做法:分别从两个人的位置进行bfs,将两次的结果写入ans数组,最后遍历ans数组,找出两个人都能到且步数和最小的kfc。
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctype.h>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <limits.h>
using namespace std;
const int N = 200 + 10;
const int INF = 0x3f3f3f3f;

char a[N][N];
int vis[N][N];
int ans[2][N][N];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int r, c, x1, yy, x2, y2;
struct node{
    int x, y, step;
    node(int x, int y, int step):x(x),y(y),step(step){}
};

void result()
{
    int cnt = INF;
    // for (int i = 0; i < r; i++){
    //     for(int j = 0; j < c; j++){
    //         printf("%d ", ans[0][i][j]);
    //     }
    //     cout << endl;
    // }
    // cout << endl;
    // for (int i = 0; i < r; i++){
    //     for(int j = 0; j < c; j++){
    //         printf("%d ", ans[1][i][j]);
    //     }
    //     cout << endl;
    // }
    for (int i = 0; i < r; i++){
        for(int j = 0; j < c; j++){
            if(ans[0][i][j] && ans[1][i][j]){		//当两个人都能到达此处时,记录下来后面比较
                cnt = min(cnt, ans[0][i][j] + ans[1][i][j]);
            }
        }
    }
    printf("%d\n", cnt * 11);
}
bool f(int x, int y)
{
    if (x >=0 && x < r && y >= 0 && y < c){
        return true;
    }
    return false;
}
void bfs(int x, int y, int flag)
{
    memset(vis, 0, sizeof(vis));
    queue<node> s;
    s.push(node(x, y, 0));
    vis[x][y] = 1;
    while(!s.empty()){
        node cnt = s.front();
        s.pop();
        for (int i = 0; i < 4; i++){
            int tx = cnt.x + dir[i][0];
            int ty = cnt.y + dir[i][1];
            if (f(tx, ty) && !vis[tx][ty] && a[tx][ty] != '#'){  //注意:只要不是#的地方都能走,也就是说Y可以走M, @上。
                vis[tx][ty] = 1;
                s.push(node(tx, ty, cnt.step + 1));
                if (a[tx][ty] == '@')
                    ans[flag][tx][ty] = cnt.step + 1;
            }
        }
    }
}
int main()
{
    while(cin >> r >> c){
        memset(ans, 0, sizeof(ans));
        for (int i = 0; i < r; i++){
            for (int j = 0; j < c; j++){
                cin >> a[i][j];
                if (a[i][j] == 'Y'){
                    x1 = i;
                    yy = j;
                }
                if (a[i][j] == 'M'){
                    x2 = i;
                    y2 = j;
                }
            }
        }
        bfs(x1, yy, 0);		//分别进行bfs遍历
        bfs(x2, y2, 1);
        result();
    }
    return 0;
}
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值