#include<iostream> using namespace std; int maze [5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, }; typedef struct {int x;int y;}Point; int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}}; Point path[40];int len=-1; void dfs(int x,int y) { int k,nx,ny; maze[x][y]=1; path[++len].x=x;path[len].y=y; if(x==4 && y==4){//结束条件 cout<<"one case:"<<endl; for(k=0;k<=len;k++) cout<<path[k].x<<‘\t‘<<path[k].y<<endl; } else for(k=0;k<4;k++) { nx=x+dir[k][0];ny=y+dir[k][1];//下一位置(nx,ny) if(nx>=0 &&nx<5 &&ny>=0 && ny<5 && maze[nx][ny]==0){ dfs(nx,ny); } } maze[x][y]=0;//把(nx,ny)尝试后清除当前标记 len--; } int main() { dfs(0,0); }
原文:https://www.cnblogs.com/ewitt/p/14623119.html