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 |
第二次做这道题,换个string来保存,自认为想法比较神奇,呵呵,虽然效率不高#include<iostream> #include <cstring> #include <string> using namespace std; bool board[27][27],is_find; int p,q,T; int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; string ans; bool DFS(int i,int j,int step) { if(i<=0 || j<=0 || i>p || j>q || board[i][j]) return false; board[i][j]=true; if(step==p*q-1) return true; for(int k=0;k<8;++k) { int di=i+dir[k][1]; int dj=j+dir[k][0]; if(DFS(di,dj,step+1)) { ans.insert(0,1,di+'0'); ans.insert(0,1,dj+'A'-1); return true; } } board[i][j]=false; return false; } int main() { cin>>T; for(int s=1;s<=T;++s) { cin>>p>>q; ans.clear(); is_find=false; for(int i=1;i<=p && !is_find;++i) { for(int j=1;j<=q;++j) { memset(board,false,sizeof(board)); if(DFS(i,j,0)){ ans.insert(0,1,i+'0');ans.insert(0,1,j+'A'-1);is_find=true;break;} } } cout<<"Scenario #"<<s<<":"<<endl; if(ans.empty())cout<<"impossible"<<endl<<endl; else cout<<ans<<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