/**
* Copyright (c) 2019 MMMMMMoSky All rights reserved.
* g++ HorseTraverse.cpp -o tmp -std=c++11
*/
#include "HorseTraverse.h"
#include <string>
#include <iostream>
using namespace std;
const int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
const int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int main()
{
cout << "本程序将求解 N * N 的地图中放置一个马, 是否可以不重复的遍历所有点" << endl;
string line;
// 读入地图大小
int N = -1;
while (N < 0)
{
cout << "请输入地图大小 N = ";
getline(cin, line);
try
{
N = stoi(line);
}
catch (const out_of_range &e)
{
N = -1;
}
catch (const invalid_argument &e)
{
N = -1;
}
if (N < 5 || N > 8)
{
cout << "N 的取值范围是 [5, 8]" << endl;
N = -1;
}
}
// 读入初始坐标
int x = -1, y = -1;
while (x < 0)
{
cout << "请输入马的初始位置坐标(从0开始, 空格隔开的两个整数): ";
getline(cin, line);
int pos = line.find(' ');
if (pos == line.npos)
{
continue;
}
try
{
x = stoi(line.substr(0, pos));
y = stoi(line.substr(pos, line.size() - pos));
}
catch (const out_of_range &e)
{
x = -1;
}
catch (const invalid_argument &e)
{
x = -1;
}
if (x < 0 || x >= N || y < 0 || y >= N)
{
cout << "横纵坐标取值范围为 [0, " << N << ")" << endl;
}
}
// TODO: 待扩展 设置"蹩马腿"的棋子
vector<vector<bool>> visited(N, vector<bool>(N, false));
vector<int> path;
visited[x][y] = true;
if (traceBack(x, y, N, N, N * N, visited, path))
{
cout << "下面是一条合法的路径, 不重复地遍历所有的点:\n";
// 打印路径
for (int i = 1; i < N * N; i++)
{
printf("(%d,%d) -> ", x, y);
x += dx[path[i - 1]];
y += dy[path[i - 1]];
if (i % 5 == 0)
{
putchar('\n');
}
}
printf("(%d,%d) Over!\n", x, y);
}
else
{
cout << "无解" << endl;
}
return 0;
}
bool traceBack(
int x, int y, int N, int M, int left,
vector<vector<bool>> &visited,
vector<int> &path)
{
left--;
if (left == 0)
{
return true;
}
for (int i = 0; i < 8; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= N || ny < 0 || ny >= M || visited[nx][ny])
{
continue;
}
path.push_back(i);
visited[nx][ny] = true;
if (traceBack(nx, ny, N, M, left, visited, path))
{
return true;
}
visited[nx][ny] = false;
path.pop_back();
}
return false;
}
基于C++进行象棋中马的遍历(高级语言程序设计实验)
版权申诉
175 浏览量
2023-06-30
17:39:21
上传
评论
收藏 3KB RAR 举报
神仙别闹
- 粉丝: 2687
- 资源: 7658
最新资源
- 海尔618算价表_七海5.20_16.00xlsx(1)(2).xlsx
- WebCrawler.scr
- 【计算机专业毕业设计】大学生就业信息管理系统设计源码.zip
- YOLO 数据集:8种路面缺陷病害检测【包含划分好的数据集、类别class文件、数据可视化脚本】
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于YOLOv8的FPS TPS AI自动锁定源码+使用步骤说明.zip
- JAVA实现Modbus RTU或Modbus TCPIP案例.zip
- 基于yolov8+streamlit的火灾检测部署源码+模型.zip
- 测试aaaaaaabbbbb
- VID20240521070643.mp4
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