走迷宫
#include<iostream> #include<vector> #include<cstdio> #include<cstring> using namespace std; const int maxsize=16; int a[maxsize][maxsize]= {0}; bool b[maxsize][maxsize]= {false}; int flag=0; struct Point { int x,y; Point(int c,int d):x(c),y(d){} }; int nextto[4][2]= { {0,-1},{-1,0},{0,1},{1,0} }; vector<Point> path; int n,m,start_x,start_y,end_x,end_y; void search(int x,int y) { if(x==end_x&&y==end_y) { flag=1; for(int i=0; i<path.size()-1; i++) { printf("(%d,%d)->",path[i].x,path[i].y); } printf("(%d,%d)\n",path[path.size()-1].x,path[path.size()-1].y); return; } for(int i=0; i<4; i++) { int newx=x+nextto[i][0]; int newy=y+nextto[i][1]; if(newx<1||newx>m||newy<1||newy>n||b[newx][newy]==true)continue; if(a[newx][newy]==1&&b[newx][newy]==false) { path.push_back(Point(newx,newy)); b[newx][newy]=true; search(newx,newy); path.pop_back(); b[newx][newy]=false; } } } int main() { while(scanf("%d %d",&m,&n)!=EOF) { flag=0; path.clear(); memset(a,0,sizeof(a)); memset(b,false,sizeof(b)); for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) { scanf("%d",&a[i][j]); } } scanf("%d %d",&start_x,&start_y); path.push_back(Point(start_x,start_y)); b[start_x][start_y]=true; scanf("%d %d",&end_x,&end_y); search(start_x,start_y); if(flag==0) { printf("-1\n"); } } return 0; }