提交时间:2024-08-21 08:59:10

运行 ID: 175504

#include <iostream> using namespace std; const int dx[5]={0, 1, 2,2,1}; const int dy[5]={0,-2,-1,1,2}; int n,m,res[10003]; bool f; // void dfs(int x,int y,int k) { if (f) return; if (x==n && y==m) { f=true; for (int i=1;i<k;++i) printf("%d ",res[i]); return; } if ((m-y)>2*(n-x)) return; int xx,yy; for (int i=1;i<=4;++i) { xx=x+dx[i]; yy=y+dy[i]; if (!f && xx>=1 && xx<=n && yy>=1 && yy<=m) { res[k]=i; dfs(xx,yy,k+1); } } } // int main() { scanf("%d%d",&n,&m); dfs(1,1,1); if (!f) printf("-1\n"); return 0; }