Oracle设置(1)设置Oracle数据库为Linux系统服务

Knight Moves


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.
 

典型的一道BFS的题目,就是求两个点间需要多少步才能跳过去(马走日哟),通过这道题把BFS熟悉了一下。


#include <iostream>
#include <string>
#include <queue>
using namespace std;

struct Coordinate
{
	int x,y,s;	
};

// 马向八个方向跳跃的坐标差
int dis[8][2]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
bool visit[10][10];
// step最终步数,sx,sy起点坐标,fx,fy终点坐标
int step,sx,sy,fx,fy;

// 判断是否越界,是否已经访问过
bool judge(int x,int y)
{
	if(x<0 || x>=8 || y<0 || y>=8)	return 0;
	if(visit[x][y]==1)	return 0;
	return 1;
}

void bfs(void)
{
	
	memset(visit,0,sizeof(visit));
	queue <Coordinate> cd;
	Coordinate p,front;
	int i;

// 对起点进行处理
	p.x=sx,p.y=sy,p.s=0;
	visit[p.x][p.y]=1;
	cd.push(p);

	while(!cd.empty())
	{
		front=cd.front();
		cd.pop();

		if(front.x==fx && front.y==fy)	
		{
			step=front.s;
			return;
		}

		for(i=0;i<8;++i)
		{
			p.x=front.x+dis[i][0];
			p.y=front.y+dis[i][1];
			
			if(judge(p.x,p.y))
			{
				p.s=front.s+1;
				visit[p.x][p.y]=1;
				cd.push(p);
			}
		}
	}
}



int main()
{
	string str1,str2;
	while(cin>>str1>>str2)
	{
		// 将输入数据转换为二维数组
		sx=str1[0]-97;
		sy=str1[1]-49;
		fx=str2[0]-97;
		fy=str2[1]-49;

		bfs();
		cout<<"To get from "<<str1<<" to "<<str2<<" takes "<<step<<" knight moves."<<endl;
	}


	return 0;
}




Oracle设置(1)设置Oracle数据库为Linux系统服务

上一篇:[MySQL]导入导出


下一篇:win10下,conda管理虚拟环境