#include<iostream> using namespace std; typedef struct node { int i,j; int pre; }*ptrn; void main() { ptrn *quelist; quelist=new ptrn[100]; int front=0,rear=0; int matrix[10][10]={ {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}}; cout<<"请输入入口在矩阵中的行位置和列位置"<<endl; int i1,j1; cin>>i1>>j1; cout<<"请输入出口在矩阵中的行位置和列位置"<<endl; int i2,j2; cin>>i2>>j2; ptrn stnode; stnode=new node; stnode->i=i1; stnode->j=j1; stnode->pre=-1; quelist[0]=stnode; rear=1; while(1) { if(quelist[front]->i!=i2||quelist[front]->j!=j2) { int i3=quelist[front]->i; int j3=quelist[front]->j; if(matrix[i3][j3+1]==0) { quelist[rear]=new node; quelist[rear]->i=i3; quelist[rear]->j=j3+1; quelist[rear]->pre=front; matrix[i3][j3+1]=1; rear++; } if(matrix[i3+1][j3]==0) { quelist[rear]=new node; quelist[rear]->i=i3+1; quelist[rear]->j=j3; quelist[rear]->pre=front; matrix[i3+1][j3]=1; rear++; } if(matrix[i3][j3-1]==0) { quelist[rear]=new node; quelist[rear]->i=i3; quelist[rear]->j=j3-1; quelist[rear]->pre=front; matrix[i3][j3-1]=1; rear++; } if(matrix[i3-1][j3]==0) { quelist[rear]=new node; quelist[rear]->i=i3-1; quelist[rear]->j=j3; quelist[rear]->pre=front; matrix[i3-1][j3]=1; rear++; } front++; if(front==rear+1) { cout<<"无路径"<<endl; break; } } else { break; } } while(1) { if(quelist[front]->pre==-1) { cout<<"["<<quelist[front]->i<<","<<quelist[front]->j<<"]"<<endl; break; } else { cout<<"["<<quelist[front]->i<<","<<quelist[front]->j<<"]"<<endl; } front=quelist[front]->pre; } }