POJ 2243 Knight Moves bfs || 双向bfs

题目:

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

题意:

给定一个 88 的棋盘,编号为 18ah ,按照给定的走法, 求从起点到终点需要的最少步数

思路:

直接 bfs 或者 bfs 都可以,在这题中, bfs 终于展现了比 bfs 高的多的效率
bfs:

//700ms
#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 >= 1 && nx <= n && ny >= 1 && 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 -1;
}
int main()
{
    int t, sx, sy, tx, ty;
    char ch1, ch2;
    n = 8;
    while(~ scanf(" %c%d %c%d", &ch1, &sy, &ch2, &ty))
    {
        sx = ch1 - 'a' + 1, tx = ch2 - 'a' + 1;
        printf("To get from %c%d to %c%d takes %d knight moves.\n", ch1, sy, ch2, ty, bfs(sx, sy, tx, ty));
    }
    return 0;
}

bfs:

//100ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> pii;
const int N = 10 + 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;
    que.push(pii(tx, ty)), dis[tx][ty] = 0 ,vis[tx][ty] = 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 >= 1 && nx <= n && ny >= 1 && 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;
    char ch1, ch2;
    n = 8;
    while(~ scanf(" %c%d %c%d", &ch1, &sy, &ch2, &ty))
    {
        sx = ch1 - 'a' + 1, tx = ch2 - 'a' + 1;
        printf("To get from %c%d to %c%d takes %d knight moves.\n", ch1, sy, ch2, ty, bfs(sx, sy, tx, ty));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值