C++用栈实现迷宫的搜索,这个代码哪里有问题?
迷宫是11111111111111111入口-00100011000111111110001101110011111011000011110011111101111011...
迷宫是
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
入口-0 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1
1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1
1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1
1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1
1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1
1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1
1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1
1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 0-出口
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
代码是:
#include<iostream>
#include<assert.h>
using namespace std;
int maze[14][17];
int mark[14][17];
const int m=12;
const int p=15;
struct offsets{
int a,b;
};
offsets move[8]={{1,1},{1,0},{0,1},{0,-1},{1,-1},{-1,1},{-1,0},{-1,-1}};
struct items{
int x,y,dir;
};
template <class T>
class stack
{
public:
stack(int sz);
~stack();
bool push(const T& x);
bool pop(T& x);
bool gettop(T& x);
bool isempty()const{return (top==-1)?true:false;}
bool isfull()const{return (top==maxsize-1)?true:false;}
int getsize()const{return top+1;}
void makeempty(){top=-1;}
private:
int top;
T *elements;
int maxsize;
};
template <class T>
stack<T>::stack(int sz):top(-1),maxsize(sz)
{
elements=new T[maxsize];
assert(elements!=NULL);
}
template<class T>
stack<T>::~stack()
{
delete elements;
}
template<class T>
bool stack<T>::push(const T& x)
{
if(isfull()==true)
{
cerr<<"栈满!"<<endl;
return false;
}
elements[++top]=x;
return true;
}
template <class T>
bool stack<T>::pop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
top--;
return true;
}
template <class T>
bool stack<T>::gettop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
x=elements[top];
return true;
}
int main()
{
int c,r;
for(r=0;r<14;r++){
for(c=0;c<17;c++)
{
cin>>maze[r][c];
}
cout<<endl;
}
for(r=0;r<14;r++)
for(c=0;c<17;c++)
mark[r][c]=0;
int i,j,d,g,h;
mark[1][0]=1;
stack<items> s(m*p);
items temp;
temp.x=1;
temp.y=0;
temp.dir=2;
s.push(temp);
while(!s.isempty()){
s.pop(temp);
i=temp.x;
j=temp.y;
d=temp.dir;
while(d<8){
g=i+move[d].a;
h=j+move[d].b;
if(g==m&&h==p){
cout<<"yes"<<endl;
return 0;
}
else if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=i;
temp.y=j;
temp.dir=d;
s.push(temp);
i=g;j=h;d=0;
}
else d++;
}
}
cout<<"no path in maze"<<endl;
return 0;
}
输出总是"no path in maze",找不到哪里有问题,求助~~在线等!!
这个迷宫是八个方向走的,不只是上下左右四个方向。而且迷宫数据肯定没问题。 展开
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
入口-0 0 1 0 0 0 1 1 0 0 0 1 1 1 1 1 1
1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 1 1
1 0 1 1 0 0 0 0 1 1 1 1 0 0 1 1 1
1 1 1 0 1 1 1 1 0 1 1 0 1 1 0 0 1
1 1 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1
1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1
1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1
1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1
1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 0 1
1 0 1 0 0 1 1 1 1 1 0 1 1 1 1 0 0-出口
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
代码是:
#include<iostream>
#include<assert.h>
using namespace std;
int maze[14][17];
int mark[14][17];
const int m=12;
const int p=15;
struct offsets{
int a,b;
};
offsets move[8]={{1,1},{1,0},{0,1},{0,-1},{1,-1},{-1,1},{-1,0},{-1,-1}};
struct items{
int x,y,dir;
};
template <class T>
class stack
{
public:
stack(int sz);
~stack();
bool push(const T& x);
bool pop(T& x);
bool gettop(T& x);
bool isempty()const{return (top==-1)?true:false;}
bool isfull()const{return (top==maxsize-1)?true:false;}
int getsize()const{return top+1;}
void makeempty(){top=-1;}
private:
int top;
T *elements;
int maxsize;
};
template <class T>
stack<T>::stack(int sz):top(-1),maxsize(sz)
{
elements=new T[maxsize];
assert(elements!=NULL);
}
template<class T>
stack<T>::~stack()
{
delete elements;
}
template<class T>
bool stack<T>::push(const T& x)
{
if(isfull()==true)
{
cerr<<"栈满!"<<endl;
return false;
}
elements[++top]=x;
return true;
}
template <class T>
bool stack<T>::pop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
top--;
return true;
}
template <class T>
bool stack<T>::gettop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
x=elements[top];
return true;
}
int main()
{
int c,r;
for(r=0;r<14;r++){
for(c=0;c<17;c++)
{
cin>>maze[r][c];
}
cout<<endl;
}
for(r=0;r<14;r++)
for(c=0;c<17;c++)
mark[r][c]=0;
int i,j,d,g,h;
mark[1][0]=1;
stack<items> s(m*p);
items temp;
temp.x=1;
temp.y=0;
temp.dir=2;
s.push(temp);
while(!s.isempty()){
s.pop(temp);
i=temp.x;
j=temp.y;
d=temp.dir;
while(d<8){
g=i+move[d].a;
h=j+move[d].b;
if(g==m&&h==p){
cout<<"yes"<<endl;
return 0;
}
else if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=i;
temp.y=j;
temp.dir=d;
s.push(temp);
i=g;j=h;d=0;
}
else d++;
}
}
cout<<"no path in maze"<<endl;
return 0;
}
输出总是"no path in maze",找不到哪里有问题,求助~~在线等!!
这个迷宫是八个方向走的,不只是上下左右四个方向。而且迷宫数据肯定没问题。 展开
展开全部
#include<iostream>
#include<assert.h>
using namespace std;
int maze[14][17];
int mark[14][17];
const int m=12;
const int p=15;
struct offsets{
int a,b;
};
offsets move[8]={{1,1},{1,0},{0,1},{0,-1},{1,-1},{-1,1},{-1,0},{-1,-1}};
struct items{
int x,y,dir;
};
template <class T>
class stack
{
public:
stack(int sz);
~stack();
bool push(const T& x);
bool pop(T& x);
bool gettop(T& x);
bool isempty()const{return (top==-1)?true:false;}
bool isfull()const{return (top==maxsize-1)?true:false;}
int getsize()const{return top+1;}
void makeempty(){top=-1;}
private:
int top;
T *elements;
int maxsize;
};
template <class T>
stack<T>::stack(int sz):top(-1),maxsize(sz)
{
elements=new T[maxsize];
assert(elements!=NULL);
}
template<class T>
stack<T>::~stack()
{
delete elements;
}
template<class T>
bool stack<T>::push(const T& x)
{
if(isfull()==true)
{
cerr<<"栈满!"<<endl;
return false;
}
elements[++top]=x;
return true;
}
template <class T>
bool stack<T>::pop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
top--;
return true;
}
template <class T>
bool stack<T>::gettop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
x=elements[top];
return true;
}
int main()
{
int c,r;
for(r=0;r<14;r++){
for(c=0;c<17;c++)
{
cin>>maze[r][c];
}
cout<<endl;
}
for(r=0;r<14;r++)
for(c=0;c<17;c++)
mark[r][c]=0;
int i,j,d,g,h;
mark[1][0]=1;
stack<items> s(m*p);
items temp;
temp.x=1;
temp.y=0;
temp.dir=2;
s.push(temp);
while(!s.isempty()){
s.pop(temp);
i=temp.x;
j=temp.y;
d=temp.dir;
while(d<8){
g=i+move[d].a;
h=j+move[d].b;
if(g==m&&h==p){
cout<<"yes"<<endl;
return 0;
}
else if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=i;
temp.y=j;
temp.dir=d;
s.push(temp);
i=g;j=h;d=0;
}
else d++;
}
}
cout<<"no path in maze"<<endl;
return 0;}
程序本身就很完美,只是初始化迷宫数据出了问题
试着修改 “cin>>maze[r][c];”即输入时的值
#include<assert.h>
using namespace std;
int maze[14][17];
int mark[14][17];
const int m=12;
const int p=15;
struct offsets{
int a,b;
};
offsets move[8]={{1,1},{1,0},{0,1},{0,-1},{1,-1},{-1,1},{-1,0},{-1,-1}};
struct items{
int x,y,dir;
};
template <class T>
class stack
{
public:
stack(int sz);
~stack();
bool push(const T& x);
bool pop(T& x);
bool gettop(T& x);
bool isempty()const{return (top==-1)?true:false;}
bool isfull()const{return (top==maxsize-1)?true:false;}
int getsize()const{return top+1;}
void makeempty(){top=-1;}
private:
int top;
T *elements;
int maxsize;
};
template <class T>
stack<T>::stack(int sz):top(-1),maxsize(sz)
{
elements=new T[maxsize];
assert(elements!=NULL);
}
template<class T>
stack<T>::~stack()
{
delete elements;
}
template<class T>
bool stack<T>::push(const T& x)
{
if(isfull()==true)
{
cerr<<"栈满!"<<endl;
return false;
}
elements[++top]=x;
return true;
}
template <class T>
bool stack<T>::pop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
top--;
return true;
}
template <class T>
bool stack<T>::gettop(T& x)
{
if(isempty()==true)
{
cerr<<"栈空"<<endl;
return false;
}
x=elements[top];
return true;
}
int main()
{
int c,r;
for(r=0;r<14;r++){
for(c=0;c<17;c++)
{
cin>>maze[r][c];
}
cout<<endl;
}
for(r=0;r<14;r++)
for(c=0;c<17;c++)
mark[r][c]=0;
int i,j,d,g,h;
mark[1][0]=1;
stack<items> s(m*p);
items temp;
temp.x=1;
temp.y=0;
temp.dir=2;
s.push(temp);
while(!s.isempty()){
s.pop(temp);
i=temp.x;
j=temp.y;
d=temp.dir;
while(d<8){
g=i+move[d].a;
h=j+move[d].b;
if(g==m&&h==p){
cout<<"yes"<<endl;
return 0;
}
else if(maze[g][h]==0&&mark[g][h]==0)
{
mark[g][h]=1;
temp.x=i;
temp.y=j;
temp.dir=d;
s.push(temp);
i=g;j=h;d=0;
}
else d++;
}
}
cout<<"no path in maze"<<endl;
return 0;}
程序本身就很完美,只是初始化迷宫数据出了问题
试着修改 “cin>>maze[r][c];”即输入时的值
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询