zoukankan      html  css  js  c++  java
  • HDU1372:Knight Moves(BFS)

    Knight Moves

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 21   Accepted Submission(s) : 17

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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

    #include <iostream>
    #include<deque>
    #include<cstring>
    #include<cstdio>
    
    using namespace std;
    struct node
    {
        int x,y,num;
    };
    deque<node> s;
    int dr[8][2]={{2,1},{2,-1},{1,2},{1,-2},{-2,-1},{-2,1},{-1,-2},{-1,2} };
    int vis[10][10];
    int ans,i,sx,sy,tx,ty;
    char ch1[4],ch2[4];
    void bfs()
    {
        node p;
        p.x=sx;
        p.y=sy;
        p.num=0;
        s.clear();
        vis[sx][sy]=1;
        s.push_back(p);
        while(!s.empty())
        {
            node q=s.front();
            for(int i=0;i<8;i++)
            {
                int xx=q.x+dr[i][0];
                int yy=q.y+dr[i][1];
                if(xx>0 && xx<=8 && yy>0 && yy<=8 && !vis[xx][yy])
                {
                    p.x=xx;
                    p.y=yy;
                    p.num=q.num+1;
                    s.push_back(p);
                    vis[xx][yy]=1;
                    if (xx==tx && yy==ty) {ans=p.num; return;}
                }
            }
            s.pop_front();
        }
        return;
    }
    int main()
    {
        while(~scanf("%s %s",&ch1,&ch2))
        {
           ans=0;
           sx=ch1[0]-'a'+1;
           sy=ch1[1]-'0';
           tx=ch2[0]-'a'+1;
           ty=ch2[1]-'0';
           memset(vis,0,sizeof(vis));
           if (sx!=tx || sy!=ty) bfs();
           printf("To get from %s to %s takes %d knight moves.\n",ch1,ch2,ans);
        }
        return 0;
    }
  • 相关阅读:
    Jmeter如何保持cookie,让所有请求都能用同一个cookie,免去提取JSESSIONID
    Jmeter如何提取响应头部的JSESSIONID
    Loadrunner如何进行有效的IP欺骗
    Center 6.5 redis 3.0安装
    小程序 wx.getRecorderManager 录音 to 语音识别
    微信小程序语音识别服务搭建全过程解析(https api开放,支持新接口mp3录音、老接口silk录音)
    java自然语言理解demo,源码分享(基于欧拉蜜)
    微信小程序——智能小秘“遥知之”源码分享(语义理解基于olami)
    bash, sh, dash 傻傻分不清楚
    微信小程序语音识别服务搭建全过程解析(项目开源在github)
  • 原文地址:https://www.cnblogs.com/stepping/p/5667490.html
Copyright © 2011-2022 走看看