H - Knight Moves DFS

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 of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy. 
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

InputThe input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard. 
OutputFor each test case, print one line saying "To get from xx to yy takes n knight moves.". 
Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

思路 :骑士每次移动只能移动一个方格 其实就是一个日字(具体原因我也不知道)8个方向,,直接用BFS查找就可以了

#include<iostream>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
struct stu{
    int a,b;
};
char a,b;
int aa,bb;
int arr[10][10];
int arr1[10][10];
int arr2[10][10];
int ar[8][2]={{-2,-1},{-2,1},{2,1},{2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
int bfs(int x1,int y1,int x2,int y2){
    memset(arr1,0,sizeof(arr1));
    queue<stu>que;
    que.push({x1,y1});
    arr1[x1][y1]=1;
    arr2[x1][y1]=0;
    while(que.size()){
        int x=que.front().a;
        int y=que.front().b;
        que.pop();
        int dx,dy;
        for(int i=0;i<8;i++)
        {
            dx=x+ar[i][0];
            dy=y+ar[i][1];
            if(dx>=0&&dx<8&&dy>=0&&dy<8&&arr1[dx][dy]!=1){
                arr1[dx][dy]=1;
                arr2[dx][dy]=arr2[x][y]+1;
                que.push({dx,dy});
                if(dx==x2&&dy==y2){
                    return arr2[dx][dy];
                }
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%c%d %c%d",&a,&aa,&b,&bb)){
        getchar();
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                arr[i][j]=1;
            }
        }
        int x1=a-‘a‘;
//        cout<<x1<<endl;
        int y1=aa-1;
//        cout<<y1<<endl;
        int x2=b-‘a‘;
//        cout<<x2<<endl;
        int y2=bb-1;
//        cout<<y2<<endl;
        arr[x1][aa-1]=0;
        arr[x2][bb-1]=0;
        if(x1==x2&&y1==y2)
            printf("To get from %c%d to %c%d takes 0 knight moves.\n",a,aa,b,bb);
        else {
            int y=bfs(x1,y1,x2,y2);
            printf("To get from %c%d to %c%d takes %d knight moves.\n",a,aa,b,bb,y);
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/Accepting/p/11241602.html

时间: 2024-11-03 00:01:49

H - Knight Moves DFS的相关文章

UVA 439 Knight Moves 走象棋 (DFS or BFS)

[题目链接]click here~~ [题目大意]类型于中国象棋里面"马"的走法,给你两个坐标,一个初始坐标,一个最终坐标,在保证有解的情况下最小的步数 [思路]BFS的话,直接模拟,因为棋盘比较小 (1)BFS +队列 代码:(3ms) #include <bits/stdc++.h> using namespace std; int dir8[8][2]= {{1,2},{2,1},{-1,2},{-2,1},{1,-2},{2,-1},{-1,-2},{-2,-1}}

【UVa】439 Knight Moves(dfs)

题目 题目 ? ? 分析 没有估价函数的IDA...... ? ? 代码 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int q,dx[10]={2,2,-2,-2,1,-1,1,-1},dy[10]={1,-1,1,-1,2,2,-2,-2},ans=1<<15; bool vis[11][11]; int x1,y1,x2,y2; bool

POJ 1915 Knight Moves [BFS]

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

UVa439 Knight Moves (BFS求最短路)

链接:http://acm.hust.edu.cn/vjudge/problem/19436分析:BFS跑一次最短路,状态转移有8个. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 using namespace std; 5 6 struct Point { 7 char r, c; 8 Point(char r = ' ', char c = ' '): r(r), c(c) {}; 9

poj 2243 Knight Moves

Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11794   Accepted: 6646 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

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

宽搜--knight moves hdu1372

写出来之后觉得是一道简单的宽搜题,但是之前写的时候遇到很多细节处理的小问题.刚刚开始,还需要更加努力. 首先是题目意思,骑士移动原来是走日字,智商捉急. 其次是方向处理问题,之前一直没转过弯.普遍方向表达有两种 第一种: int h[8][2]={{-2,-1},{-2,1},{-1,2},{-1,-2},{1,-2},{1,2},{2,-1},{2,1}}; for(int i=0;i<8;i++){ next.a=head.a+h[i][0]; next.b=head.b+h[i][1];

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应用)

题意  求国际象棋中骑士从一个位置移东到另一个位置所需最少步数 基础的BFS应用 #include <bits/stdc++.h> using namespace std; int x[] = { -2, -1, -2, -1, 1, 2, 1, 2}; int y[] = { -1, -2, 1, 2, -2, -1, 2, 1}; int d[15][15], sx, sy, ex, ey; pair<int, int> q[105], t; int bfs() { int c