1009. Knight Moves

题目

Time Limit: 1sec Memory Limit:32MB
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
There are multiple test cases. The first line contains an integer T, indicating the number of 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 Copy
8
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output Copy
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.
Hint
This is a typical BFS problem. The graph has 8*8 vertices: (1,1),(1,2), …,(8,8). Two vertices (m1,n1) and (m2,n2) are connected by an edge if the knight can move from (m1,n1) to (m2,n2). Then for a given starting position, you simply do a BFS from the position. When you have arrived at the given destination, you are done.

This is what you might try:

  1. Get the adjacency lists for the graph;

  2. Implelment BFS, and in the algorithm when you push some vertex u into the queue, you will also remember the smallest number of steps from the starting vertex to u and push this number together with u into the queue.

The solution above is a nice exercise to get you familar with graph representations and BFS.

However, you don’t have to constuct the adjacency lists explicitly. Because when you are doing BFS, you will be able to compute the adjacent vertices of a vertex (m,n) by just looking at the vertex itself.

Some other possible solution: computer a 64*64 lookup table where an (i,j) element is the smallest number of steps from vertex i to vertex j. And when you get the input data, you just look it up in the table. This is the all-pairs shortest path problem.

As the table is symmetric, you only need to remember half of the table, and thus save half of the memory.

学习

/*
	2018年11月23日14:37:56
	 
	1009. Knight Moves
	给出两个点的坐标,按照日字走,求步数
	
	在按照日字走路时,与上一个代码有相似地方,都是利用dir[][],然后重新赋值坐标! 
	对dfs有更深的理解 
	调试了很久的代码,发现是细节地方将变量表示错了! 
*/

code

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int MAX = 8;
bool vis[MAX][MAX];
int dir[8][2] = {1, 2, 1, -2, -1, 2, -1, -2, 2, 1, -2, 1, 2, -1, -2, -1};
int sx, sy, ex, ey, Step = 0;
struct node
{
	int x, y, step;//s代表step 
};

void bfs();

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		string s1, s2;
		cin >> s1 >> s2;
		sx = s1[0] - 'a';
		sy = s1[1] - '1';
		ex = s2[0] - 'a';///not 3
		ey = s2[1] - '1';///not 4
		
		memset(vis, false, sizeof(vis));//succeed
//		for(int i = 0; i <8 ;++i)for(int j = 0; j < 8 ; ++j)cout << vis[i][j] << " ";
		bfs();
		cout << "To get from " << s1 << " to " << s2 << " takes "<< Step << " knight moves." << endl;
	}
	
	return 0;
}

void bfs()
{
	queue<node> Q;
	node temp, p, q;
	temp.x = sx;
	temp.y = sy;
	vis[sx][sy] = true;
	temp.step = 0;
	Q.push(temp);
	if(sx == ex && sy == ey)
	{
		Step = 0;
		return;
	}
	while(!Q.empty())
	{
		p = Q.front();
		Q.pop();
	//	Step++;
		for(int i = 0; i < 8; ++i)
		{
		//	cout << "h" << endl;
			int x = p.x + dir[i][0];
			int y = p.y + dir[i][1];
			if(x >= 0 && x < 8 && y >= 0 && y < 8 && (!vis[x][y]))///!!!vis[x][y] not vis[x, y]
			{
				q.x = x;
				q.y = y;
				q.step = p.step + 1;
			// 	cout << x << " "<< y <<" " << q.step << " "<<  vis[x][y]<< endl;
				if(x == ex && y == ey)
				{
					Step = q.step;
					return;
				}
				vis[x][y] = true;
				Q.push(q);
			}
		}
	//	cout << "ex,ey"<< ex << ey << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值