输出为什么只到(5,5)啊,悬关

P1238 走迷宫

@[wym_2012](/user/929529) 因为你没有保存5-6
by wzb13958817049 @ 2023-12-03 09:50:22


@[wzb13958817049](/user/917584) dalao可以帮忙改下吗谢谢
by wym_2012 @ 2023-12-03 09:57:34


@[wym_2012](/user/929529) ```cpp #include<bits/stdc++.h> using namespace std; int to[4][2]={0,-1,-1,0,0,1,1,0}; bool vis[19][19]; char mp[19][19]; int n,m; struct node{ int x,y; }; vector<node>xf; node u; int sx,sy,fx,fy; int flag; void dfs(int x,int y){ u.x=x;u.y=y; if(u.x==fx&&u.y==fy){ flag=1; for(int i=0;i<xf.size();i++){ if(i==0) cout<<"("<<xf[i].x<<","<<xf[i].y<<")"; else cout<<"->("<<xf[i].x<<","<<xf[i].y<<")"; } cout<<endl; return; } for(int i=0;i<4;i++){ int tx,ty; tx=x+to[i][0]; ty=y+to[i][1]; if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty]&&mp[tx][ty]!='0'){ vis[tx][ty]=true; xf.push_back({tx,ty}); dfs(tx,ty); vis[tx][ty]=false; xf.pop_back(); } } } int main(){ cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>mp[i][j]; cin>>sx>>sy>>fx>>fy; vis[sx][sy]=1;xf.push_back({sx,sy}); dfs(sx,sy); if(flag==0) cout<<"-1"; return 0; } ``` 看似小改,实则全改,dfs还是改成了我习惯写的那种,过了
by wzb13958817049 @ 2023-12-03 10:20:26


6 @[wzb13958817049](/user/917584)
by nizixuan @ 2023-12-03 10:22:07


@[nizixuan](/user/774988) 倪总大驾光临
by wzb13958817049 @ 2023-12-03 10:22:56


@[nizixuan](/user/774988) %%%膜拜大佬
by wzb13958817049 @ 2023-12-03 10:23:23


@[wzb13958817049](/user/917584) 谢谢大佬已关
by wym_2012 @ 2023-12-03 10:24:55


@[wym_2012](/user/929529) 1.你最后答案(5,6)没输出 只要输出一下就好了 2. ```cpp vis[x][y]=false; xf.pop_back(); ``` 你这两句可能要放循环外面(因为我是这么写的) 最后是代码 ```cpp #include<bits/stdc++.h> using namespace std; int to[4][2]={0,-1,-1,0,0,1,1,0}; bool vis[19][19]; char mp[19][19]; int n,m; struct node{ int x,y; }; vector<node>xf; node u; int sx,sy,fx,fy; int flag; void dfs(int x,int y) { u.x=x,u.y=y; if(u.x==fx&&u.y==fy) { flag=1; for(int i=0;i<xf.size();i++) { if(i==0) cout<<"("<<xf[i].x<<","<<xf[i].y<<")"; else cout<<"->("<<xf[i].x<<","<<xf[i].y<<")"; } cout<<"->("<<u.x<<","<<u.y<<")"; cout<<endl; return; } vis[u.x][u.y]=true; xf.push_back(u); for(int i=0;i<4;i++) { int tx,ty; tx=x+to[i][0]; ty=y+to[i][1]; if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty]&&mp[tx][ty]!='0') { dfs(tx,ty); } } vis[x][y]=false; xf.pop_back(); } int main() { cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>mp[i][j]; cin>>sx>>sy>>fx>>fy; dfs(sx,sy); if(flag==0) cout<<"-1"; return 0; } ``` 我试过了,100分
by Alvin_Wang @ 2023-12-03 10:32:54


@[Alvin_Wang](/user/749755) 谢谢已过
by wym_2012 @ 2023-12-03 10:34:46


此帖完
by wym_2012 @ 2023-12-03 10:35:05


|