zoukankan      html  css  js  c++  java
  • 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 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.
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    using namespace std;
    int mv[8][2] = {{-2,-1},{-1,-2},{-2,1},{-1,2},{1,-2},{2,-1},{1,2},{2,1}};
    int v[9][9],map[9][9];
    char a[3],b[3];
    struct node
    {
        int x,y,ans;
    }q[1000001];
    void bfs(int x,int y)
    {
        int e=0;
        int s=0;
        memset(v,0,sizeof(v));
        struct node t,f;
        t.x=x;
        t.y=y;
        t.ans=0;
        v[t.x][t.y]=1;
        q[e++]=t;
        while(s<e)
        {
            t=q[s++];
            if(map[t.x][t.y]==1)
            {
                printf("To get from %s to %s takes %d knight moves.
    ",a,b,t.ans);
            }
            for(int i=0;i<8;i++)
            {
                f.x=t.x+mv[i][0];
                f.y=t.y+mv[i][1];
                f.ans=t.ans+1;
                if(f.x>=0&&f.x<8&&f.y>=0&&f.y<8&&v[f.x][f.y]==0)
                {
                    q[e++]=f;
                    v[f.x][f.y]=1;
                }
            }
        }
    
    }
    int main()
    {
        while(scanf("%s%s",a,b)!=EOF)
        {
            memset(map,0,sizeof(map));
            map[b[1]-'0'-1][b[0]-'a']=1;//因为a-'0'从0开始,所以a[1]-'0'-1;
            bfs(a[1]-'0'-1,a[0]-'a');
    
        }
        return 0;
    }
  • 相关阅读:
    微服务架构有哪些优势?
    Java 线程数过多会造成什么异常?
    Java 死锁以及如何避免?
    抽象的(abstract)方法是否可同时是静态的(static), 是否可同时是本地方法(native),是否可同时被 synchronized 修饰?
    内部类可以引用它的包含类(外部类)的成员吗?有没有 什么限制?
    CSS选取第几个标签元素:nth-child、first-child、last-child
    数据库约束
    DQL查询语句
    网络编程(客户端,服务端文件上传下载)
    缓冲流,转换流
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/3917404.html
Copyright © 2011-2022 走看看