ZOJ1091 Knight Moves

来源:互联网 发布:淘宝宝贝描述上传图片 编辑:程序博客网 时间:2024/06/04 22:25
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=91
Knight Moves

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 Specification

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 Specification

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.
总感觉这道题在POJ上做过,比较经典的搜索问题,看网上有用最短路做的,还有用dfs做的,这里我用的是bfs做的,白白贡献了几次wrong  PS:  都怪输出太长,没注意细节

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b){
return a>b;
}
struct node
{
int x,y;
int ans;
};
int vis[10][10];
int sx,sy,ex,ey;
int dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};
int bfs()
{
queue<node>q;
node u;
u.x=sx;u.y=sy;
u.ans=0;
q.push(u);
while(!q.empty())
{
node u=q.front();q.pop();
if(u.x==ex&&u.y==ey) return u.ans;
else
{
for(int i=0;i<8;i++)
{
node v;
v.x=u.x+dir[i][0];
v.y=u.y+dir[i][1];
if(v.x<=8&&v.y<=8&&v.x>=1&&v.y>=1&&!vis[v.x][v.y])
{
vis[v.x][v.y]=1;
v.ans=u.ans+1;
q.push(v);
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
char s1[10],s2[10];
while(scanf("%s %s",&s1,&s2)!=EOF)
{
//getchar();
cle(vis);
sx=(int)(s1[0]-'a')+1;
sy=(int)(s1[1]-'0');
ex=(int)(s2[0]-'a')+1;
ey=(int)(s2[1]-'0');
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,bfs());
}
return 0;
}



0 0
原创粉丝点击