HDU1372&&POJ1915

来源:互联网 发布:淘宝指数"怎样应用 编辑:程序博客网 时间:2024/06/04 17:52

Knight Moves

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


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 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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.
Knight Moves
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 20208 Accepted: 9365

Description

BackgroundMr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?The ProblemYour task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.For people not familiar with chess, the possible knight moves are shown in Figure 1.

Input

The input begins with the number n of scenarios on a single line by itself.Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.

Output

For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.

Sample Input

380 07 01000 030 50101 11 1

Sample Output

5280
两题基本一模一样 BFS求起点到终点的最短距离,可以转化为中国象棋的马走日的形式,第二题POJ上也就是给的那个棋盘变大了而已,其他都是一样的- -。所以一遍BFS搜
过去就可以得到答案。。。

#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<queue>#include<algorithm>using namespace std;int dis[10][2]={{-2,1},{-2,-1},{2,1},{2,-1},{-1,2},{-1,-2},{1,2},{1,-2}};struct node{int x;int y;int step;}a;int ans;int vis[10][10];void bfs(int x,int y){    if(x==a.x&&y==a.y)    {        ans=0;        return ;    }    int i;    memset(vis,0,sizeof(vis));    vis[x][y]=1;    queue<node> q;    node aa,bb;    aa.x=x;    aa.y=y;    aa.step=0;    q.push(aa);    while(!q.empty())    {        bb=q.front();        q.pop();        for(i=0;i<8;i++)        {            aa.x=bb.x+dis[i][0];            aa.y=bb.y+dis[i][1];            aa.step=bb.step+1;            if(aa.x==a.x&&aa.y==a.y)            {                ans=aa.step;                return ;            }            if(aa.x>=1&&aa.x<=8&&aa.y>=1&&aa.y<=8&&vis[aa.x][aa.y]==0)            {                vis[aa.x][aa.y]=1;                q.push(aa);            }        }    }}int main(){    char s1[10];    char s2[10];    node s;    while(cin>>s1>>s2)    {        s.x=s1[0]-'a'+1;        s.y=s1[1]-'0';        a.x=s2[0]-'a'+1;        a.y=s2[1]-'0';        ans=0;        bfs(s.x,s.y);         cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<ans<<" knight moves."<<endl;    }    return 0;}















/**********************************************************************************************************************************/
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>using namespace std;struct node{int x;int y;int step;}a;int dis[10][2]={{-2,1},{-2,-1},{2,1},{2,-1},{-1,2},{-1,-2},{1,2},{1,-2}};int  vis[400][400];int ans;int n;void bfs(int x,int y){    if(x==a.x&&y==a.y)    {        ans=0;        return ;    }    int i;    memset(vis,0,sizeof(vis));    vis[x][y]=1;    queue<node> q;    node aa,bb;    aa.x=x;    aa.y=y;    aa.step=0;    q.push(aa);    while(!q.empty())    {        bb=q.front();        q.pop();      for(i=0;i<8;i++)      {          aa.x=bb.x+dis[i][0];          aa.y=bb.y+dis[i][1];          aa.step=bb.step+1;          if(aa.x==a.x&&aa.y==a.y)          {              ans=aa.step;              return ;          }          if(aa.x>=0&&aa.x<n&&aa.y>=0&&aa.y<n&&vis[aa.x][aa.y]==0)          {              vis[aa.x][aa.y]=1;              q.push(aa);          }      }    }}int main(){    int t;    cin>>t;    node s;    int sx,sy;    int ex,ey;    while(t--)    {        cin>>n;        cin>>sx>>sy;        cin>>ex>>ey;        s.x=sx;        s.y=sy;        a.x=ex;        a.y=ey;        ans=0;        bfs(s.x,s.y);        cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击