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 |
这道题很让人郁闷!!!一直WA,自己造的测试数据都能过,无奈看了看DISUCUSS,发现了神奇的字母序。在我的印象里,字母序不就是strcmp么。但是和这个结果输出有什么关系啊??希望有人能解答下。 我把搜索顺序改成DISUCUSS上的就过了.... #include <iostream> using namespace std; bool visited[50][50]; int p,q; char rounte[100000]; int rear; int off_x[8] = {-1,1,-2,2,-2,2,-1,1}; int off_y[8] = {-2,-2,-1,-1,1,1,2,2}; bool dfs(int left, int a, int b) { int i; //完成遍历,退出 if(left==p*q) { return true; } //八个方向的测试 for(i=0; i<8; i++) { //位置不合法 if(a+off_x[i]<0 || a+off_x[i]>=p || b+off_y[i]<0 || b+off_y[i]>=q) { continue; } //访问过,剪枝 if(visited[a+off_x[i]][b+off_y[i]]) { continue; } visited[a+off_x[i]][b+off_y[i]]=true; rounte[rear++]=(char)('A'+b+off_y[i]); rounte[rear++]=(char)('1'+a+off_x[i]); //递归成功 if(dfs(left+1, a+off_x[i], b+off_y[i])) { return true; } //还原状态回溯 visited[a+off_x[i]][b+off_y[i]]=false; rear -= 2; } return false; } int main() { int t,k,i; cin >> t; k=1; while(t--) { memset(visited,false,sizeof(visited)); memset(rounte,'\0',sizeof(rounte)); cin>>p>>q; visited[0][0]=true; rear=2; rounte[0]='A'; rounte[1]='1'; cout << "Scenario #"<<k++<<":" << endl; if(dfs(1,0,0)) { rounte[rear]='\0'; for(i=0; i<rear; i++) { cout << rounte[i]; } cout << endl; } else { cout << "impossible" << endl;; } cout << 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