zoukankan      html  css  js  c++  java
  • Knight Moves

    Knight Moves

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 581 Accepted Submission(s): 421
     
    Problem 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 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.
     
    Input
    The 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.

     
    Output

                For 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.
     
     
    Source
    University of Ulm Local Contest 1996
     
    Recommend
    Eddy
    #include<bits/stdc++.h>
    #define N 10
    using namespace std;
    int dir[8][2]={
        {1,2},
        {2,1},
        {-1,2},
        {-2,1},
        {1,-2},
        {2,-1},
        {-1,-2},
        {-2,-1}
    };
    char beg[3],en[3];
    bool vis[N][N];
    int X1,X2,Y1,Y2;
    struct node
    {
        int x,y,step;
        node(){}
        node(int X,int Y,int S)
        {
            x=X;
            y=Y;
            step=S;
        }
    };
    bool ok(int x,int y)
    {
        if(x<1||x>8||y<1||y>8||vis[x][y])
            return false;
        return true;
    }
    int bfs(int x,int y)
    {
        queue<node>q;
        q.push(node(x,y,0));
        vis[x][y]=true;
        while(!q.empty())
        {
            node fr=q.front();
            q.pop();
            if(fr.x==X2&&fr.y==Y2)
                return fr.step;
            for(int i=0;i<8;i++)
            {
                int fx=fr.x+dir[i][0];
                int fy=fr.y+dir[i][1];
                if(ok(fx,fy))
                {
                    q.push(node(fx,fy,fr.step+1));
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
        while(scanf("%s%s",&beg,&en)!=EOF)
        {
            memset(vis,false,sizeof vis);
            X1=(int)(beg[0]-'a'+1);
            Y1=(int)(beg[1]-'0');
            X2=(int)(en[0]-'a'+1);
            Y2=(int)(en[1]-'0');
            printf("To get from %s to %s takes %d knight moves.
    ",beg,en,bfs(X1,Y1));
        }
        return 0;
    }
  • 相关阅读:
    一般表的一般方法
    asp.net采集函数(采集、分析、替换、入库一体)
    在 Access 里使用查询建立 存储过程/视图, 并使用 ASP 执行
    制作Access代码生成器 研发中【资料整理】
    Asp.net中打造通用数据访问类(c#)[转]
    Ajax.Net程序教程.彭彭编写
    C# 类中 属性和方法写在一个类里 和 属性和方法 分开的区别感受!!
    自己在vs2003下写的findLabel[应用程序]
    [转]Response.Write后css失效问题的解决
    javascript结构图
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6038393.html
Copyright © 2011-2022 走看看