auto为什么会导致死循环?

新手上路,请多包涵
#include<bits/stdc++.h>
using namespace std;
#define L(i,l,r) for(int i=l;i<r;++i)
#define R(i,l,r) for(int i=r;i>=l;--i)
const int N=510;
int n,m,dis[3][N][N],dire[3][N][N];
char g[N][N];
string s[4]={"上","右","下","左"};
struct state{
    int x,y,lie;
}q[N*N*3],pre[3][N][N];
bool check(int x,int y){
    if(x>=n||y>=m||x<0||y<0)return false;
    return g[x][y]!='#';
}
int bfs(state st,state end){
    L(k, 0, 3)
    L(i, 0, n)memset(dis[k][i],-1,m*4);
    int d[3][4][3]={
        {{-2,0,2},{0,1,1},{1,0,2},{0,-2,1}},//立(上右下左)(x,y,lie)
        {{-1,0,1},{0,2,0},{1,0,1},{0,-1,0}},//横
        {{-1,0,0},{0,1,2},{2,0,0},{0,-1,2}}//竖
    };
    int hh=0,tt=0;
    q[tt++]=st;
    dis[st.lie][st.x][st.y]=0;
    dire[st.lie][st.x][st.y]=-1;
    while(hh<=tt){
        auto t=q[hh++];
        L(i, 0, 4){
            auto[x,y,lie]=t;
            x+=d[lie][i][0],y+=d[lie][i][1],lie=d[lie][i][2];
            if(!check(x,y))continue;
            if(!lie&&g[x][y]=='E')continue;
            if(lie==1&&!check(x,y+1))continue;
            if(lie==2&&!check(x+1,y))continue;
            if(dis[lie][x][y]==-1){
                dis[lie][x][y]=dis[t.lie][t.x][t.y]+1;
                pre[lie][x][y]={t.x,t.y,t.lie};
                dire[lie][x][y]=i;
                q[tt++]={x,y,lie};
            }
        }
    }
    return dis[end.lie][end.x][end.y];
}
int main(){
    while(scanf("%d%d",&n,&m),n||m){
        L(i, 0, n)scanf("%s",g[i]);
        state st={-1},end;
        L(i, 0, n)
            L(j, 0, m){
                if(g[i][j]=='X'&&st.x==-1){
                    st={i,j};
                    if(g[i][j+1]=='X')st.lie=1;
                    else if(g[i+1][j]=='X')st.lie=2;
                    else st.lie=0;
                }
                if(g[i][j]=='O')end={i,j,0};
            }
        int t=bfs(st, end);
        if(t==-1)puts("Impossible");
        else{
            printf("%d\n",t);
            auto[x,y,lie]=end;
            vector<int>l;
            while(dire[lie][x][y]!=-1){
                l.push_back(dire[lie][x][y]);
                auto now=pre[lie][x][y];
                x=now.x,y=now.y,lie=now.lie;
                // auto[x,y,lie]=now;
            }
            reverse(l.begin(), l.end());
            for(int v:l)printf("%s",s[v].c_str());
            puts("");
        }
    }
    return 0;
}

请问上述代码的第70行换成第71行注释的内容时,为什么会造成死循环。

题目: https://www.acwing.com/problem/content/174/

阅读 2.2k
2 个回答

auto 会声明新的变量,从而不会改变 while 循环外的 x,y,lie 的值。

while(....) 中的 x,y,lie 使用的 while 循环外的 x,y,lie ,在循环中不会被改变,于是就死循环了。

// ...
while (dire[lie][x][y] != -1) {
    l.push_back(dire[lie][x][y]);
    auto now = pre[lie][x][y];
    x = now.x, y = now.y, lie = now.lie;
    // auto[x, y, lie] = now;
}
// ...
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题