信息学奥赛一本通 1450 Knight Moves

【题目描述】

原题来自:POJ 1915

编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数。骑士一步可以移动到的位置由下图给出。

【输入】

第一行给出骑士的数量 nn。

在接下来的 3n3n 行中,每 33 行描述了一个骑士。其中,

第一行一个整数 LL 表示棋盘的大小,整个棋盘大小为 L×LL×L;

第二行和第三行分别包含一对整数 (x,yx,y),表示骑士的起始点和终点。假设对于每一个骑士,起始点和终点均合理。

【输出】

对每一个骑士,输出一行一个整数表示需要移动的最小步数。如果起始点和终点相同,则输出 00。

【输入样例】

3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1

【输出样例】

5
28
0

【提示】

对于 100% 的数据,有 4≤L≤3004≤L≤300,保证 0≤x,y≤L−10≤x,y≤L−1。

代码如下

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
const int N = 1000 + 5;
const int dnewStr[] = { 0,0,-1,1,1,-1,1,1 };
const int dy[] = { 1,-1,0,0,-1,1,-1,1 };
using namespace std;
struct Node {
    int x, y;
    Node() {}
    Node(int x, int y) : x(x), y(y) {}
};
int n;
int vis[N][N];
int dis[N][N];
int dir[8][2] = { {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2} };
void BFS(Node st, Node ed) {
    if (st.x == ed.x && st.y == ed.y) {
        printf("0\n");
        return;
    }

    memset(dis, 0, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    queue<Node> posQ; //正向队列
    queue<Node> negQ; //反向队列

    posQ.push(st);
    negQ.push(ed);
    vis[st.x][st.y] = 1;
    vis[ed.x][ed.y] = 2;
    while (!posQ.empty() || !negQ.empty()) {
        if (!posQ.empty()) {
            Node now = posQ.front();
            posQ.pop();
            int x = now.x;
            int y = now.y;

            for (int i = 0; i < 8; i++) {
                int nx = x + dir[i][0];
                int ny = y + dir[i][1];
                if (nx < 0 || ny < 0 || nx >= n || ny >= n)
                    continue;
                if (vis[nx][ny] == 2) { //相遇
                    int step = dis[x][y] + dis[nx][ny] + 1;
                    printf("%d\n", step);
                    return;
                }
                if (vis[nx][ny] == 0) {
                    vis[nx][ny] = 1;
                    dis[nx][ny] = dis[x][y] + 1;
                    posQ.push(Node(nx, ny));
                }
            }
        }
        if (!negQ.empty()) {
            Node now = negQ.front();
            int x = now.x;
            int y = now.y;

            for (int i = 0; i < 8; i++) {
                int nx = now.x + dir[i][0];
                int ny = now.y + dir[i][1];
                if (nx < 0 || ny < 0 || nx >= n || ny >= n)
                    continue;
                if (vis[nx][ny] == 1) { //相遇
                    int step = dis[nx][ny] + dis[x][y] + 1;
                    printf("%d\n", step);
                    return;
                }
                if (vis[nx][ny] == 0) {
                    vis[nx][ny] = 2;
                    dis[nx][ny] = dis[x][y] + 1;
                    negQ.push(Node(nx, ny));
                }
            }
        }
    }
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        Node st, ed;
        scanf("%d%d%d%d", &st.x, &st.y, &ed.x, &ed.y);
        BFS(st, ed);
    }
    return 0;
}

 怎么样,这段代码是不是很简单啊,大家都看懂了么 ,喜欢博主的小伙伴们记得一键3连啊,我们下次见哦

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值