bfs 走马 棋盘

写了半天发现没看题 走的是马才对QAQ

D - Knight Moves
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Input

Output

Sample Input

Sample Output

Hint

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<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int chess[10][10] = { 0 };
int d[8][2] = { 2,1,2,-1,-2,1,-2,-1,1,-2,1,2,-1,-2,-1,2};
struct node
{
	int x, y, step;
	//bool operator < (const node &rhs) const {
	//	return rhs.step < step;
	//}


};
	struct node strat, to;
int bfs()
{
	node s, e;
	s.step = 0;
	s.x = strat.x;
	s.y = strat.y;
	queue<node>q;
	q.push(s);
	chess[s.x][s.y] = 1;
	while(!q.empty())
	{
		e = q.front();
		q.pop();
		if(e.x==to.x && e.y==to.y)
		{
			return e.step;
		}
		for(int i=0;i<8;i++)
		{
			s.x = e.x + d[i][0];
			s.y = e.y + d[i][1];
			if(chess[s.x][s.y]==0 && s.x>0 && s.x<9 && s.y>0 && s.y<9)
			{
				s.step = e.step + 1;
				chess[s.x][s.y] = 1;
				q.push(s);
			}
		}
	}
}
int bian(char x)
{
	switch (x)
	{
	case 'a':return 1; break;
	case 'b':return 2; break;
	case 'c':return 3; break;
	case 'd':return 4; break;
	case 'e':return 5; break;
	case 'f':return 6; break;
	case 'g':return 7; break;
	case 'h':return 8; break;
	}
}
int main()
{
	char temp1[2], temp2[2],count=0;
	while (~scanf("%s %s", temp1, temp2))
	{
		memset(chess, 0, sizeof(chess));
		
		strat.x = bian(temp1[0]);
		to.x = bian(temp2[0]);
		strat.y = temp1[1] - 48;
		to.y = temp2[1] - 48;
		int t=bfs();
		printf("To get from %s to %s takes %d knight moves.\n", temp1, temp2,t );
	}
	return 0;
}

//by tp
全部评论

相关推荐

美团 后端开发 总包n(15%是股票)
点赞 评论 收藏
分享
我是小红是我:学校换成中南
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务