POJ1915 Knight Moves BFS (UVA439 与之相似)

1.常数组记录骑士的8个方向走法

2.用网记录骑士走过的点,防止重复 reached[MAX_SIZIE][MAX_SIEZE];

3.运用队列queue。(自己写的第一个版本是用递归, 各种错误。 AC版本是看了:(原连接找不到了尴尬) 理解之后 自己重写的)


先上AC代码:

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

#define MAX_SIZE (300 + 10)
#define REACHED 1
#define NO_REACHED 0

struct Site {
	int x, y;
	int steps;
};

int size, reached[MAX_SIZE][MAX_SIZE];
int const direction[8][2] = {{1,2}, {-1,2}, {1,-2}, {-1,-2}, {2,1}, {-2,1}, {2,-1}, {-2,-1}};
Site end;

int letsGo(Site);
bool rightfulSite(Site);

int main(int argc, char *argv[])
{
	int cases;
	Site star;

	cin >> cases;

	while (cases--) {
		cin >> size;
		cin >> star.x >> star.y;
		cin >> end.x >> end.y;

		memset(reached, NO_REACHED, sizeof(reached));
		star.steps = 0; end.steps = 0;

		cout << letsGo(star) << endl;
	}

	return 0;//!!!!!
}

/*************************************************************
 * letsGo(): BFS the way
 * **********************************************************/
int letsGo(Site cur)
{
	queue <Site> q;
	Site tp1, tp2;

	q.push(cur);

	while (!q.empty()) {
		tp1 = q.front();
		q.pop();

//for test
///cout << tp1.steps << "*" << endl;
//end
		if (tp1.x == end.x && tp1.y == end.y) {
			return tp1.steps;
		}

		for (int i = 0; i < 8; i++) {

			tp2.x = tp1.x + direction[i][0];
			tp2.y = tp1.y + direction[i][1];
			tp2.steps = tp1.steps + 1;
//for test
//cout << tp2.steps << "*" << endl;
//end
			
			if (rightfulSite(tp2)) {
				q.push(tp2);
				reached[tp2.x][tp2.y] = REACHED;
			}else {
				continue;
			}
		}
	}

	return 0;
}

/*************************************************************
 * rightfulSite(): check if the size is in size*size and the
 *					size have reached.
 ************************************************************/
bool rightfulSite(Site cur) 
{
	if (cur.x >= 0 && cur.x < size
	 && cur.y >= 0 && cur.y < size
	 && reached[cur.x][ cur.y] == NO_REACHED) {

		return true;
	}else {
		return false;
	}
}


这个程序没有用到递归(栈的思想), 而是用到queue(队列思想), 程序考reached[][] 来记录某点是否走过, 若用后进先出的栈思想, 会让无用的Knight提前“占领”

reached 导致后来者一到达 就被return。(我自己写的就是这样)。  讲搜索的时候没去, 什么都没听睡觉真是伤脑筋。。。先做一题, 学一题吧。


下面是我自己原有的思路(栈思想):

#include <stdio.h>
#include <string.h>

#define MAX_STEPS 90000
#define MAX_L 300
#define REACHED true
#define NOREACHED false

struct knight{
	int x;
	int y;
	int steps;
};

int min, l;
bool reached[MAX_L][MAX_L];
knight end;
int const direction[8][2] = {{2,1}, {2,-1}, {-2,1}, {-2,-1}, {1,2}, {1,-2}, {-1,2},{-1,-2}};

void letsGo(knight);

int main() {
	int n;
	knight star;

	scanf("%d", &n);

	while (n--) {
		scanf("%d", &l);
		scanf("%d%d", &star.x, &star.y);
		scanf("%d%d", &end.x, &end.y);

		star.steps = 0;
		min = MAX_STEPS;
		memset(reached, 0, sizeof(reached)); //NOREACHED = false = 0

		letsGo(star);

		printf("%d\n", min);
	}

	return 0;
}

/***************************************************************************************
 * letsGo():
 * ************************************************************************************/
void letsGo(knight pre) {
	int tp_x, tp_y;
	bool tp_reached[MAX_L][MAX_L];

	//reached end
	if (pre.x == end.x && pre.y == end.y) {
		min = pre.steps < min ? pre.steps : min;
		return;
	}

	//over size of repeat reached a point
	if (pre.x < 0 || pre.y < 0 || pre.x >= l || pre.y >= l) {
		return;
	}

	if (reached[pre.x][pre.y] == REACHED) {
		return;
	}else {
		reached[pre.x][pre.y] = REACHED; //mark reached
	}

	for (int i = 0; i < 8; i++) {
		tp_x = pre.x; 
		tp_y = pre.y;
		/*for (int r = 0; r < l; r++) {
			for (int c = 0; c < l; c++) {
				tp_reached[r][c] = reached[r][c];
			}
		}*/

		pre.x += direction[i][0];
		pre.y += direction[i][1];
		pre.steps++;
		letsGo(pre);

		pre.x = tp_x;
		pre.y = tp_y;
		pre.steps--;
		/*for (int r = 0; r < l; r++) {
			for (int c = 0; c < l; c++) {
				reached[r][c] = tp_reached[r][c];
			}
		}
	}
}






1.毛病1 在上面说过了, reached被占用。 程序运行的结果会因为 l 的不同的不同 尴尬

2. 假如每个分支都有一个reached来记录路线, 那么程序运行的结果是 l > 3以上就要等很久。 正不正确还不知道, 远远超时了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值