1. 程式人生 > >2016SDAU程式設計練習二1015

2016SDAU程式設計練習二1015

Knight Moves 


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.<br>Of course you know that it is vice versa. So you offer him to write a program that solves the &quot;difficult&quot; part. <br><br>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. <br>
 


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. <br>
 


Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.". <br>
 


Sample Input
e2 e4<br>a1 b2<br>b2 c3<br>a1 h8<br>a1 h7<br>h8 a1<br>b1 c3<br>f6 f6<br> 


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


Source

University of Ulm Local Contest 1996

題意:國際象棋騎士,求a到b最短距離

思路:這個也說過,BFS,不越界就行

感想:。。。不想做了。。。

AC程式碼:
#include<iostream>
#include<string.h>
#include<queue>
#include<stdio.h>
using namespace std;
int vis[20][20];
string s1,s2;
struct ans
{
    int x;
    int y;
    int ste;
}n1,n2;
int dix[10]={-2,-2,-1,-1,1,1,2,2};
int diy[10]={-1,1,-2,2,-2,2,-1,1};
int ww(int a,int b)
{
    if(a>0&&a<=8&&b>0&&b<=8) return 1;
    return 0;
}
int BFS( )
{
    n1.ste=0;
    queue<ans> Q;
    Q.push(n1);
    while(!Q.empty())
    {
        n1=Q.front();
        Q.pop();
        if(n1.x==s2[1]-'0'&&n1.y==s2[0]-'a'+1) return n1.ste;
        for(int i=0;i<8;i++)
        {
            n2.x=n1.x+dix[i];
            n2.y=n1.y+diy[i];
            if(ww(n2.x,n2.y)&&!vis[n2.x][n2.y])
            {
                vis[n2.x][n2.y]=1;
                n2.ste=n1.ste+1;
                Q.push(n2);
            }
        }
    }
    return -1;
}


int main()
{
    //freopen("a.txt","r",stdin);
    while(cin>>s1>>s2)
    {
        int i,j;
        n1.x=s1[1]-'0';
        n1.y=s1[0]-'a'+1;
        for(i=0;i<20;i++)
            for(j=0;j<20;j++)
             vis[i][j]=0;
        vis[n1.x][n1.y]=1;
        int t=BFS();
            cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<t<<" knight moves."<<endl;
    }
    return 0;
}


 

相關推薦

2016SDAU程式設計練習1015

Knight Moves  Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the short

2016SDAU程式設計練習1002

Strange fuction  Problem Description Now, here is a fuction:<br>&nbsp;&nbsp;F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 &lt

2016SDAU程式設計練習1018

Tempter of the Bone  Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked

2016SDAU程式設計練習1024

Sequence one  Problem Description Search is important in the acm algorithm. When you want to solve a problem by using the search method,

2016SDAU程式設計練習1008

猜數字  A有1數m,B來猜.B每猜一次,A就說太大t太小或;對了問B猜n次可以猜到的最大數。   Input 第1行是整數T,表示有T組資料,下面有T行 <br>每行一個整數n (1 ≤ n ≤ 30) <br>   Output 猜n次可以猜到

2016SDAU程式設計練習1009

連連看  Problem Description “連連看”相信很多人都玩過。沒玩過也沒關係,下面我給大家介紹一下游戲規則:在一個棋盤中,放了很多的棋子。如果某兩個相同的棋子,可以通過一條線連起來(這條線不能經過其它棋子),而且線的轉折次數不超過兩次,那麼這兩個棋子就可以

2016SDAU程式設計練習1025

Sequence two  Problem Description Search is important in the acm algorithm. When you want to solve a problem by using the search method,

2016SDAU程式設計練習1013

A strange lift  Problem Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0

2016SDAU程式設計練習三1007

Problem G Problem Description都說天上不會掉餡餅,但有一天gameboy正走在回家的小徑上,忽然天上掉下大把大把的餡餅。說來gameboy的人品實在是太好了,這餡餅別處都不掉,就掉落在他身旁的10米範圍內。餡餅如果掉在了地上當然就不能吃了,所以

2016SDAU程式設計練習三1010

Problem J  Problem Description 有一樓梯共M級,剛開始時你在第一級,若每次只能跨上一級或二級,要走上第M級,共有多少種走法?   Input 輸入資料首先包含一個整數N,表示測試例項的個數,然後是N行資料,每行包含一個整數M(1<=M&l

2016SDAU程式設計練習三1012

Problem L  Problem Description 在2×n的一個長方形方格中,用一個1× 2的骨牌鋪滿方格,輸入n ,輸出鋪放方案的總數.<br>例如n=3時,為2× 3方格,骨牌的鋪放方案有三種,如下圖:<br><img src=

2016SDAU程式設計練習三1001

Problem A Problem Description   Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For

2016SDAU程式設計練習三1014

Problem N  Problem Description 我們看到過很多直線分割平面的題目,今天的這個題目稍微有些變化,我們要求的是n條折線分割平面的最大數目。比如,一條折線可以將平面分成兩部分,兩條折線最多可以將平面分成7部分,具體如下所示。<br><

2016SDAU程式設計練習三1017

Problem Q  Problem Description Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to col

C++Primer Plus筆記——第十章 類和動態記憶體分配課後程式設計練習答案

目錄 課後習題 習題1 習題2 習題3 習題4 習題5&6 課後習題 習題1 #include <iostream> #include <cstring> using namespace std; class Cow {

函式程式設計實驗:遞迴練習

module HW where {- 1. 定義求兩個非負整數最大公因子的函式: mygcd ::Integer ->Integer ->Integer -} mygcd ::Integer ->Integer ->Integer my

C primer plus 第六版 第六章 第十程式設計練習答案

#include<stdio.h> #define l 1.0 int main(void) { int i = 0; // Create for loop. i

C primer plus 第六版 第十章 第十程式設計練習答案

Github地址: φ(>ω<*)這裡這裡。   /*    本程式應 習題-12 建立。     題目要求: 重寫程式清單 10.7 的 rain.c 程式,把 main() 中的主要任務都改成用函式來完成。 */ #inc

Flink程式設計練習

Map 班級學生成績的隨機生成 輸入:本班同學的學號 輸出:<學號,成績> 資料準備 首先需要一個stuID.csv檔案,每一列為一個學號: 然後將檔案放入HDFS中: hdfs d

遊戲程式設計模式(Game Programming Patterns)的C#&Unity練習:觀察者模式

觀察者模式 核心描述:“在物件間定義一種 一對多的依賴關係,以便當某個物件的情況發生改變時,與它存在依賴關係的所有物件都能收到通知,並自動進行更新。” 知識點: 1.複習了“繼承”和“多型”,包括讓子類以介面的身份進行活動。子類可以直接使用父類的方法。