Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
顺序果然很重要,922MS,求非递归解法,附代码#include<iostream> #include<string> #define SIZE 30 using namespace std; int n,m; int tcase,times=1; int go[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; bool hash[SIZE][SIZE]={false}; void Init(void) { memset(hash,false,sizeof(hash)); hash[0][0]=true; } bool canGo(int x,int y) { return (x>=0&&y>=0)&&(x<n&&y<m)&&(hash[x][y]==false); } bool DFS(int x,int y,int tot,string ans) { if(tot==n*m) { cout<<ans<<endl<<endl; return true; } else { for(int i=0;8-i>0;i++) { int x1=x+go[i][0]; int y1=y+go[i][1]; char ans1=y1+'A'; char ans2=x1+'1'; if(canGo(x1,y1)) { hash[x1][y1]=true; if(DFS(x1,y1,tot+1,ans+ans1+ans2)) return true; hash[x1][y1]=false; } } return false; } } int main() { for(scanf("%d",&tcase);tcase-times>=0;times++) { Init(); scanf("%d %d",&n,&m); cout<<"Scenario #"<<times<<":"<<endl; if(DFS(0,0,1,"A1")==false) cout<<"impossible"<<endl<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator