Knight Moves (双向bfs)

# 10028. 「一本通 1.4 例 3」Knight Moves

【题目描述】

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

【算法】

双向bfs。

【代码】

#include <bits/stdc++.h>
#define P pair<int,int>
#define ff first
#define ss second
using namespace std;
int T,ans;
int d1[310][310],d2[310][310];
const int dx[8]={-2,-1,1,2,2,1,-1,-2},dy[8]={1,2,2,1,-1,-2,-2,-1};
int main() {
    scanf("%d",&T);
    while(T--) {
        ans=2e9;
        int L; scanf("%d",&L);
        P x; scanf("%d%d",&x.ff,&x.ss); x.ff++,x.ss++;
        P y; scanf("%d%d",&y.ff,&y.ss); y.ff++,y.ss++;
        for(int i=1;i<=L;i++)
            for(int j=1;j<=L;j++)
                d1[i][j]=d2[i][j]=-1;
        queue<P> q1,q2;
        q1.push(x),d1[x.ff][x.ss]=0;
        q2.push(y),d2[y.ff][y.ss]=0;
        while(q1.size()&&q2.size()) {
            P now1=q1.front(); q1.pop();
            P now2=q2.front(); q2.pop();
            if(d2[now1.ff][now1.ss]!=-1) {
                ans=min(ans,d1[now1.ff][now1.ss]+d2[now1.ff][now1.ss]);
                break;
            }
            if(d1[now2.ff][now2.ss]!=-1) {
                ans=min(ans,d1[now2.ff][now2.ss]+d2[now2.ff][now2.ss]);
                break;
            }
            for(int i=0;i<8;i++) {
                int nx=now1.ff+dx[i],ny=now1.ss+dy[i];
                if(nx>=1&&nx<=L&&ny>=1&&ny<=L&&d1[nx][ny]==-1) {
                    d1[nx][ny]=d1[now1.ff][now1.ss]+1;
                    q1.push(make_pair(nx,ny));
                }
            }
            for(int i=0;i<8;i++) {
                int nx=now2.ff+dx[i],ny=now2.ss+dy[i];
                if(nx>=1&&nx<=L&&ny>=1&&ny<=L&&d2[nx][ny]==-1) {
                    d2[nx][ny]=d2[now2.ff][now2.ss]+1;
                    q2.push(make_pair(nx,ny));
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Willendless/p/9581576.html

时间: 2024-10-08 09:29:53

Knight Moves (双向bfs)的相关文章

Knight Moves(hdu1372 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6731    Accepted Submission(s): 4059 Problem Description A friend of you is doing res

poj2243&amp;&amp;hdu1372 Knight Moves(BFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y

poj 1915 Knight Moves (bfs搜索)

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21919   Accepted: 10223 Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast

Sicily Knight Moves(BFS)

1000. Knight Moves                       Time Limit: 1sec    Memory Limit:32MB Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square

HDOJ 1372 Knight Moves【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7482    Accepted Submission(s): 4481 Problem Description A friend of you is doin

HDU 1372 Knight Moves (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10372    Accepted Submission(s): 6105 Problem Description A friend of you is doin

HDU1372:Knight Moves(经典BFS题)

HDU1372:Knight Moves(BFS) Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that v

UVA - 439 - Knight Moves (BFS)

UVA - 439 Knight Moves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knigh

Knight Moves(BFS,走’日‘字)

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8831    Accepted Submission(s): 5202 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) wher

HDOJ 题目1372 Knight Moves(BFS)

Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7551    Accepted Submission(s): 4513 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe