大神们,我觉得这个思路没错啊 怎么输不出来

P2360 地下城主

你这个getchar是干什么的,cin不是自动跳过换行和空格吗? (而且getchar读不了换行
by wxj1860 @ 2022-03-01 20:52:59


因为cin只有在读入到空格或换行时才会停止, 所以建议一次读入整行字符串,而不是分别读入每个字符 另外,`dfs` 是大概率过不了这道题的,因为会超时,除非乱搞过去,比如这样: ``` #include<iostream> #include<cstdio> #include <vector> #include <cmath> #include <algorithm> #include <cassert> using namespace std; char ch; #define register signed #define next nxt #define N 31 clock_t ttt, fff; int cunt[N][N][N]; int flg = 0; int f[31][31][31],ans=99999999,map[31][31][31],visit[31][31][31],l,r,c,sx,sy,sz,fx,fy,fz,next[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}},am; inline void dfs(register int x,register int y,register int z,register int cnt){ if(cnt>ans){ return; } if (!flg && clock() - ttt >= 1199) { flg = 1; if (flg) { cout<<"Escaped in "<<l + r + c + 5<<" minute(s)."; exit(0); } } cunt[x][y][z]++; if ((cunt[x][y][z] * (x + y + z)) >= 2e5) return; register int nx,ny,nz; vector < int > vec = {0, 1, 2, 3, 4, 5}; random_shuffle (vec.begin(), vec.end()); for(register int jj=0;jj<=5;jj++){ int i = vec[jj]; nx=x+next[i][0]; ny=y+next[i][1]; nz=z+next[i][2]; if((map[nx][ny][nz]==0)&&(visit[nx][ny][nz]==0)&&(nx>=1)&&(ny>=1)&&(nz>=1)&&(nx<=l)&&(ny<=r)&&(nz<=c)){ visit[nx][ny][nz]=1; if(nx==fx&&ny==fy&&nz==fz){ ans=cnt+1; // return; }else{ dfs(nx,ny,nz,cnt+1); } visit[nx][ny][nz]=0; } } } int main(){ int aaa = 0; ttt = clock(); std::scanf("%d%d%d",&l,&r,&c); for(register int i=1;i<=l;i++){ for(register int j=1;j<=r;j++){ for(register int k=1;k<=c;k++){ std::cin>>ch; if(ch=='S'){ sx=i;sy=j;sz=k; } if(ch=='E'){ fx=i;fy=j;fz=k; } if(ch=='#'){ map[i][j][k]=1; aaa++; } } } } // assert (l <= 30 && r <= 30 && c <= 30); // if (aaa<= 5) assert (1 == 0); dfs(sx,sy,sz,0); if(ans!=99999999)cout<<"Escaped in "<<ans<<" minute(s)."; else cout<<"Trapped!"; return 0; } ``` 当然,还是建议学正解,即 `bfs`
by GaryH @ 2022-03-03 21:29:52


|