求救,莫名其妙的错误

P1363 幻象迷宫

#include<cstdio> #include<cstring> #include<queue> using namespace std; int m,n,sx,sy; inline int nx(int x){ while(x<0) x+=n; return x; } inline int ny(int y){ while(y<0) y+=m; return y; } char ditu[1500][1500]; bool block[1500][1500]; bool used[1500][1500]; int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}}; struct node{ int x,y; node(int a,int b): x(a),y(b){} }; inline bool bfs(int x,int y){ queue<node> ltk; ltk.push(node(x,y)); block[x][y]=true; while(!ltk.empty()){ node temp=ltk.front(); ltk.pop(); for(register int i=0;i<4;++i){ int a=temp.x+dir[i][0],b=temp.y+dir[i][1]; int nnx=nx(a)%n,nny=ny(b)%m; //printf("%d %d %d %d\n",a,b,nnx,nny); if(nnx==sx&&nny==sy&&(a!=sx||b!=sy)) return true; if(((!block[nnx][nny])||!used[a+n][b+m])&&ditu[nnx][nny]!='#'){ block[nnx][nny]=true; used[a+n][b+m]=true; ltk.push(node(a,b)); } } } return false; } int main(){ //freopen("he.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF){ memset(block,false,sizeof(block)); memset(used,false,sizeof(used)); getchar(); for(register int i=0;i<n;++i){ for(register int j=0;j<m;++j){ ditu[i][j]=getchar(); if(ditu[i][j]=='S') sx=i,sy=j; //printf("%d %d %c\n",i,j,ditu[i][j]); } getchar(); } //printf("%d %d\n",sx,sy); if(bfs(sx,sy)) printf("Yes\n"); else printf("No\n"); } return 0; }
by 曹孟德 @ 2019-05-22 22:40:04


No,咋发代码啊,Orz
by 曹孟德 @ 2019-05-22 22:40:47


在代码前面打“```cpp”就可以了
by charliegong @ 2019-05-22 22:45:54


like: ```cpp #include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; cout<<a+b; return 0; }
by charliegong @ 2019-05-22 22:47:22


发现问题了,我把stdio全换成iostream的输入输出就过了,有大神可以解释一下吗,本地两者都正常
by 曹孟德 @ 2019-05-22 23:15:40


记得以前也有一次碰到过这种问题,但是没有去想,,,
by 曹孟德 @ 2019-05-22 23:17:25


@[曹孟德](/space/show?uid=154731) 同样的问题
by xyf007 @ 2019-07-12 10:11:45


我发现这个测评系统有坑,getchar()的时候发现它好像会藏一些字符(例如行末有空格) 测试了一下发现,它有一个读入的时候本来应该只有一个换行的结果出现了两个(就是输入数据中间有一个空行) 而且下载数据下来看是没有的 所以用cin,和用scanf读入字符串都没事,但是用getchar(),getline()和scanf读入字符的时候就会出现多读了一个换行,然后后面数据全乱直接WA掉 用下面代码读入会出错 ```cpp while(scanf("%d %d",&n,&m)!=EOF){ memset(map,0,sizeof(map)); c=getchar(); while(c!='\n') c=getchar(); for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(c=='#') map[i][j]=1; if(c=='S') sx=i,sy=j; c=getchar(); } if(i!=n-1) c=getchar(); } ``` 用下面代码读入就不会 ```cpp char line[1500]; while(scanf("%d %d",&n,&m)!=EOF){ memset(map,0,sizeof(map)); for(i=0;i<n;i++){ scanf("%s",line); for(j=0;j<m;j++){ if(line[j]=='#') map[i][j]=1; if(line[j]=='S') sx=i,sy=j; } } ```
by ep938936 @ 2019-08-23 14:04:57


|