提交时间:2024-08-21 17:17:25

运行 ID: 180667

#include<bits/stdc++.h> using namespace std; set< pair<int,int> > st; stack< pair<int,int> > stk; stack<string> sk; int dt[5][2]={{0,0},{-2,1},{-1,2},{1,2},{2,1}}; int n,m,cmp=1; int main(){ cin>>m>>n; pair<int,int> sp; sp.first=1; sp.second=1; st.insert(sp); stk.push(sp); sk.push(""); while(!stk.empty()){ sp=stk.top();stk.pop(); string str=sk.top();sk.pop(); if(sp.first==n && sp.second==m){ cout<<str<<endl; return 0; } for(int i=4;i>=1;i--){ pair<int,int> to; to.first=sp.first+dt[i][0]; to.second=sp.second+dt[i][1]; if(to.first>=1 && to.first<=n && to.second>=1 && to.second<=m && !st.count(to)){ st.insert(to); stk.push(to); string tostr=str; if(cmp==0){ tostr+=" "; } tostr+=char(i+'0'); sk.push(tostr); } } cmp=0; } cout<<"-1"<<endl; return 0; }