zoukankan      html  css  js  c++  java
  • HDOJ 1372 Knight Moves

    Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3340    Accepted Submission(s): 2087


    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.
     1 /* 功能Function Description:     hdoj-1372  用STL队列实现BFS
     2    开发环境Environment:          DEV C++ 4.9.9.1
     3    技术特点Technique:
     4    版本Version:
     5    作者Author:                   可笑痴狂
     6    日期Date:                     20120725
     7    备注Notes:        哈哈!! 第一篇用深搜队列
     8 */
     9 
    10 #include<iostream>
    11 #include<cstring>
    12 #include<string>
    13 #include<queue>
    14 using namespace std;
    15 
    16 struct point 
    17 {
    18     int x;
    19     int y;
    20     int step;
    21 };
    22 
    23 int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};
    24 
    25 int BFS(string st,string end)
    26 {
    27     bool visit[10][10];
    28     memset(visit,false,sizeof(visit));
    29     point s,e,t;
    30     queue<point> q;
    31     s.x=st[0]-'a';      //将其转化为二维坐标表示
    32     s.y=st[1]-'0';
    33     s.step=0;
    34     e.x=end[0]-'a';
    35     e.y=end[1]-'0';
    36     if(s.x==e.x&&s.y==e.y)
    37         return 0;
    38     q.push(s);
    39     visit[s.x][s.y]=true;
    40     while(!q.empty())
    41     {
    42         s=q.front();
    43         q.pop();
    44         for(int i=0;i<8;++i)
    45         {
    46             t.x=s.x+dir[i][0];
    47             t.y=s.y+dir[i][1];
    48             t.step=s.step+1;
    49             if(visit[t.x][t.y]==false)
    50             {
    51                 if(t.x==e.x&&t.y==e.y)
    52                     return t.step;
    53                 if(t.x>=0&&t.x<8&&t.y>0&&t.y<=8)
    54                 {
    55                     visit[t.x][t.y]=true;
    56                     q.push(t);
    57                 }
    58             }
    59         }
    60     }
    61     //return 0;
    62 }
    63 
    64 int main()
    65 {
    66     string st,end;
    67     while(cin>>st>>end)
    68     {
    69         cout<<"To get from "<<st<<" to "<<end<<" takes "<<BFS(st,end)<<" knight moves."<<endl;
    70     }
    71     return 0;
    72 }
    功不成,身已退
  • 相关阅读:
    table问题
    生成跟相应qq聊天
    查数组 indexOf()用法
    Ajax请求数据的两种方式
    Ajax面试题
    内部类及静态内部类的实例化
    为什么在开发中大部分的时候都在用session而Application基本上都不去使用?
    Java序列化与反序列化
    Java反射机制
    Java中线程同步的方法
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2607994.html
Copyright © 2011-2022 走看看