如果加上这句话:
#include <bits/stdc++.h>
using namespace std;
int n,m;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
char a[105][105];
bool found,vi[105][105];
bool check(int x,int y){
if(x<1||y<1||x>n||y>m)return false;
if(a[x][y]=='#')return false;
if(vi[x][y])return false;
return true;
}
void dfs(int x,int y){
if(x==n&&y==m){
found=true;
return ;
}
vi[x][y]=true;
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(check(nx,ny))dfs(nx,ny);
}
return ;
}
int main(){
ios::sync_with_stdio(false);
cin >>n >>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin >>a[i][j];
}
}
dfs(1,1);
cout <<(found?"Yes":"No");
cout<<"\n-------------------\n";
system("pause");
return 0;
}
最后是这样的:
3 5
.##.#
.#...
...#.
按任意键继续...
Yes
-------------------
但是把 ios::sync_with_stdio(false); 注释掉之后就是这样的:
3 5
.##.#
.#...
...#.
Yes
-------------------
按任意键继续...
就会恢复正常。
这是为什么啊?