# 【练习回顾】dfs迷宫+路径打印

## 输出格式

#include<stdio.h>
#include<string.h>

int N,M;
int map[8][8];
int x1,y1,x2,y2;
int cnt = 0;
char stack[100][30];
int top = -1;

int isAble(int x, int y){ return 0<=x && x<N && 0<=y && y<M && map[x][y]==0; }

void dfs(int x,int y){
char tmp[30];
sprintf(tmp,"(%d,%d)",x,y);
top++; strcpy(stack[top],tmp);

if(x == x2 && y == y2){
cnt++;

for(int i=0;i<=top;++i) {
printf("%s",stack[i]);
}puts("");

stack[top--];
return;
}
if(isAble(x+1, y)) {
map[x+1][y] = 1;
dfs(x+1, y);
map[x+1][y] = 0;
}
if(isAble(x-1, y)){
map[x-1][y] = 1;
dfs(x-1, y);
map[x-1][y] = 0;
}
if(isAble(x, y+1)){
map[x][y+1] = 1;
dfs(x, y+1);
map[x][y+1] = 0;
}
if(isAble(x, y-1)){
map[x][y-1] = 1;
dfs(x, y-1);
map[x][y-1] = 0;
}

stack[top--];
return;
}

int main(){
scanf("%d%d",&N,&M);
for(int i=0;i<N;++i) for(int j=0;j<M;++j)
scanf("%d",&map[i][j]);
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1 = x1 - 1; x2 = x2 - 1;
y1 = y1 - 1; y2 = y2 - 1;

dfs(x1,y1);
printf("%d",cnt);
return 0 ;
}

posted @ 2021-11-02 00:33  Elucidator_xrb  阅读(104)  评论(0编辑  收藏  举报