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 |
那可以给一组测试反例吗?In Reply To:Re:按一定顺序搜索完全可以解决!! Posted by:sza at 2006-08-19 21:44:14 #include <iostream> using namespace std; int flag[30][30],k,row,col; int step_x[30],step_y[30]; int dfs(int ,int); const int dir_y[]={-2,-2,-1,-1,1,1,2,2}; const int dir_x[]={-1,1,-2,2,-2,2,-1,1}; int main() { int Case,i; cin>>Case; for(i=1;i<=Case;i++) { cin>>row>>col; cout<<"Scenario #"<<i<<":"<<endl; memset(flag,0,sizeof(flag)); flag[1][1]=1; step_x[0]=step_y[0]=1; k=1; if(dfs(1,1)) for(i=0;i<k;i++) cout<<char(step_y[i]+64)<<step_x[i]; else cout<<"impossible"; cout<<endl<<endl; } return 0; } int dfs(int x,int y) { if(k==row*col) return 1; int i; for(i=0;i<8;i++) { int g=x+dir_x[i]; int h=y+dir_y[i]; if(flag[g][h]==0&&g>0&&g<=row&&h>0&&h<=col) { flag[g][h]=1; step_x[k]=g; step_y[k]=h; k++; if(dfs(g,h)) return 1; else { flag[g][h]=0; k--; } } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator