hdu 1372 Knight Moves BFS解法 + A*算法 两种解法AC

154 篇文章 1 订阅
33 篇文章 0 订阅

Knight Moves

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


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.
 
水题一枚,值得注意的国际象棋中马的走法和 象棋中是一样的,走日。
一开始用DFS,TLE,就改成了BFS。另外坐标全是小于10的数,所以我另pos = x*10+y;这样便于操作也便于分离。
看代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <queue>

using namespace std ;

int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
queue<int> q;
int visited[15][15] ;
int des , ans = INT_MAX;

bool judge(int x ,int y)
{
	if(x<=0||x>8 || y<=0||y>8)
	{
		return false;
	}
	return true ;
}
void BFS(int pos)
{
	q.push(pos) ;
	while(!q.empty())
	{
		int now = q.front();
		int x=now/10,y=now%10 ;
		q.pop();
		if(now == des) 
		{
			if(visited[x][y] < ans)
			{
				ans = visited[x][y] ;
			}
			continue ;
		}
		else if(visited[x][y]>=ans)
		{
			continue ;
		}
		for(int i = 0 ; i < 8 ; ++i)
		{
			int nextX = x+dir[i][0] , nextY = y+dir[i][1] ;
			if(judge(nextX,nextY) && (visited[nextX][nextY] == 0 || visited[nextX][nextY]<=visited[x][y]+1))
			{
				visited[nextX][nextY] = visited[x][y]+1 ;
				q.push(nextX*10+nextY) ;
			}
		}
	}
	
}

int main()
{
	char p1[4],p2[4];
	while(scanf("%s%s",p1,p2)!=EOF)
	{
		ans = INT_MAX ;
		des = (p2[0]-'a'+1)*10+(p2[1]-'0');
		memset(visited,0,sizeof(visited)) ;
		BFS((p1[0]-'a'+1)*10+(p1[1]-'0')) ;
		printf("To get from %s to %s takes %d knight moves.\n",p1,p2,ans);
	}
	return 0 ;
}

/**************************************update2015/2/14**********************************************************/

上面的BFS 花费了200MS+,而A*只用30MS+,孰优孰劣,一看就明白了。

A*算法代码:

#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>
#define MAX 10
using namespace std ;
struct Knode{
	int x,y,g,h,f,step;
	bool operator<(const Knode &k) const
	{
		return f > k.f ;
	} 
};
int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
bool close[MAX][MAX] ;
int row = 8 , col = 8 ,desX,desY,ans;
priority_queue<Knode> p ;

bool judge(Knode t)
{
	if(t.x<0||t.x>=row || t.y<0||t.y>=col)
	{
		return false ;
	}
	return true ;
}
int Heuristic(const Knode &a)		//manhattan估价函数
{                    
	return (abs(a.x-desX)+abs(a.y-desY))*10;
}
void AStar()
{
	while(!p.empty())
	{
		Knode k = p.top();
		p.pop();
		close[k.x][k.y] = true ;
		if(close[desX][desY])
		{
			ans = k.step ;
			break ;
		}
		for(int i = 0 ; i < 8 ; ++i)
		{
			Knode t ;
			t.x = k.x+dir[i][0] , t.y = k.y+dir[i][1] ;
			if(!judge(t))
			{
				continue ;
			}
			if(!close[t.x][t.y])
			{
				t.g = k.g + 23 ;	//23表示根号5乘以10再取其ceil
				t.h = Heuristic(t) ;
				t.f = t.g+t.h ;
				t.step = k.step+1 ;
				p.push(t) ; 
			}
		}
	}
}

int main()
{
	char str[6];
	while(gets(str))
	{
		Knode k ;
		k.x = (str[0]-'a') , k.y = (str[1]-'1') ;
		k.f = k.g = k.h = k.step = 0 ;
		p.push(k) ;
		desX = str[3]-'a' , desY = str[4]-'1' ;
		ans = 0 ;
		memset(close,false,sizeof(close)) ;
		AStar();
		printf("To get from %c%c to %c%c takes %d knight moves.\n",str[0],str[1],str[3],str[4],ans) ;
		while(!p.empty())	p.pop() ;
	}
	return 0 ;
}

PS:我这代码在关闭控制台的时候就会停止工作。。不知道为啥,但是可以正常AC。。有大神看出来BUG,请告知我一声,谢谢。

/*******************************************************************************************************************/

我还是想把我超时的DFS代码贴上来,虽然超时,但是思路还是对的。
Ps:如果谁有剪枝的思路也可以和我交流一下,

//超时代码 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#define MAX 90

using namespace std ;

int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};

bool visited[MAX] ;
int des , ans = INT_MAX;

bool judge(int x ,int y)
{
	if(x<=0||x>8 || y<=0||y>8)
	{
		return false;
	}
	if(visited[x*10+y])
	{
		return false;
	}
	return true ;
}

void DFS(int pos , int step)
{
	if(pos == des)
	{
		if(ans>step)
		{
			ans = step ;
		}
		return ;
	}
	else if(step>=ans)
	{
		return ;
	}
	else
	{
		int x = pos/10,y = pos%10 ;
		for(int i = 0 ; i < 8 ; ++i)
		{
			int nextX = x + dir[i][0],nextY = y + dir[i][1] ;
			if(judge(nextX,nextY))
			{
				visited[nextX*10+nextY] = true ;
				DFS(nextX*10+nextY,step+1) ;
				visited[nextX*10+nextY] = false ;
			}
		}
	}
}

int main()
{
	char p1[4],p2[4];
	while(scanf("%s%s",p1,p2)!=EOF)
	{
		ans = INT_MAX ;
		des = (p2[0]-'a'+1)*10+(p2[1]-'0');
		sizeof(visited,0,sizeof(visited)) ;
		DFS((p1[0]-'a'+1)*10+(p1[1]-'0'),0) ;
		printf("%d\n",ans);
	}
	return 0 ;
}

与大家共勉。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值