问题描述
我已经完成了我的任务。而且我也在打印坐标(从源到老鼠访问/到达死角的目的地),但我只想打印从源到目的地的最终老鼠路径(不包括访问和死角)。我正在创建一个函数“void print(node p)”(其中节点是类名,p 是对象,但是当我在 while 循环之后调用它时,它不显示任何内容。当我在每一行中调用它时,它会显示所有坐标(访问/死点)。有人能告诉我我做错了什么吗?我应该怎么做才能从堆栈中打印最终元素?
班级
class node {
public:
int x,y;
int pos;
node(int i,int j)
{
x = i;
y = j;
// Initially Position
// set to 0
pos = 0;
}
/*void setValue(int i,int j)
{
x = i;
y = j;
}*/
int getrow()
{
return x;
}
int getcol()
{
return y;
}
};
打印坐标函数
void print(node p)
{
cout << "(" << p.getrow() << "," << p.getcol() << ")" << "\t";
}
检查死胡同、开放路径和回溯的功能
bool isReachable(int maze[SIZE][SIZE],int& count)
{
bool visited[SIZE][SIZE];
int i = 0,j = 0;
cout << endl;
cout <<"\n" <<"Start at cordinate" << "("<<i<< "," << j << ")"<<endl;
cout << endl;
stack<node> s;
node path1(i,j);
node temp(i,j);
s.push(temp);
while (!s.empty()) {
// Pop the top node and move to
// left,right,top,down or
// backtrack according the value of node's
// pos variable.
temp = s.top();
int d = temp.pos;
i = temp.x,j = temp.y;
// Increment the direction and
// push the node in the stack again.
temp.pos++;
s.pop();
s.push(temp);
// If we reach the coordinates
// return true
if (i == SIZE-1 and j == SIZE-1) {
count++;
cout << endl;
cout << "\n"<<"Exit at coordinate" <<"(" <<i <<","<<j <<")"<< endl;
maze[i][j]=3;
return true;
}
// Checking the Up direction.
if (d == 0)
{
if (i - 1 >= 0 && maze[i - 1][j] == 0 && visited[i - 1][j])
{
node temp1(i - 1,j);
visited[i - 1][j] = false;
s.push(temp1);
count++;
maze[i][j] = 3;
path1 = s.top();
print(path1);
}
}
// Checking the left direction
else if (d == 1)
{
if (j - 1 >= 0 && maze[i][j - 1] == 0 && visited[i][j - 1])
{
node temp1(i,j - 1);
visited[i][j - 1] = false;
s.push(temp1);
count++;
maze[i][j] = 3;
path1 = s.top();
print(path1);
}
}
// Checking the down direction
else if (d == 2)
{
if (i + 1 < SIZE && maze[i + 1][j] == 0 && visited[i + 1][j])
{
node temp1(i + 1,j);
visited[i + 1][j] = false;
s.push(temp1);
count++;
maze[i][j] = 3;
path1 = s.top();
print(path1);
}
}
// Checking the right direction
else if (d == 3)
{
if (j + 1 < SIZE && maze[i][j + 1] == 0 && visited[i][j + 1])
{
node temp1(i,j + 1);
visited[i][j + 1] = false;
s.push(temp1);
count++;
maze[i][j] = 3;
path1 = s.top();
print(path1);
}
}
// If none of the direction leads to end point
// pop the value from where it enter.
else
{
visited[temp.x][temp.y] = true;
s.pop();
count--;
maze[i][j] = -1;
path1 = s.top();
print(path1);
}
}
return count;
return 0;
}
主要功能
int main()
{
// Initially setting the visited
// array to true (unvisited)
bool visited[SIZE][SIZE];
memset(visited,true,sizeof(visited));
int maze[SIZE][SIZE] =
{//0,1,2,3,4,5,6,7,8,9,{ 0,0 },//0
{ -1,-1,-1 },//1
{ 0,//2
{ 0,//3
{ 0,//4
{ 0,//5
{ 0,//6
{ 0,//7
{ 0,//8
{ 0,0 } //9
};
int count = 0;
cout <<'\n'<<"Original maze!" << '\n';
cout << endl;
printMaze(maze);
if (isReachable(maze,count)) {
cout << '\n' << "Final Path Maze"<<endl;
cout << "Path length:" << count<<endl;
cout << endl;
}
else
cout << "No Exit Found!" << '\n';
printMaze(maze);
getchar();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)