#include <bits/stdc++.h>
using namespace std;
char map1[1000][1000];
int visted[1000][1000];
vector<pair<int,int>>a;
vector<vector<pair<int,int>>>b;
int n,m,xs,ys,xt,yt,ans=0x3f3f3f3f;
int dirs[4][4]={{-1,0},{1,0},{0,-1},{0,1}};
void dfs(int x ,int y,int step){
if(x==xt && y == yt){
ans = min(ans,step);
b.push_back(a);
return;
}
for(int i = 0 ; i < 4 ; ++i){
int next_x = x + dirs[i][0];
int next_y = y + dirs[i][1];
if(next_x>=1 && next_x <=n && next_y >=1 && next_y<=m &&map1[next_x][next_y]=='.' && visted[next_x][next_y]==0){
visted[next_x][next_y]=1;
a.push_back({next_x,next_y});
dfs(next_x,next_y,step+1);
visted[next_x][next_y]=0;
a.pop_back();
}
}
}
int main(int argc, char** argv){
freopen("/newhome/wangjd/leetcode/testin.txt","r",stdin);
cin>>n>>m>>xs>>ys>>xt>>yt;
for(int i = 1 ; i <= n;++i){
for(int j = 1 ; j <= m;++j){
cin>>map1[i][j];
}
}
a.push_back({xs,ys});
visted[xs][ys]=1;
dfs(xs,ys,0);
cout<<ans<<endl;
for(auto i: b){
for(auto j : i){
cout<<j.first<<','<<j.second<<endl;
}
cout<<"-----------"<<endl;
}
}
dfs练习
最新推荐文章于 2024-07-22 09:38:37 发布