您的位置:首页 > 其它

HDU 1372 Knight Moves

2015-01-31 13:40 337 查看

题目链接: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): 7400 Accepted Submission(s): 4441



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.
DFS版AC代码:[code]#include<iostream>
#include<cstring>
using namespace std;
const int maxn=15,inf=1<<30;
int n,m,dp[maxn][maxn];
char Map[maxn][maxn];
bool vis[maxn][maxn];
bool flag;
int q,sx,sy,ex,ey,Min;
//int dir[][2]={ {0,1},{-1,0},{0,-1},{1,0},{-1,1},{-1,-1},{1,-1},{1,1}};
int dir[][2]={{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2}};
//                 right  up   left   down
void dfs(int x,int y,int cnt)
{
    if(x==ex&&y==ey)
    {
        Min=min(Min,cnt);
        return ;
    }
    for(int i=0;i<8;i++)
    {
        int nx=x+dir[i][0],ny=y+dir[i][1];
        if(0<=nx&&nx<8&&0<=ny&&ny<8)
        {
            if(dp[nx][ny]>cnt) dp[nx][ny]=cnt,dfs(nx,ny,cnt+1);
        }
    }
}
int main()
{
    string start,en;
    while(cin>>start>>en)
    {
        fill(&dp[0][0],&dp[maxn][0],inf);
        sx=start[0]-'a';sy=start[1]-'1';
        ex=en[0]-'a';ey=en[1]-'1';
        Min=1<<30;
        dfs(sx,sy,0);
        cout<<"To get from "<<start<<" to "<<en<<" takes "<<Min<<" knight moves."<<endl;
    }
    return 0;
}
普通BFS版AC代码:#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=105;
char Map[maxn][maxn];
bool vis[maxn][maxn];
struct node
{
int x,y;
int s;
bool friend operator < (node a,node b)
{
return a.s>b.s;
}
};
int n,m,sx,sy,ex,ey;
//int dir[][2]={ {0,1},{-1,0},{0,-1},{1,0},{-1,1},{-1,-1},{1,-1},{1,1}};
// 右 上 左 下 右上 左上 左下 右下
int dir[][2]={{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2}};
//马走日的8个方向
int bfs()
{
if(sx==ex&&sy==ey) return 0;
priority_queue<node>q;
node now,next;
now.x=sx,now.y=sy,now.s=0;
q.push(now);
memset(vis,0,sizeof(vis));
vis[sx][sy]=1;
while(q.size())
{
now=q.top();q.pop();
for(int i=0;i<8;i++)
{
int nx=now.x+dir[i][0],ny=now.y+dir[i][1];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&Map[nx][ny]!='X'&&!vis[nx][ny])
{
vis[nx][ny]=1;
next.x=nx,next.y=ny,next.s=now.s+1;
if(nx==ex&&ny==ey) return next.s;
q.push(next);
}
}
}
return -1;
}
int main()
{
string start,en;
n=m=8;
while(cin>>start>>en)
{
sx=start[0]-'a';sy=start[1]-'1';
ex=en[0]-'a';ey=en[1]-'1';
int Min=bfs();
cout<<"To get from "<<start<<" to "<<en<<" takes "<<Min<<" knight moves."<<endl;
}
return 0;
}
[/code]
双向广搜AC代码:
#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=15;
int vis[maxn][maxn];
int dp[maxn][maxn];
struct node
{
    int x,y;
};
int n,m,sx,sy,ex,ey;
//int dir[][2]={ {0,1},{-1,0},{0,-1},{1,0},{-1,1},{-1,-1},{1,-1},{1,1}};
//                    右    上     左       下    右上  左上     左下  右下
int dir[][2]={{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2}};
//马走日的8个方向
int bfs()
{
    if(sx==ex&&sy==ey) return 0;
    queue<node>q;
    node now,next;
    now.x=sx,now.y=sy;
    next.x=ex,next.y=ey;
    q.push(now);
    q.push(next);
    memset(vis,0,sizeof(vis));
    vis[sx][sy]=1;
    vis[ex][ey]=2;
    while(q.size())
    {
        now=q.front();q.pop();
        //if(now.x==ex&&now.y==ey) return now.s;
        int x=now.x,y=now.y;
        for(int i=0;i<8;i++)
        {
            int nx=x+dir[i][0],ny=y+dir[i][1];
            if(0<=nx&&nx<n&&0<=ny&&ny<m)
            {
                if(!vis[nx][ny])
                {
                    vis[nx][ny]=vis[x][y];
                    next.x=nx,next.y=ny;
                    dp[nx][ny]=dp[x][y]+1;
                    q.push(next);
                }
                else if(vis[x][y]!=vis[nx][ny]) return dp[x][y]+dp[nx][ny]+1;
            }
        }
    }
    return -1;
}
int main()
{
     string start,en;
    n=m=8;
    while(cin>>start>>en)
    {
        memset(dp,0,sizeof(dp));
        sx=start[0]-'a';sy=start[1]-'1';
        ex=en[0]-'a';ey=en[1]-'1';
        int Min=bfs();
        cout<<"To get from "<<start<<" to "<<en<<" takes "<<Min<<" knight moves."<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: