hdu 1372 || poj 2243 Knight Moves(搜索:BFS+优先队列水题)

本文讨论了一种使用队列而非优先队列解决象棋马走日问题的方法,通过分析输入读取方式对性能的影响,提出了优化策略,并提供了具体的代码实现。文章深入探讨了算法效率和优化技巧,旨在为读者提供解决类似问题的思路。

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

给出两个坐标,问你按象棋马走日的方式最少需要跳几次

因为涉及到最少所以我就想到了优先队列

这个题很水,但是读入很奇葩!

在杭电上我用cin string和scanf char[]就会超时

但是用cin char[]就不超时了...


刚刚想了下这道题好像没有必要使用优先队列,因为每次遍历的都是当前步数最少的状态

没有别的条件限制,所以直接用队列即可,把优先队列换成队列快了600ms...


代码如下:

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 1010
#define LL long long
using namespace std;

bool vis[10][10][MAXN];
int dir[8][2] = {{-2,-1}, {-2,1}, {-1,-2}, {-1,2}, 
                {1,-2}, {1,2}, {2,-1}, {2,1}};

struct Node {
    int x, y, times;
    bool operator < (const Node &rhs) const {
        return times > rhs.times;
    }
}start, end;

queue<Node> q;

void judgequeue(int x, int y, int times) {
    if(x>=1 && x<=8 && y>=1 && y<=8 && !vis[x][y][times]) {
        Node tmp;
        tmp.x = x;
        tmp.y = y;
        tmp.times = times;
        q.push(tmp);
    }
}

int BFS() {
    memset(vis, 0, sizeof(vis));
    while(!q.empty())
        q.pop();
    start.times = 0;
    q.push(start);

    while(!q.empty()) {
        Node tmp = q.front();
        vis[tmp.x][tmp.y][tmp.times] = true;
        q.pop();
        if(tmp.x==end.x && tmp.y==end.y)
            return tmp.times;
        
        for(int i=0; i<8; ++i)
            judgequeue(tmp.x+dir[i][0], tmp.y+dir[i][1], tmp.times+1);

    }
    return 0;
}

int main(void) {
    int ans;
    char str1[5], str2[5];
    while(cin >> str1 >> str2) {
        memset(vis, 0, sizeof(vis));
        start.x = str1[0]-'a';
        start.y = str1[1]-'0';
        start.x++;
        end.x = str2[0]-'a';
        end.y = str2[1]-'0';
        end.x++;
        ans = BFS();
        printf("To get from %s to %s takes %d knight moves.\n", str1, str2, ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值