Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo questions of c++ which just change a little,however very different answers
    text
    copied!<p>Recently I do a exercise about algorithm with c++. Exercise in here:<a href="http://poj.org/problem?id=3083" rel="nofollow">poj</a></p> <p>I find two very confused questions. I write a class MAZE and there are three primary functions in MAZE,they are <code>int left_path();int right_path();int mini_path();</code> and a function to print the answers:</p> <pre><code>void display(){ cout&lt;&lt;left_path()&lt;&lt;" "&lt;&lt;right_path()&lt;&lt;" "; cout&lt;&lt;mini_path()&lt;&lt;endl; } </code></pre> <p>the program can work correctly.As we see the function display() can be easy; I write like this </p> <pre><code>void display(){ cout&lt;&lt;left_path()&lt;&lt;" "&lt;&lt;right_path()&lt;&lt;" "&lt;&lt;mini_path()&lt;&lt;endl; } </code></pre> <p>just one change ;however the program can't work,it like loop infinitely.</p> <p>following is the other question: the function mini_path's frame like this</p> <pre><code>int maze::mini_path(){ ini(); queue&lt;pair&lt;int,int&gt; &gt; q; q.push(make_pair(x,y)); while(!q.empty()){ pair&lt;int,int&gt; 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; } </code></pre> <p>if there is "return -1" in the end ,the function works right,else the function return random big number.</p> <p><strong>The program is in only one file and i use the gun compiler.</strong></p> <p><strong>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.</strong></p> <p>source code(simplified for question2):</p> <pre><code> 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&lt;&lt;mini_path()&lt;&lt;endl; } private: bool is_not_dest(){ return !(x==d_x &amp;&amp; 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&gt;&gt;width&gt;&gt;height; char sym; for(int i=0;i&lt;height;++i) for(int j=0;j&lt;width;++j){ fin&gt;&gt;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&lt;pair&lt;int,int&gt; &gt; 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&lt;int,int&gt; tmp=q.front(); q.pop(); x=tmp.first; y=tmp.second; int t=maze_map[x][y]+1; if((x==d_x &amp;&amp; (y-d_y==1 || y-d_y==-1)) ||(y==d_y &amp;&amp; (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&gt;&gt;n; while(n-- &gt;0){ class maze m; m.display(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload