#include <iostream> #include <fstream> using namespace std; #define M 10 #define N 10 #define MaxSize 100 int mg[M][N]= { {1,1,1,1,1,1,1,1,1,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,1,0,0,0,1,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,0,1,1,1,0,0,0,0,1}, {1,0,0,0,1,0,0,0,0,1}, {1,0,1,0,0,0,1,0,0,1}, {1,0,1,1,1,0,1,1,0,1}, {1,1,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1} }; ofstream ocout; struct { int i; int j; }st[MaxSize]; int num=1; void PathMap(int x1,int y1,int x2,int y2,int d) { if (d==0) { st[d].i=x1; st[d].j=y1; } int di=1; mg[x1][y1]=-1; if (x1==x2&&y1==y2) { ocout.open("c:\\li.txt",ios::app|ios::out); ocout<<"第"<<num<<"条路线如下:"<<"\n"; for (;d>0;d--) { ocout<<st[d].i<<" "<<st[d].j<<"\n"; } ocout.close(); num++; } else while(di<5) { if (di==1) { st[d+1].i=st[d].i-1; st[d+1].j=st[d].j; di=di+1; if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1) { PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1); } } else if (di==2) { st[d+1].i=st[d].i; st[d+1].j=st[d].j+1; di=di+1; if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1) { PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1); } } else if (di==3) { st[d+1].i=st[d].i+1; st[d+1].j=st[d].j; di=di+1; if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1) { PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1); } } else { st[d+1].i=st[d].i; st[d+1].j=st[d].j-1; di=di+1; if (mg[st[d+1].i][st[d+1].j]!=-1&&mg[st[d+1].i][st[d+1].j]!=1) { PathMap(st[d+1].i,st[d+1].j,x2,y2,d+1); } } } mg[x1][y1]=0; } int main() { PathMap(1,1,8,8,0); }