- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试编写一个简单的 C++ 程序来填充距离起点有一定距离的迷宫,但我失败了。
代码是:
/*I'm trying to write a simple C++ program to fill a maze with distances from the starting point, but am failing.
The pseudocode for the function is
Set every element of the distance array to 99.
Set position (sr,sc) of the distance array to 0.
Push the starting coordinate (sr,sc) onto the coordinate stack.
While the stack is not empty,
{ Pop the top coordinate off the stack. This gives you the current
(r,c) location that your algorithm is exploring.
Let m be the minimum of the values of the four neighbors of (r,c)
in the distance array. If the value at (r,c) in the distance
array is greater than m+1, then set it to m+1.
Let v be the value at (r,c) in the distance array.
Check where you can move from the current position:
If you can move NORTH and the value at position (r-1,c) in the
distance array is greater than v+1, then set it to v+1 and
push (r-1,c) onto the coordinate stack.
If you can move EAST and the value at position (r,c+1) in the
distance array is greater than v+1, then set it to v+1 and
push (r,c+1) onto the coordinate stack.
If you can move SOUTH and the value at position (r+1,c) in the
distance array is greater than v+1, then set it to v+1 and
push (r+1,c) onto the coordinate stack.
If you can move WEST and the value at position (r,c-1) in the
distance array is greater than v+1, then set it to v+1 and
push (r,c-1) onto the coordinate stack.
And the code is
*/
//#include "StdAfx.h"
#include <stack>
#include<iostream>
#include <iomanip>
using namespace std;
class Coord
{
public:
Coord(int rr, int cc) : m_r(rr), m_c(cc) {}
int r() const { return m_r; }
int c() const { return m_c; }
private:
int m_r;
int m_c;
};
int minimum(int a[],int size)
{
int small=a[0];
for(int i=0;i<size;i++)
if(a[i]<small)
small=a[i];
return small;
}
void determineDistances(const char maze[][10], int sr, int sc, int dist[][10])
{
stack<Coord> coordStack; //from HW spec
coordStack.push(Coord(sr,sc));
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
dist[i][j]=99;
}
dist[sr][sc]=0;
int distarr[4]; // Array to hold up, down, left, right values
int min;
int currval; // Current value in dist[][10] array
while (!coordStack.empty())
{
Coord inuse = coordStack.top();
coordStack.pop(); // Pop the top coordinate off the stack
const int row = inuse.r(); // Assigning row value
const int col = inuse.c(); // Assigning col value
// (row,col) location that your algorithm is exploring
cout<<"test row "<<row<<" col "<<col<<endl;
distarr[0]=dist[row-1][col]; // Up
distarr[1]=dist[row+1][col]; // Down
distarr[2]=dist[row][col-1]; // Left
distarr[3]=dist[row][col+1]; // Right
min=minimum(distarr,4); // Let max be the minimum of the values of the four neighbors of (r,c)
// in the distance array
if(dist[row][col]>min+1) // If the value at (r,c) in the distance
dist[row][col]=min+1; // array is greater than m+1, then set it to m+1.
currval=dist[row][col];
if ((maze[row-1][col] == '.')&&(dist[row-1][col]>(currval+1)))
{
dist[row-1][col]=currval+1;
coordStack.push(Coord(row+1,col));
}
if (maze[row+1][col] == '.'&&(dist[row+1][col]>(currval+1)))
{
dist[row+1][col]=currval+1;
coordStack.push(Coord(row+1,col));
}
if (maze[row][col+1] == '.'&&(dist[row][col+1]>(currval+1)))
{
dist[row][col+1]=currval+1;
coordStack.push(Coord(row,col+1));
}
if (maze[row][col-1] == '.'&&(dist[row][col-1]>(currval+1)))
{
dist[row][col-1]=dist[row][col]+1;
coordStack.push(Coord(row,col-1));
}
//maze[row][col]='C';
}
}
int main()
{
char myMaze[10][10] = {
{ 'X','X','X','X','X','X','X','X','X','X'},
{ 'X','.','.','.','.','.','X','.','.','X'},
{ 'X','X','.','X','X','.','.','.','.','X'},
{ 'X','.','.','X','.','.','.','X','.','X'},
{ 'X','.','.','.','X','X','.','X','X','X'},
{ 'X','X','X','.','.','.','.','X','.','X'},
{ 'X','X','.','.','.','X','.','X','.','X'},
{ 'X','X','.','X','.','X','.','X','X','X'},
{ 'X','X','.','.','.','X','.','.','.','X'},
{ 'X','X','X','X','X','X','X','X','X','X'}
};
int distances[10][10];
determineDistances(myMaze, 6, 3, distances);
for (int r = 0; r < 10; r++)
{
for (int c = 0; c < 10; c++)
cout << ' ' << setw(2) << distances[r][c];
cout << endl;
}
// Output should be
// 99 99 99 99 99 99 99 99 99 99
// 99 7 6 7 8 9 99 9 10 99
// 99 99 5 99 99 8 7 8 9 99
// 99 5 4 99 8 7 6 99 10 99
// 99 4 3 2 99 99 5 99 99 99
// 99 99 99 1 2 3 4 99 99 99
// 99 99 1 0 1 99 5 99 99 99
// 99 99 2 99 2 99 6 99 99 99
// 99 99 3 4 3 99 7 8 9 99
// 99 99 99 99 99 99 99 99 99 99
std::cin.get();
//but the output is
/*
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 1 2 99 99 99 99 99
99 99 1 0 1 99 99 99 99 99
99 99 2 1 2 99 99 99 99 99
99 99 3 2 3 99 99 99 99 99
99 99 99 99 6 99 99 99 99 99
*/
//Any ideas on what the error is?
}
输出:
test row 6 col 3
test row 6 col 2
test row 7 col 2
test row 8 col 2
test row 8 col 3
test row 8 col 4
test row 9 col 4
test row 6 col 4
test row 7 col 4
test row 8 col 4
test row 7 col 4
test row 7 col 3
test row 8 col 3
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 1 2 99 99 99 99 99
99 99 1 0 1 99 99 99 99 99
99 99 2 1 2 99 99 99 99 99
99 99 3 2 3 99 99 99 99 99
99 99 99 99 6 99 99 99 99 99
我在想我可能在某处遗漏了推送指令。任何想法将不胜感激。
最佳答案
这部分:
if ((maze[row-1][col] == '.')&&(dist[row-1][col]>(currval+1)))
{
dist[row-1][col]=currval+1;
coordStack.push(Coord(row+1,col));
}
最后一行应该是 row-1
而不是 row+1
。
关于c++ - 基于堆栈的迷宫探索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4867421/
假设您已经用 Python 编写了一个 m x n 矩阵。矩阵之外的值是不可能的。假设你是在矩阵中移动的东西(就像在迷宫中)并且你不能跨越边界。当您在迷宫中移动时,您会不断考虑您的选择,您可以走哪条路
我正在实现随机鼠标算法来探索迷宫。一段时间后,算法陷入无限循环。我调试了一下,它似乎在一条 channel 之间来回卡住了。 请看一下我的算法实现。 这是我的代码:方向是相对于机器人的。 public
我有一个用 java 编写的工作 ascii 迷宫解算器,使用 char 数组,它将正确路径的每个位置设置为前一个位置 + 1。我使用以下代码来从中获取正确路径,但是它仅适用于垂直运动。任何有关此事的
我有一个生成随机迷宫的程序。迷宫中会显示一个红点,并且迷宫中的每个方 block 都会闪烁红点。迷宫中的所有 block 都是 == 1,如果红点穿过该 block ,它就会递增++。红点朝最小数字的
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我创建了一个从文本文件上传的迷宫,该迷宫当前在运行时完全可见且功能正常。但是,我只想将播放的路线显示为可见,因此仅使起始位置和周围的墙壁/地板在开始时可见。有人知道该怎么做吗? 以下是 Board 类
起初我觉得这很容易,但是当我开始做的时候,我不知道如何继续下去了。我的想法是使用面板,然后绘制粗线,但是绘制墙壁并使我的角色不会超出这些墙壁的正确方法是什么?我无法想象我怎么可能做到这一点。这是一个迷
我从一个文件中得到了一个迷宫,我尝试使用一个程序编写一个类Exercise4,该程序将这样的迷宫文件读入二维 boolean 数组。然后在控制台上显示该数组,每一行一行。使用空白符号和 # 符号表示数
如何通过光栅图像数据找到非线性路径?例如,最低成本算法?起点和终点已知,并给出如下: 起点 = (0,0) 终点 = (12,-5) 例如,通过(灰度)光栅图像提取蜿蜒河流的近似路径。 # fake
在我的游戏中,玩家在迷宫中导航。我不知道如何与墙壁进行正确的碰撞检测。停留在某个区域很容易进行碰撞检测: if (x > rightWallX - playerWidth) x = rightWall
基本上,我一直在按照 Java 教程制作一个基本的迷宫游戏,其中我生成一个随机迷宫,并将其保存到文件中,然后使用 Jpanel 将其打印出来,但是在编译时我不断收到此错误。 Exception in
注意:这是 MSVC,C++17 问题。 免责声明:我知道有人尝试过,是的,我试图找到相关的 SO 答案。 我可以编码 UDL , 以实现将数字文字转换为 std::array,在编译时: /
我目前正在开发一个随机迷宫生成器,它将迷宫存储在一个名为 grid 的二维数组中。这将在稍后用于生成一个真正的 3D 迷宫,用户随后可以穿过该迷宫。 在做了一些研究之后,我尝试使用递归除法算法创建这个
题目地址:https://leetcode-cn.com/problems/the-maze-ii/ 题目描述 There is a ball in a maze with empty space
我正在尝试用 python 编写脚本来解决一种具有多个起点和多个终点的迷宫。正确的路径是从起点沿着直线前进。 例如一个有 4 条路径的迷宫: 起初我想使用左手/右手规则,但由于迷宫的特点,它没有太大意
我正在尝试在 opengl 中创建一个简单的 3D 迷宫。我最初的想法是有一个立方体网格,每个立方体的一些面是透明的(用于走廊)。但是,我在想出一种有效执行此操作的方法时遇到了一些麻烦。我不想为我的迷
我的 DFS 算法在解中缺少节点时遇到问题(检查图片)。每次我的算法遇到死胡同时都会发生这种情况:节点从堆栈中弹出并返回,直到找到可用的移动,并且再也不会重新包含在内。有没有一种简单的方法可以在不重新
所以我正在用 Java 构建 pacman 游戏来自学游戏编程。 我有一个基本的游戏窗口,其中绘制了吃 bean Sprite 和幽灵 Sprite ,吃 bean 使用箭头键移动,不会超出窗口的墙壁
我使用的代码只是取自一个示例,它确实为我的场景建了一堵墙: /** This loop builds a wall out of individual bricks. */ public vo
我正在从事一个包含这些条件的学校元素: 只使用 JS、HTML5 和 CSS 制作迷宫。 在 Angular 色周围制作 torch 效果。你不能穿墙照明。 我开始使用 Canvas 制作这款游戏
我是一名优秀的程序员,十分优秀!