提交时间:2024-08-21 10:04:00

运行 ID: 175672

#include<bits/stdc++.h> using namespace std; int road[100]; int dx[5]={0,1,2,2,1}; int dy[5]={0,-2,-1,1,2}; int f; int ax,ay; void dfs(int x,int y,int k){ if (f==1){ return; } if(x==ax&&y==ay){ f=1; for(int i=0;i<k;i++){ cout<<road[i]<<" "; } return; } if((ay-y)>2*(ax-x)){ return; } for(int i=1;i<=4;i++){ int nx=x+dx[i]; int ny=y+dy[i]; if(!f&&nx>=1&&nx<=ax&&ny>=1&&ny<=ay){ road[k]=i; dfs(nx,ny,k+1); } } return; } int main(){ cin>>ax>>ay; dfs(1,1,0); if(f==0){ cout<<-1; } return 0; }