C++ 老鼠在迷宫中的问题,如何显示老鼠在随机生成的矩阵中步进的坐标?
我开始编码,在网上搜索了很多代码,但找不到解决问题的方法。我要把我当前的代码粘贴到那里。我已经编码过,如果rat有完美的路径或者没有(rat只能踩在1号上,“0”和“2”是墙),但是我不能显示每个“1号”的坐标。例如:C++ 老鼠在迷宫中的问题,如何显示老鼠在随机生成的矩阵中步进的坐标?,c++,C++,我开始编码,在网上搜索了很多代码,但找不到解决问题的方法。我要把我当前的代码粘贴到那里。我已经编码过,如果rat有完美的路径或者没有(rat只能踩在1号上,“0”和“2”是墙),但是我不能显示每个“1号”的坐标。例如: 1,0,1, 1,2,2, 1,1,1, 那么路径是,(0,0)->(1,0)->(2,0)->(1,2)->(2,2) 如果矩阵是 1,0,2, 0,2,1, 1,0,1, 那老鼠就没有路了。 请编辑我的代码,并粘贴到那里,请或如果你已经解决了我的问题,请与我分享它的程序。。
1,0,1,
1,2,2,
1,1,1,
那么路径是,(0,0)->(1,0)->(2,0)->(1,2)->(2,2)
如果矩阵是
1,0,2,
0,2,1,
1,0,1,
那老鼠就没有路了。
请编辑我的代码,并粘贴到那里,请或如果你已经解决了我的问题,请与我分享它的程序。。。我在C++中是新的。
// CPP program to solve Rat in a maze
// problem with backtracking using stack
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
#define N 4
#define M 5
void mtxkeres(int (&mat)[N][M]);
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;
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] and
visited[i - 1][j]) {
node temp1(i - 1, j);
visited[i - 1][j] = false;
s.push(temp1);
}
}
// Checking the left direction
else if (d == 1) {
if (j - 1 >= 0 and maze[i][j - 1] and
visited[i][j - 1]) {
node temp1(i, j - 1);
visited[i][j - 1] = false;
s.push(temp1);
}
}
// Checking the down direction
else if (d == 2) {
if (i + 1 < n and maze[i + 1][j] and
visited[i + 1][j]) {
node temp1(i + 1, j);
visited[i + 1][j] = false;
s.push(temp1);
}
}
// Checking the right direction
else if (d == 3) {
if (j + 1 < m and maze[i][j + 1] and
visited[i][j + 1]) {
node temp1(i, j + 1);
visited[i][j + 1] = false;
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] = true;
s.pop();
}
}
// 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, true, 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;
}
//求解迷宫中老鼠的CPP程序
//使用堆栈的回溯问题
#包括
#包括
#包括
使用名称空间std;
#定义n4
#定义M 5
无效mtxkeres(内部和材料)[N][M];
类节点{
公众:
int x,y;
int dir;
节点(inti,intj)
{
x=i;
y=j;
//初始方向
//设置为0
dir=0;
}
};
//n*m矩阵的迷宫
int n=n,m=m;
//食物坐标
国际外汇基金;
布尔访问[N][M];
布尔是可调的(整数迷宫[N][M])
{
//最初从(0,0)开始。
int i=0,j=0;
堆栈s;
节点温度(i,j);
s、 推动(温度);
而(!s.empty()){
//弹出顶部节点并移动到顶部节点
//左、右、上、下或缩回
//根据节点的
//dir变量。
温度=s.顶部();
int d=临时目录;
i=温度x,j=温度y;
//增加方向和方向
//再次在堆栈中推送节点。
temp.dir++;
s、 pop();
s、 推动(温度);
//如果我们到达食物坐标
//返回真值
如果(i==fx和j==fy){
返回true;
}
//检查向上方向。
如果(d==0){
如果(i-1>=0且迷宫[i-1][j]和
访问[i-1][j]){
节点temp1(i-1,j);
访问[i-1][j]=错误;
s、 推(temp1);
}
}
//检查左方向
else如果(d==1){
如果(j-1>=0且迷宫[i][j-1]和
访问[i][j-1]){
节点temp1(i,j-1);
访问[i][j-1]=错误;
s、 推(temp1);
}
}
//检查下降方向
else如果(d==2){
如果(i+1 我可以开始编码并在网上搜索很多代码吗?从一个计划开始。列出你想要的(要求)和你需要做的(任务)为了得到你想要的。当一项任务太大以至于你不能坐下来写代码时,把它当作另一个项目来对待:列出需求并将其分解为任务。除非你所获取的内容简短易懂,否则从互联网上获取代码通常会让你陷入误解的兔子洞,浪费时间试图打败som这并不能真正实现你想要的改变,最终也无法学习。好吧,至少你发布的代码中没有像你这样提出问题的新手会发布的那些恼人的cin
语句。我相信你。无关:有一个名为的变量,它包含当你在一两年内开始学习这个课程来改进它时,你的非访客职位将会受到伤害