第二题跟你思路差不多 #include <stdio.h> #include <iostream> #include <algorithm> #include <vector> using namespace std; int map[11][11]; int cnt[100][100]; int flag[100][100]; int inf=100; struct point { int x; int y; }; int f(int n,int m,vector<point> &vec) { int x=0,y=0; while (1) { if(y+1<n&&map[x][y+1]==1&&flag[x][y+1]==0) { cnt[x][y+1]=cnt[x][y]+1; y=y+1; point p;p.x=x,p.y=y; vec.push_back(p); flag[x][y]=1; } else if(x-1<n&&map[x-1][y]==1&&flag[x-1][y]==0) { cnt[x-1][y]=cnt[x][y]+3; x=x-1; point p;p.x=x,p.y=y; vec.push_back(p); flag[x][y]=1; } else if(x+1<n&&map[x+1][y]==1&&flag[x+1][y]==0) { cnt[x+1][y]=cnt[x][y]; x=x+1; point p;p.x=x,p.y=y; vec.push_back(p); flag[x][y]=1; } else if(y-1<n&&map[x][y-1]==1&&flag[x][y-1]==0) { cnt[x][y-1]=cnt[x][y]+1; y=y-1; point p;p.x=x,p.y=y; vec.push_back(p); flag[x][y]=1; } else { point p=*(vec.end()-1); map[p.x][p.y]=0; flag[p.x][p.y]=0; vec.pop_back(); } //cout<<x<<","<<y<<endl; if(x==0&&y==m-1) break; } return vec.size(); } int main() { int n,m,p; while (cin>>n>>m>>p) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cin>>map[i][j]; } vector<point> vec; int rtn = f(n,m,vec); if(rtn>=p) cout<<"Can not escape!"<<endl; else { cout<<"[0,0]"; for(int i=0;i<vec.size();i++) cout<<",["<<vec[i].x<<","<<vec[i].y<<"]"; cout<<endl; } } }