c++ - 如何让它沿对角线方向移动?
问题描述
我的代码已经有四个基本方向(上、下、左、右)。但是,我也想让它们沿对角线方向移动,因此最多可以添加八个方向。我该怎么做?我从中看到,我基本上必须同时改变两个方向,但我不知道该怎么做。
代码:
// problem with backtracking using stack
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
#define N 4
#define M 5
class node {
public:
int x, y;
int dir;
node(int i, int j)
{
x = i;
y = j;
// Initially direction
// set to 0
dir = 0;
}
};
// maze of n*m matrix
int n = N, m = M;
// Coordinates of food
int fx, fy;
bool visited[N][M];
bool isReachable(int maze[N][M])
{
// Initially starting at (0, 0).
int i = 0, j = 0;
cout << '[' << i << ':' << j << ']';
stack<node> s;
node temp(i, j);
s.push(temp);
while (!s.empty()) {
// Pop the top node and move to the
// left, right, top, down or retract
// back according the value of node's
// dir variable.
temp = s.top();
int d = temp.dir;
i = temp.x, j = temp.y;
// Increment the direction and
// push the node in the stack again.
temp.dir++;
s.pop();
s.push(temp);
// If we reach the Food coordinates
// return true
if (i == fx and j == fy) {
return true;
}
// Checking the Up direction.
if (d == 0) {
if (i - 1 >= 0 and maze[i - 1][j] == 1 and
!visited[i - 1][j]) {
cout <<'['<< i - 1 << ':'<<j << ']' ;
node temp1(i - 1, j);
visited[i - 1][j] = true;
s.push(temp1);
}
}
// Checking the left direction
else if (d == 1) {
if (j - 1 >= 0 and maze[i][j - 1] == 1 and
!visited[i][j - 1]) {
cout << '[' << i << ':' << j-1 << ']' ;
node temp1(i, j - 1);
visited[i][j - 1] = true;
s.push(temp1);
}
}
// Checking the down direction
else if (d == 2) {
if (i + 1 < n and maze[i + 1][j] == 1 and
!visited[i + 1][j]) {
cout << '[' << i + 1 << ':' << j << ']' ;
node temp1(i + 1, j);
visited[i + 1][j] = true;
s.push(temp1);
}
}
// Checking the right direction
else if (d == 3) {
if (j + 1 < m and maze[i][j + 1] == 1 and
!visited[i][j + 1]) {
cout << '[' << i << ':' << j + 1<< ']' ;
node temp1(i, j + 1);
visited[i][j + 1] = true;
s.push(temp1);
}
}
// If none of the direction can take
// the rat to the Food, retract back
// to the path where the rat came from.
else {
visited[temp.x][temp.y] = false;
s.pop();
}
//system("pause");
}
// If the stack is empty and
// no path is found return false.
return false;
}
// Driver code
int main()
{
// Initially setting the visited
// array to true (unvisited)
memset(visited, false, sizeof(visited));
// Maze matrix
int maze[N][M] = {
{ 1, 0, 1, 1, 0 },
{ 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 1 },
{ 1, 1, 1, 1, 1 }
};
// Food coordinates
fx = 2;
fy = 3;
if (isReachable(maze)) {
cout << "Path Found!" << '\n';
}
else
cout << "No Path Found!" << '\n';
return 0;
}```
解决方案
通过结合您已有的条件......您有以下检查第二个维度:
if (j + 1 < m and maze[i][j + 1] == 1 and !visited[i][j + 1])
如果您想同时增加两者,您可以执行以下操作:
if (i + 1 < n and j + 1 < m and maze[i + 1][j + 1] == 1 and !visited[i + 1][j + 1])
由于您有 4 条对角线,因此您将对其他 3 条对角线进行类似的检查。小心复制和粘贴,不要忘记增加/减少 i 和 j。
推荐阅读
- javascript - iMacros - 删除字符串中的最后一个字符
- laravel - 我将图像保存在 storage/app/public 中,为什么当我在服务器图像上运行网站时不显示
- c# - Sharpkml 在文件夹中插入图标
- java - 为什么 for 循环的条件部分中的“False”显示错误但“True”工作正常?
- java - 如果数据尚不存在,如何将数据添加到 Firestore
- python - SQLAlchemy 在列名中创建带有空格的模型
- arrays - Node/EJS:循环遍历具有多个父/子对象的 JSON 对象
- javascript - 在纯 JS 中为 100 个动态创建的行创建分页
- python - 二分搜索没有给出正确的结果
- react-native - 在 React Native 中获得 Invariant Violation。实现启动画面