c++的两个问题只是稍微改变了一点,但答案却截然不同

发布于 2024-10-21 00:46:45 字数 3788 浏览 2 评论 0原文

最近我用c++做了一个关于算法的练习。在这里练习:poj

我发现两个非常困惑的问题。 我写了一个类 MAZE,MAZE 中有三个主要函数,它们是 int left_path();int right_path();int mini_path(); 以及打印答案的函数:

void display(){
    cout<<left_path()<<" "<<right_path()<<" ";
    cout<<mini_path()<<endl;
}

程序可以正常工作。正如我们所见,函数 display() 很容易; 我这样写

void display(){
    cout<<left_path()<<" "<<right_path()<<" "<<mini_path()<<endl;
}

只是一处更改;但是程序无法运行,它就像无限循环。

以下是另一个问题: 函数mini_path的框架是这样的,

int maze::mini_path(){
    ini();
    queue<pair<int,int> > q;
    q.push(make_pair(x,y));
    while(!q.empty()){

        pair<int,int> tmp=q.front();
       q.pop();

       int t=...;
       if(E){
          return t;
       }
       if(E){
        S
       }
       if(E){
         S
       }
       if(E){
          S
       }
       if(E){
          S
       } 

   }
    return -1;
}  

如果最后有“return -1”,则函数正常工作,否则函数返回随机大数。

程序只有一个文件,我使用gun编译器。

我没有显示全部代码,因为我认为没有人想看到它们。我只是想问一下可能会出现什么问题导致上述奇怪的行为。

源代码(针对问题2进行了简化):

    typedef enum {LEFT=-1,RIGHT=1,UP,DOWN} direction;

    ifstream fin("file_test3.txt");
    class maze{
    public:
        maze(){input();}
        int mini_path();
        void input();
        void display(){
            cout<<mini_path()<<endl;
        }
    private:
        bool is_not_dest(){
            return !(x==d_x && y==d_y);
        }
        void ini_dir()
        {
            if(e_x==0) dir=DOWN;
            else if(e_x==height-1) dir=UP;
            else if(e_y==0) dir=RIGHT;
            else dir=LEFT;
        }
        void ini(){
            x=e_x;
            y=e_y;
            path_lenth=1;
            ini_dir();
        }
        direction dir,d;
        int width,height,maze_map[40][40],path_lenth;
        int x,y,e_x,e_y,d_x,d_y;
    };
    void maze::input()
{
    fin>>width>>height;
    char sym;
    for(int i=0;i<height;++i)
        for(int j=0;j<width;++j){
            fin>>sym;
            if(sym=='#')
                maze_map[i][j]=1;
            else if(sym=='.')
                maze_map[i][j]=0;
            else if(sym=='S'){
                maze_map[i][j]=-1;
                e_x=i;
                e_y=j;
            }
            else {
                maze_map[i][j]=-2;
                d_x=i;
                d_y=j;
            }
        }
}

int maze::mini_path()
{
    ini();
    queue<pair<int,int> > q;

    if(dir==LEFT) {maze_map[x][--y]=2;}
    else if(dir==RIGHT) {maze_map[x][++y]=2;}
    else if(dir==UP) {maze_map[--x][y]=2;}
    else {maze_map[++x][y]=2;}

    q.push(make_pair(x,y));
   while(!q.empty()){
        pair<int,int> tmp=q.front();
        q.pop();
        x=tmp.first;
        y=tmp.second;
        int t=maze_map[x][y]+1;
        if((x==d_x && (y-d_y==1 || y-d_y==-1)) ||(y==d_y && (x-d_x==1||x-d_x==-1))){
            return t;
        }
        if(maze_map[x-1][y]==0){
            maze_map[x-1][y]=t;
            q.push(make_pair(x-1,y));
        }
        if(maze_map[x+1][y]==0){
            maze_map[x+1][y]=t;
            q.push(make_pair(x+1,y));
        }
        if(maze_map[x][y-1]==0){
            maze_map[x][y-1]=t;
            q.push(make_pair(x,y-1));
        }
        if(maze_map[x][y+1]==0){
            maze_map[x][y+1]=t;
            q.push(make_pair(x,y+1));
        }
    }
    return -1;
}

main()
{
    int n;
    fin>>n;
    while(n-- >0){
        class maze m;
        m.display();
    }
}

Recently I do a exercise about algorithm with c++. Exercise in here:poj

I find two very confused questions.
I write a class MAZE and there are three primary functions in MAZE,they are
int left_path();int right_path();int mini_path();
and a function to print the answers:

void display(){
    cout<<left_path()<<" "<<right_path()<<" ";
    cout<<mini_path()<<endl;
}

the program can work correctly.As we see the function display() can be easy;
I write like this

void display(){
    cout<<left_path()<<" "<<right_path()<<" "<<mini_path()<<endl;
}

just one change ;however the program can't work,it like loop infinitely.

following is the other question:
the function mini_path's frame like this

int maze::mini_path(){
    ini();
    queue<pair<int,int> > q;
    q.push(make_pair(x,y));
    while(!q.empty()){

        pair<int,int> tmp=q.front();
       q.pop();

       int t=...;
       if(E){
          return t;
       }
       if(E){
        S
       }
       if(E){
         S
       }
       if(E){
          S
       }
       if(E){
          S
       } 

   }
    return -1;
}  

if there is "return -1" in the end ,the function works right,else the function return random big number.

