我已经成功地完成了一个迷宫的最短路径算法(见下面的代码)。但是,我想将最短路径的坐标存储到传递给我的函数的 Stack 参数中。有人可以告诉我如何实现这一目标吗?这是我正在研究的迷宫:
图例:1:墙,0:有效路径,s:开始,e:结束
String[][] map = new String[][]
{
new String[] { "1","1","1","0","0","0","1","1","1","1" },
new String[] { "s","0","0","0","1","1","0","0","0","1" },
new String[] { "1","1","0","0","1","0","0","1","0","1" },
new String[] { "1","1","1","0","0","0","0","0","0","1" },
new String[] { "1","0","1","1","1","0","1","1","0","1" },
new String[] { "0","0","0","0","0","0","0","0","0","1" },
new String[] { "0","1","1","1","1","1","1","1","1","1" },
new String[] { "0","0","0","0","0","0","0","0","0","e" },
};
Run Code Online (Sandbox Code Playgroud)
我的算法:
// Pre-condition: Two integers indicating the row and col number to start from,
// a 2d array of string objects representing the map of the maze,
// a …
Run Code Online (Sandbox Code Playgroud) 我创建了列表和"房间"类.我已将房间添加到列表中.现在我坚持编码游戏本身.基本上我想从A房开始,只能按下(S)outh按钮到房间E和(W)est按钮到房间B等等,从房间A到房间E和房间B迭代.此外,我正在考虑为游戏调用另一个类,因此代码可以读取.因此,main将只有几行代码.这是我的代码.无论如何,请随时指出如何优化它.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
roomsList theRooms = new roomsList();
theRooms.allRoomsList();
theRooms.addRoomToEnd("A");
theRooms.addRoomToEnd("B");
theRooms.addRoomToEnd("C");
theRooms.addRoomToEnd("D");
theRooms.addRoomToEnd("E");
theRooms.addRoomToEnd("F");
theRooms.addRoomToEnd("G");
theRooms.addRoomToEnd("H");
theRooms.addRoomToEnd("I");
theRooms.addRoomToEnd("J");
theRooms.addRoomToEnd("K");
theRooms.addRoomToEnd("L");
Console.WriteLine("What is your name?");
string playerName = Console.ReadLine();
Console.WriteLine("================================================================");
Console.WriteLine(playerName + " You have been Chosen, Enter if you dare!!");
Console.WriteLine("================================================================");
Console.WriteLine("(Y)es/(N)o");
string decision = Console.ReadLine();
if (decision == "y")
{
Console.WriteLine("You need to make you way to Room L, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试Reddit的/ r/dailyprogrammer挑战.
我们的想法是找到一个ASCII迷宫的解决方案.不幸的是,递归的工作方式与我预期的不同.程序检查是否有空间移动到当前空间的右侧,左侧,下方或上方.如果有,那么空间被移动到,并且新的协调员再次输入该功能.这一直持续到找到结束.
当找到结束时,程序退出.如果找到死角,则递归将返回到前一点并检查更多方向,这将一直持续到结束.
我的程序运行良好,但即使在备份递归后,迷宫也会绘制我的线条(由'*****'表示).我不知道如何解释,所以我会用图像来提供更好的描述.
每种新颜色代表一条新路径.但是我希望只显示当前的递归路径.例如,在这种情况下,我希望只显示黄色路径.有人能帮助我理解为什么所有路径都存在吗?
import time
import sys
import os
maze = """\
###############
#S # #
### ### ### # #
# # # # #
# ##### ##### #
# # # #
# ### # ### ###
# # # # # #
# # ### # ### #
# # # # # # #
### # # # # # #
# # # # # #
# …
Run Code Online (Sandbox Code Playgroud) 这涉及我只能猜测的是用于生成随机迷宫的某人代码中的缺陷.代码有点长,但大部分都是注释掉的选项和/或没有特别关注随机化.
我得到了2001x2001迷宫从链接dllu竖起并保存为PNG 这里.从那以后,我创造了这个.为了获得蓝色图案,我开始从迷宫的左下角开始填充死角.根据他使用的回溯算法,这就是迷宫开始产生的点:所以如果你跟踪由此产生的死角的踪迹,你可以系统地填充迷宫那一侧的所有死角.换句话说,中心蓝色质量表示从左下角开始直到2678 x 1086处的唯一前沿像素的总可访问区域.
但是有一些东西立即异常,因为蓝色的"分形"似乎重演了.实际上,通过覆盖分形的一部分,旋转和镜像,您可以看到形状的确切对应.这个叠加层的另一个异常将一个大陆的一部分映射到另一个大陆,但奇怪的是这次只有一块大陆.显然这些不是唯一的自动对应.
但是除了死端组件的形状之外,当你放大时,墙壁的实际图案会重复出来.最奇怪的是,重复并不精确,但只有50-60%的墙壁对应.放大和区域的样本:
问题很简单,代码中是什么造成了这种模糊的随机性缺失?
我完全陷入了本周末的家庭作业.
由于一些愚蠢的原因,我的递归遍历在它到达我的迷宫('E')结束时并没有停止,而是继续前进.
这是读者:
public class mainProg {
public static void main(String[] args) {
// The name of the file to open.
Scanner reader = new Scanner(System.in); // Reading from System.in
System.out.println("Enter the name of textfile to be read ( add .txt): ");
String fileName = reader.next();
char temp;
// This will reference one line at a time
String line = null;
int count = 1;
int heightCounter = 0;
try {
// FileReader reads text files in the default encoding. …
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成一个迷宫并在可能的情况下使用 DFS 算法来解决它。我从
生成一个随机迷宫,然后尝试解决它(如果有解决方案)。迷宫已生成
每次代码运行时都是随机的,但生成的迷宫的路径始终相同,并且
尽管迷宫是随机生成的,但似乎总有一条路径。
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
#include <stack>
const int SIZE = 10;
enum Direction {
TOP,
RIGHT,
BOTTOM,
LEFT
};
struct Cell {
bool visited;
bool walls[4]; // top, right, bottom, left
Cell() {
visited = false;
std::fill(walls, walls + 4, true);
}
};
int getRandomNumber(int min, int max) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(min, max);
return dis(gen);
}
bool isValidCell(int row, int col) {
return (row >= 0 && …
Run Code Online (Sandbox Code Playgroud) 我的程序有这个小问题。在 Visual Studio 2012 中它运行良好,但是如果我用 G++ 编译它(是的,由于我上面的原因,我必须使用它来编译),错误信号 11(SIGSEGV) 或 6(SIGABRT) 根据输入被触发. 这是一个编程练习,我有另一个程序(在在线服务器上)用 10 个不同的输入测试我的程序。正如我所说,该程序在使用 Visual Studio 2012 时编译并运行良好。
关于程序:它找到从起点(x,y)到多个出口的最短路径(出口数量无关且不同。可能只有1个出口,也可能有200个)。输入如下:
7 12 // maze height and width
##########.# //
#..........# //
#.###.###### //
#..X#.#..... // the maze blueprint
#.###.#.#### //
#..........# //
############ //
Run Code Online (Sandbox Code Playgroud)
还有我的程序:
#include <iostream>
#include <vector>
typedef struct _laby_t {
int h, w;
char **pohja; // 'pohja' is finnish and means layout
} laby_t;
typedef std::vector<int> monovector;
typedef std::vector< std::vector<int> > bivector;
laby_t *laby_allocate (int r, int c) …
Run Code Online (Sandbox Code Playgroud) 我了解“深度优先”迷宫加速算法,但我需要一些帮助来使用 Javascript 实现它。
我正在尝试读取一个带有12x12 ASCII迷宫的文本文件.但是,我在屏幕上看到的只是一个12x12的星号网格.
我在上一学期编写的CLI扫雷游戏中使用了类似的代码,它工作正常.我不知道我做了什么导致它不起作用......
码:
bool loadBoard(Tile board [][gridSize], string filename) {
ifstream hndl;
char isWall;
hndl.open(filename);
// Check that the file is opened
if (hndl.is_open()) {
for (int row = 0; row < gridSize; row++) {
for (int col = 0; col < gridSize; col++) {
hndl >> isWall;
if (isWall == '*')
board[row][col].wall = true;
cout << row << col << isWall << " ";
}
cout << endl;
}
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
文件maze.txt:
************
* * * …
Run Code Online (Sandbox Code Playgroud) 我正在进行一项任务,以解决从二维字符数组创建的迷宫.为了测试程序,我制作了一个简单的4x4迷宫.但是,当打印到屏幕上时,迷宫由数字组成.我对这种情况如何发生感到非常困惑.任何帮助,将不胜感激.
作业是这样的:
char *maze[4][4];
for (int i=0; i < 4; ++i)
{
maze[0][i] = "#";
maze[3][i] = "#";
maze[1][i] = ".";
}
maze[2][0] = "#";
maze[2][3] = "#";
maze[2][1] = ".";
maze[2][2] = ".";
Run Code Online (Sandbox Code Playgroud)
和印刷在这里:
for(int i =0; i < 4; ++i)
{
for(int j = 0; j < 4; ++j)
{
printf("%c",maze[i][j]);
}
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
我希望它打印出来:
####
....
#..#
####
Run Code Online (Sandbox Code Playgroud)
但相反它打印:
0000
2222
0220
0000
Run Code Online (Sandbox Code Playgroud) 我正在用MIPS编写一个使用左手规则算法解决迷宫的程序.我已经编写了算法,但是我需要找到一种方法来跟踪我已经访问过的迷宫中的空间,以便找到解决迷宫的"最佳"和最直接的解决方案.
在程序中,寄存器$ t9是一个32位数字,用于存储有关穿过迷宫的汽车位置的信息,包括列和行位置,这是我需要隔离的.基本上,我需要知道的是如何使用/隔离那些特定的位.
位31-24是一个8位数字,表示2的恭维中的行位23-16是一个8位数字,表示2的恭维中的列
tl; dr我只需要从MIPS中的$ t9中的32位数字中提取前8位和下一位8位
谢谢!
我正在尝试从照片中找到迷宫的位置。
我想得到的是迷宫角落的 (x,y) 点。
正如你所看到的,我cv2.Canny()
对图片进行了应用,并得到了一个非常漂亮干净的图像作为开始。
所以下一步就是定位迷宫。
我已经搜索了一段时间,所有 SOF 问题都要求找到“完美”矩形的位置,例如这个和这个 但在我的情况下,矩形没有闭合轮廓,因此它们的代码不起作用就我而言。
也看过 OpenCV 代码,他们都试图找到轮廓并将这些轮廓绘制到图像上,但它对我不起作用。我刚刚得到了 1 个大轮廓,它单独出现在我照片的边界上。
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
Run Code Online (Sandbox Code Playgroud)
更新 1
代码:
import cv2
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
import imutils
img = cv2.imread('maze.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
edges = cv2.Canny(img,100,200)
f,axarr = plt.subplots(1,2,figsize=(15,15))
axarr[0].imshow(img)
axarr[1].imshow(edges)
plt.show()
Run Code Online (Sandbox Code Playgroud)