首页 > ACM题库 > HDU-杭电 > HDU 1372 Knight Moves-BFS-[解题报告] C++
2013
12-09

HDU 1372 Knight Moves-BFS-[解题报告] C++

Knight Moves

问题描述 :

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.

输入:

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.

输出:

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

样例输入:

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

样例输出:

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.

Problem link adress:click here

This is a basic BFS problem.But the way of knight’s move  is strange,if you don’t know the law of the knight’s move,you are likely to cann’t work the problem out.However,if you can find the regularity of the problem from the given cases’s data,you must be very smart!

#include<iostream>
#include<queue>
#include<string>
using namespace std;
int map[10][10];
char s1[5],s2[5];
int visted[10][10];
int x1,y1;
int x2,y2;
int dir[8][2]={1,2,-1,2,1,-2,-1,-2,2,1,2,-1,-2,1,-2,-1};
struct node
{
	int x,y,step;
};
void BFS()
{
	int x,y;
	node cur,next;
	queue<node>q;
	memset(visted,0,sizeof(visted));
	cur.x=x1;
	cur.y=y1;
	cur.step=0;
	visted[x1][y1]=1;
	q.push(cur);
	while(!q.empty())
	{
		cur=q.front();
		q.pop();
		if(cur.x==x2&&cur.y==y2)
		{
			printf("To get from %s to %s takes %d knight moves.\n",s1,s2,cur.step);
			return ;
		}
		for(int i=0;i<8;i++)
		{
			next.x=x=cur.x+dir[i][0];
			next.y=y=cur.y+dir[i][1];
		    next.step=cur.step+1;
			if(x>=0&&x<8&&y>=0&&y<8)
			{
				visted[x][y]=1;
				q.push(next);
			}

		}
	}
}


int main()
{
	
	while(scanf("%s%s",s1,s2)!=EOF)
	{
		x1=s1[0]-'a';
		y1=s1[1]-'1';
		x2=s2[0]-'a';
		y2=s2[1]-'1';
		BFS();
	}
	return 0;
}

  

解题报告转自:http://www.cnblogs.com/heat-man/archive/2012/12/22/2829576.html


,
  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。

  2. 为什么for循环找到的i一定是素数叻,而且约数定理说的是n=p1^a1*p2^a2*p3^a3*…*pk^ak,而你每次取余都用的是原来的m,也就是n