首页 > 其他 > 详细

HDU 1372 Knight Moves (搜索 使用 dfs bfs两种实现)

时间:2014-04-16 12:56:55      阅读:532      评论:0      收藏:0      [点我收藏+]

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5875    Accepted Submission(s): 3558


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.
 

题目意思:在一张8*8的象棋盘上,马走日的方式移动,问任意给定的两点pa,pb,问从pa到pb至少需要移动几步。 bfs是最好的解决方法,简单而且高效,在这里我同时用了dfs来实现,在时间上稍差了一些,但仍然能过。。。。


BFS 版本

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <queue>
#include <climits>

using namespace std;

const int MAX = 8;
const int dirx[MAX] = {-2,-2,2,2,-1,-1,1,1},diry[MAX] = {1,-1,-1,1,2,-2,-2,2};

typedef struct Point{
	int x,y;
}Point;

Point p;
int cstep[MAX][MAX],dx,dy;
queue<Point> que;

void init(){
	int i,j;

	for(i=0;i<MAX;++i){
		for(j=0;j<MAX;++j){
			cstep[i][j] = -1;
		}
	}

	while(!que.empty()){
		que.pop();
	}
}


int bfs(int x,int y){
	int i,tx,ty;
	p.x = x;
	p.y = y;
	cstep[x][y] = 0;
	que.push(p);
	while(!que.empty()){
		p = que.front();
		que.pop();
		x = p.x;
		y = p.y;
		if(x==dx && y==dy)break;
		for(i=0;i<MAX;++i){
			tx = x + dirx[i];
			ty = y + diry[i];
			if(tx<0 || ty<0 || tx>=MAX || ty>=MAX)continue;
			if(cstep[tx][ty]!=-1)continue;
			cstep[tx][ty] = cstep[x][y] + 1;
			p.x = tx;
			p.y = ty;
			que.push(p);
		}
	}
	return cstep[dx][dy];
}

int main(){
	//freopen("in.txt","r",stdin);
	char csx,csy,cdx,cdy;
	int sx,sy,minx;
	while(scanf("%c%c %c%c%*c",&csy,&csx,&cdy,&cdx)!=EOF){
		sx = csx-‘1‘;
		sy = csy-‘a‘;
		dx = cdx-‘1‘;
		dy = cdy-‘a‘;
		init();
		minx = bfs(sx,sy);
		printf("To get from %c%c to %c%c takes %d knight moves.\n",csy,csx,cdy,cdx,minx);
	}
    return 0;
}


DFS 版本


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

const int MAX = 8;
const int dirx[MAX] = {-2,-2,2,2,-1,-1,1,1},diry[MAX] = {1,-1,-1,1,2,-2,-2,2};

int cstep[MAX][MAX],minx,dx,dy;

void init(){
	int i,j;

	for(i=0;i<MAX;++i){
		for(j=0;j<MAX;++j){
			cstep[i][j] = INT_MAX;
		}
	}
}

void dfs(int x,int y,int cnt){
	if(x<0 || y<0 || x>=MAX || y>=MAX)return;
	if(x==dx && y==dy){
		if(cnt<minx)minx = cnt;
		return;
	}
	//通过下面注释掉的main方法,运行处8*8棋盘,任意两个点最短距离的最大值为6
	//所以加上这个剪枝,不加这个剪枝就超时
	if(cnt>6)return;
	if(cnt>cstep[x][y])return;
	cstep[x][y] = cnt;

	int i,tx,ty;

	for(i=0;i<MAX;++i){
		tx = x + dirx[i];
		ty = y + diry[i];
		dfs(tx,ty,cnt+1);
	}

}

int main(){
	//freopen("in.txt","r",stdin);
	char csx,csy,cdx,cdy;
	int sx,sy;
	while(scanf("%c%c %c%c%*c",&csy,&csx,&cdy,&cdx)!=EOF){
		sx = csx-‘1‘;
		sy = csy-‘a‘;
		dx = cdx-‘1‘;
		dy = cdy-‘a‘;
		minx = INT_MAX;
		init();
		dfs(sx,sy,0);
		printf("To get from %c%c to %c%c takes %d knight moves.\n",csy,csx,cdy,cdx,minx);
	}
    return 0;
}



/*
int main(){
//	freopen("out.txt","w",stdout);
	int i,j,ans;
	dx = 0,dy = 0;
	ans = -1;
	for(i=0;i<8;++i){
		for(j=0;j<8;++j){
			minx = INT_MAX;
			init();
			dfs(i,j,0);
			printf("%d,",minx);
			if(minx>ans)ans = minx;
		}
	}
	printf("\n%d\n",ans);
	return 0;
}
*/



HDU 1372 Knight Moves (搜索 使用 dfs bfs两种实现),布布扣,bubuko.com

HDU 1372 Knight Moves (搜索 使用 dfs bfs两种实现)

原文:http://blog.csdn.net/iaccepted/article/details/23748039

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!