POJ 1915 Knight Moves bfs || 双向bfs

题目:

http://poj.org/problem?id=1915

题意:

给定一个 nn 的棋盘,给定起点和终点,按照题目给定的跳跃方式,求起点到终点的最小步数

思路:

水题一道,直接 bfs 即可,学习双向 bfs 的练习题。所谓双向 bfs ,就是起点和终点开始进行 bfs ,直到两者在搜索过程中相遇,这样可以减少一些搜索量,值得一提的是,如果起点终点相同的话,双向 bfs 是搜索不到的,需要特判一下。就本题而言,两者效率差不多。。。
bfs:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> pii;
const int N = 500 + 10, INF = 0x3f3f3f3f;

int n;
int dis[N][N];
int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};

int bfs(int sx, int sy, int tx, int ty)
{
    queue<pii> que;
    memset(dis, 0x3f, sizeof dis);
    que.push(pii(sx, sy)), dis[sx][sy] = 0;
    while(! que.empty())
    {
        pii p = que.front(); que.pop();
        if(p.first == tx && p.second == ty) return dis[tx][ty];
        for(int i = 0; i < 8; i++)
        {
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if(nx >= 0 && nx < n && ny >= 0 && ny < n && dis[nx][ny] > dis[p.first][p.second] + 1)
            {
                dis[nx][ny] = dis[p.first][p.second] + 1;
                que.push(pii(nx, ny));
            }
        }
    }
    return 0;//这句没用
}
int main()
{
    int t, sx, sy, tx, ty;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d%d", &n, &sx, &sy, &tx, &ty);
        printf("%d\n", bfs(sx, sy, tx, ty));
    }
    return 0;
}

bfs:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> pii;
const int N = 500 + 10, INF = 0x3f3f3f3f;

int n;
int dis[N][N], vis[N][N];
int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}, dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};

int bfs(int sx, int sy, int tx, int ty)
{
    queue<pii> que;
    memset(dis, 0, sizeof dis);
    memset(vis, 0, sizeof vis);
    if(sx == tx && sy == ty) return 0;//特判起点终点相同的情况
    que.push(pii(sx, sy)), dis[sx][sy] = 0, vis[sx][sy] = 1;//1标记是从起点正向搜索访问到的点
    que.push(pii(tx, ty)), dis[tx][ty] = 0 ,vis[tx][ty] = 2;//2标记是从终点反向搜索访问到的点
    while(! que.empty())
    {
        pii p = que.front(); que.pop();
        for(int i = 0; i < 8; i++)
        {
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if(nx >= 0 && nx < n && ny >= 0 && ny < n)
            {
                if(! vis[nx][ny])//没访问过,正常访问
                {
                    vis[nx][ny] = vis[p.first][p.second];//标记此点是正向还是反向搜索到的点
                    dis[nx][ny] = dis[p.first][p.second] + 1;
                    que.push(pii(nx, ny));
                }
                else if(vis[p.first][p.second] != vis[nx][ny])//相邻两点都被访问且一个正向一个反向,说明一条从起点到终点的最短路径已经出现
                    return dis[p.first][p.second] + dis[nx][ny] + 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t, sx, sy, tx, ty;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d%d", &n, &sx, &sy, &tx, &ty);
        printf("%d\n", bfs(sx, sy, tx, ty));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值