马踏棋盘

#if 1
#include<iostream>
#include<ctime>
#include<Windows.h>
#include<thread>

#define X 5
#define Y 5


int chess[X][Y];

//	马 的 下 一 步 是 否 可 行
int nextxy(int* x, int* y, size_t count)
{	
	// 马 往下一个位置跳 chess[*x][*y] == 0 未踩过的地方 
	// 找到位置 成功返回1
	switch (count)
	{
	case 0:
		if (*x + 2 <= X - 1 && *y - 1 >= 0 && chess[*x + 2][*y - 1] == 0)
		{
			*x = *x + 2;
			*y = *y - 1;
			return 1;
		}
		break;

	case 1:
		if (*x + 2 <= X - 1 && *y + 1 <= Y - 1 && chess[*x + 2][*y + 1] == 0)
		{
			*x = *x + 2;
			*y = *y + 1;
			return 1;
		}
		break;

	case 2:
		if (*x + 1 <= X - 1 && *y - 2 >= 0 && chess[*x + 1][*y - 2] == 0)
		{
			*x = *x + 1;
			*y = *y - 2;
			return 1;
		}
		break;

	case 3:
		if (*x + 1 <= X - 1 && *y + 2 <= Y - 1 && chess[*x + 1][*y + 2] == 0)
		{
			*x = *x + 1;
			*y = *y + 2;
			return 1;
		}
		break;

	case 4:
		if (*x - 2 >= 0 && *y - 1 >= 0 && chess[*x - 2][*y - 1] == 0)
		{
			*x = *x - 2;
			*y = *y - 1;
			return 1;
		}
		break;

	case 5:
		if (*x - 2 >= 0 && *y + 1 <= Y - 1 && chess[*x - 2][*y + 1] == 0)
		{
			*x = *x - 2;
			*y = *y + 1;
			return 1;
		}
		break;

	case 6:
		if (*x - 1 >= 0 && *y - 2 >= 0 && chess[*x - 1][*y - 2] == 0)
		{
			*x = *x - 1;
			*y = *y - 2;
			return 1;
		}
		break;

	case 7:
		if (*x - 1 >= 0 && *y + 2 <= Y - 1 && chess[*x - 1][*y + 2] == 0)
		{
			*x = *x - 1;
			*y = *y + 2;
			return 1;
		}
		break;

	default:
		break;
	}

	return 0;
}

// 打印棋盘跳马步骤
void print()
{
	for (int i = 0; i < X; i++)
	{
		for (int j = 0; j < Y; j++)
		{
			std::cout << chess[i][j] << "\t";
		}
		std::cout << "\n";
	}
}

//绘制棋盘
// 深度优先遍历、回溯算法、递归
//(x, y) 为起始位置
//tag标记变量,在棋盘的具体步骤值
int ChessBoard(int x, int y, int tag)
{
	int x1 = x, y1 = y, count = 0;
	size_t flag = 1;
	chess[x][y] = tag;
	if (X * Y == tag)		//棋盘所有位置成功遍历完毕
	{
		print();
		return 1;
	}


	//开始执行跳马		第一次搜索
	flag = nextxy(&x1, &y1, count);		//从八个方向选一个方向找到可行位置,找到则继续
	while (flag==0 && count < 7)		//找不到换一个方向继续寻找
	{
		count++;
		flag = nextxy(&x1, &y1, count);
	}

	while (flag)
	{
		//如果第一次就找到下一个可以跳的位置则继续
		if ( ChessBoard(x1, y1, tag + 1) )
		{

			return 1;
		}
		
		// 从第二次开始 如果失败
		x1 = x;
		y1 = y;
		count++;

		flag = nextxy(&x1, &y1, count);
		while (flag == 0 && count < 7)
		{
			count++;
			flag = nextxy(&x1, &y1, count);

		}

	}
	if (0 == flag)
	{
		chess[x][y] = 0;
	}
	return 0;
}

void bar()
{

	std::cout << "\n正在输出记录数据...... ";
	for (int i = 0; i <= 100; i++)      // 打印百分比 
	{
		std::cout.width(3);//i的输出为3位宽
		std::cout << i << "%";
		std::cout << "\b\b\b\b";//回删三个字符,使数字在原地变化
	}
	std::cout << "\n\n";
}

int main()
{
	//初始化棋盘
	for (int i = 0; i < X; i++)
	{
		for (int j = 0; j < Y; j++)
		{
			chess[i][j] = 0;
		}

	}

	//auto i = ChessBoard(2, 0, 1);
	//std::thread first(bar);
	//std::thread second(ChessBoard, 2, 0, 1);
	ChessBoard(2, 0, 1);

	return 0;
}

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值