The program is in only one file and i use the gun compiler.

I don't show the total codes,because i think nobody wants to see them.I just want to ask what problems may lead above strange behaviors.

source code(simplified for question2):

    typedef enum {LEFT=-1,RIGHT=1,UP,DOWN} direction;

    ifstream fin("file_test3.txt");
    class maze{
    public:
        maze(){input();}
        int mini_path();
        void input();
        void display(){
            cout<<mini_path()<<endl;
        }
    private:
        bool is_not_dest(){
            return !(x==d_x && y==d_y);
        }
        void ini_dir()
        {
            if(e_x==0) dir=DOWN;
            else if(e_x==height-1) dir=UP;
            else if(e_y==0) dir=RIGHT;
            else dir=LEFT;
        }
        void ini(){
            x=e_x;
            y=e_y;
            path_lenth=1;
            ini_dir();
        }
        direction dir,d;
        int width,height,maze_map[40][40],path_lenth;
        int x,y,e_x,e_y,d_x,d_y;
    };
    void maze::input()
{
    fin>>width>>height;
    char sym;
    for(int i=0;i<height;++i)
        for(int j=0;j<width;++j){
            fin>>sym;
            if(sym=='#')
                maze_map[i][j]=1;
            else if(sym=='.')
                maze_map[i][j]=0;
            else if(sym=='S'){
                maze_map[i][j]=-1;
                e_x=i;
                e_y=j;
            }
            else {
                maze_map[i][j]=-2;
                d_x=i;
                d_y=j;
            }
        }
}

int maze::mini_path()
{
    ini();
    queue<pair<int,int> > q;

    if(dir==LEFT) {maze_map[x][--y]=2;}
    else if(dir==RIGHT) {maze_map[x][++y]=2;}
    else if(dir==UP) {maze_map[--x][y]=2;}
    else {maze_map[++x][y]=2;}

    q.push(make_pair(x,y));
   while(!q.empty()){
        pair<int,int> tmp=q.front();
        q.pop();
        x=tmp.first;
        y=tmp.second;
        int t=maze_map[x][y]+1;
        if((x==d_x && (y-d_y==1 || y-d_y==-1)) ||(y==d_y && (x-d_x==1||x-d_x==-1))){
            return t;
        }
        if(maze_map[x-1][y]==0){
            maze_map[x-1][y]=t;
            q.push(make_pair(x-1,y));
        }
        if(maze_map[x+1][y]==0){
            maze_map[x+1][y]=t;
            q.push(make_pair(x+1,y));
        }
        if(maze_map[x][y-1]==0){
            maze_map[x][y-1]=t;
            q.push(make_pair(x,y-1));
        }
        if(maze_map[x][y+1]==0){
            maze_map[x][y+1]=t;
            q.push(make_pair(x,y+1));
        }
    }
    return -1;
}

main()
{
    int n;
    fin>>n;
    while(n-- >0){
        class maze m;
        m.display();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷默言语 2024-10-28 00:46:45

我看到了!你能看到吗? :)

#include <iostream>

using namespace std;

int foo(int bar)
{
    cout << bar << endl;
    return bar;
}

int _tmain(int argc, _TCHAR* argv[])
{
    cout << foo(1) << foo(2) << foo(3) << endl;
    return 0;
}

输出:

3
2
1
123

I see it! Can you see it? :)

#include <iostream>

using namespace std;

int foo(int bar)
{
    cout << bar << endl;
    return bar;
}

int _tmain(int argc, _TCHAR* argv[])
{
    cout << foo(1) << foo(2) << foo(3) << endl;
    return 0;
}

The output:

3
2
1
123
小矜持 2024-10-28 00:46:45

关于问题1:
调用函数的顺序会有所不同。
第一个解决方案将按以下顺序调用它们:

  1. right_path
  2. left_path
  3. mini_path

第二个解决方案按以下顺序生成:

  1. mini_path
  2. right_path
  3. left_path

所以您可能想要的解决方案是:

void display(){
    cout<<left_path()<<" ";
    cout<<right_path()<<" ";
    cout<<mini_path()<<endl;
}

regarding question1:
The order in which the functions are called will be different.
the first solution will call them in following order:

  1. right_path
  2. left_path
  3. mini_path

the second solution results in following order:

  1. mini_path
  2. right_path
  3. left_path

so the solution you probaly want is:

void display(){
    cout<<left_path()<<" ";
    cout<<right_path()<<" ";
    cout<<mini_path()<<endl;
}
眼眸里的那抹悲凉 2024-10-28 00:46:45

没有足够的信息来回答第一个问题;两个代码是等效的。

[编辑:检查其他答案。无论如何,两个代码应该等效:你的代码中有错误。]

关于第二个问题,我猜“return -1”标志着你的迷宫中“没有可能的路径”,这就是为什么,当您删除它时,您的程序将停止工作。

在迷宫问题中,回溯算法逐格移动。当从一个方格开始没有可能的路径时,必须将该方格标记为无路径。

There is not enough info to answer the first question; both codes are equivalent.

[Edit:Check other answers. Anyway, both codes should be equivalent: you have bugs in your code.]

About the second question, I guess that that "return -1" marks "no possible path" in your maze, that's why, when you remove it, your program stops working.

In the maze problem, a backtracking algorithm moves square by square. When from a square there is no possible path, this square must be marked as no path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文