Red Knight's Shortest Path

题目太长,就不写了,出自:https://www.hackerrank.com/contests/world-codesprint-12/challenges/red-knights-shortest-path

思路:用广度优先搜索寻找终点,广度优先搜索是求最短路径的一种方法。注意保存路径的方法,比如a->b->c,不管c前面的b是怎么走的,c始终是在b的基础上转移过来的,所以c的路径就是b的路径加上b->c的路径。

//#include <bits/stdc++.h>
#include<iostream>
#include<queue>
#include<string>
using namespace std;

void printShortestPath(int n, int i_start, int j_start, int i_end, int j_end) {
	//  Print the distance along with the sequence of moves.
	typedef struct{
		int row;
		int col;
		string str;
		//bool visited;
	}Vertex;
	//Vertex vertex[200][200];
	int count = 0;
	//int flag = 0;
	//int flagFor = 0;
	//int flagWhile = 0;
	int numVertex = n*n;
	bool visited[200][200] = { false };
	/*string str1, str2, str3, str4, str5, str6;
	int flagStr1 =0, flagStr2 = 0, flagStr3 = 0, flagStr4 = 0, flagStr5 = 0, flagStr6 = 0;
*/
	queue<Vertex> Q;
	Vertex start = { i_start, j_start };
	visited[i_start][j_start] = true;
	Q.push(start);

	/*while(numVertex-1)
	{*/

		while(!Q.empty()){
			Vertex v = Q.front();
			Q.pop();

			for(int i = 1; i <= 6; i++){
				switch(i){
				case 1://UL
					if(v.row - 2 >= 0 && v.col - 1 >= 0 && visited[v.row - 2][v.col - 1] == false)
					{
						visited[v.row - 2][v.col - 1] =true;
						Vertex temp = { v.row - 2, v.col - 1 ,v.str+"UL "};
						//vertex[v.row - 2][v.col - 1].str += "UL ";
						Q.push(temp);
						//str1 += "UL ";
						if(temp.row == i_end && temp.col == j_end)
						{
							//flag = 1;
							//flagStr1 = 1;
                                               for(int i =0;i<temp.str.size();i++)
                       if(temp.str[i]==' ')
                           count++;
                   cout<<count<<endl;
							cout << temp.str << endl;
							return;
							//break;
						}
												
					}break;
				case 2://UR
					if(v.row - 2 >= 0 && v.col + 1 <n  && visited[v.row - 2][v.col + 1] == false)
					{
						visited[v.row - 2][v.col + 1] = true;
						Vertex temp = { v.row - 2, v.col + 1 ,v.str+"UR "};
						Q.push(temp);
						//str2 += "UR ";
						if(temp.row == i_end && temp.col == j_end)
						{
							///flag = 1;
							//flagStr2 = 1;
                                               for(int i =0;i<temp.str.size();i++)
                       if(temp.str[i]==' ')
                           count++;
                   cout<<count<<endl;
							cout << temp.str << endl;
							return;
							//break;
						}
						
						
					}break;
				case 3://R
					if(v.col + 2 <n && visited[v.row][v.col + 2] == false)
					{
						visited[v.row][v.col + 2] = true;
						Vertex temp = { v.row, v.col + 2, v.str+"R " };
						Q.push(temp);
						//str3 += "R ";
						if(temp.row == i_end && temp.col == j_end)
						{
							//flag = 1;
							//flagStr3 = 1;
                                               for(int i =0;i<temp.str.size();i++)
                       if(temp.str[i]==' ')
                           count++;
                   cout<<count<<endl;
							cout << temp.str << endl;
							return;
							//break;
						}
											
					}break;
				case 4://LR				
					if(v.row + 2 < n && v.col + 1 < n && visited[v.row + 2][v.col + 1] == false)
					{
						visited[v.row + 2][v.col + 1] = true;
						Vertex temp = { v.row + 2, v.col + 1 ,v.str+"LR "};
						Q.push(temp);
						//str4 += "LR ";
						if(temp.row == i_end && temp.col == j_end)
						{
							//flag = 1;
							//flagStr4 = 1;
                                               for(int i =0;i<temp.str.size();i++)
                       if(temp.str[i]==' ')
                           count++;
                   cout<<count<<endl;
							cout << temp.str << endl;
							return;
							//break;
						}
						//else{						
					}break;
				case 5://LL
					if(v.row + 2 <n && v.col - 1 >= 0 && visited[v.row + 2][v.col - 1] == false)
					{
						visited[v.row + 2][v.col - 1] = true;
						Vertex temp = { v.row + 2, v.col - 1 , v.str+"LL "};
						Q.push(temp);
						//str5 += "LL ";
						if(temp.row == i_end && temp.col == j_end)
						{
							//flag = 1;
							//flagStr5 = 1;
                                               for(int i =0;i<temp.str.size();i++)
                       if(temp.str[i]==' ')
                           count++;
                   cout<<count<<endl;
							cout << temp.str << endl;
							return;
							//break;
						}
						//else						
					}break;
				case 6://L
					if(v.col - 2 >= 0 && visited[v.row][v.col - 2] == false)
					{
						visited[v.row][v.col - 2] = true;
						Vertex temp = { v.row, v.col - 2 ,v.str+"L "};
						//vertex[v.row ][v.col - 2].str += "L ";
						Q.push(temp);
						//str6 += "L ";
						if(temp.row == i_end && temp.col == j_end)
						{
							//flag = 1;
							//flagStr6 = 1;
                   for(int i =0;i<temp.str.size();i++)
                       if(temp.str[i]==' ')
                           count++;
                   cout<<count<<endl;
							cout << temp.str << endl;
							return;
							//break;
						}
											
					}break;
				}//switch(i)
		
			}//for(i)
			

		}//while(Q)


	cout << "Impossible" << endl;
	
	//return;
}

int main() {
	int n;
	cin >> n;
	int i_start;
	int j_start;
	int i_end;
	int j_end;
	cin >> i_start >> j_start >> i_end >> j_end;
	printShortestPath(n, i_start, j_start, i_end, j_end);
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值