这些是板子,防止自己忘掉 实际操作中不限于这些
dfs
int check(int x,int y){
if(x<1||x>n||y<1||y>m)return 0;
if(vis[x][y]||s[x][y]=='.')return 0;
return 1;
}
void dfs(int x,int y){
for(int i=0;i<4;i++){
if(check(x+x1[i],y+y1[i])){
vis[x+x1[i]][y+y1[i]]=1;
dfs(x+x1[i],y+y1[i]);
}
}
}
bfs
const int maxn=100;
int vis[maxn][maxn];
int step[maxn][maxn];
int s[maxn][maxn];
struct node{
int x,y,cnt;
};
node fa[maxn][maxn];
int x1[]={-1,1,0,0};
int y1[]={0,0,-1,1};
int n,m;
int check(int x,int y){
if(x<1||x>n||y<1||y>m)return 0;
if(vis[x][y]||s[x][y]=='.')return 0;
return 1;
}
int bfs(){
memset(vis,0,sizeof vis);
node now;
now.x=1;now.y=1;now.cnt=0;
queue<node>q;
vis[now.x][now.y]=1;
q.push(now);
while(!q.empty()){
now=q.front();
q.pop();
step[now.x][now.y]=now.cnt;
// if(now.cnt==)return now.cnt;
for(int i=0;i<4;i++){
node next;
if(check(now.x+x1[i],now.y+y1[i])){
next.x=now.x+x1[i];next.y=now.y+y1[i];
vis[next.x][next.y]=1;
fa[next.x][next.y]=now;
q.push(next);
}
}
}
return -1;
}
void print(node now){
stack<node>s;
for(;;){
s.push(now);
// if(now.x==&&now.y==)break;
now=fa[now.x][now.y];
}
while(!s.empty()){
now=s.top();
//printf()
s.pop();
}
}